Skip to content

Arbitrary bysetpos restriction to 1 to 366 #905

Open

Description

@matthewgilbert

bysetpos seems to have an arbitrary restriction to between (1, 366) or (-366, -1)

raise ValueError("bysetpos must be between 1 and 366, "

Unsure what the initial rational for this was, but the restriction doesn't appear to be defined anywhere in RFC 5545.

The BYSETPOS rule part specifies a COMMA-separated list of values
that corresponds to the nth occurrence within the set of
recurrence instances specified by the rule. BYSETPOS operates on
a set of recurrence instances in one interval of the recurrence
rule. For example, in a WEEKLY rule, the interval would be one
week A set of recurrence instances starts at the beginning of the
interval defined by the FREQ rule part. Valid values are 1 to 366
or -366 to -1. It MUST only be used in conjunction with another
BYxxx rule part. For example "the last work day of the month"
could be represented as:

FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1

Each BYSETPOS value can include a positive (+n) or negative (-n)
integer. If present, this indicates the nth occurrence of the
specific occurrence within the set of occurrences specified by the
rule.

Activity

  1. matthewgilbert commented on Apr 11, 2019

    @matthewgilbert
    Author

    Somewhat related to the above, although possibly a separate issue, is that bysetpos with minutely data seems to be broken. With dateutil.__version__ == '2.8.0'

    from datetime import datetime
    from dateutil.rrule import MINUTELY, rrule
    from dateutil.relativedelta import MO
    list(rrule(MINUTELY, dtstart=datetime(2019, 1, 1, 0, 0), bysetpos=1, count=1, byweekday=MO))
    [datetime.datetime(2019, 1, 7, 0, 0)]

    whereas the following runs indefinitely

    list(rrule(MINUTELY, dtstart=datetime(2019, 1, 1, 0, 0), bysetpos=2, count=1, byweekday=MO))

    Not exactly sure what's going on here, but at first glance looks like rrule._iter never reaches its yield statement. This buggy behaviour with minutely data is consistent with the restriction above to (1, 366) which suggests bysetpos was not initially designed with minutely or secondly data in mind.

  2. added this to the Bugfix release milestone on Apr 23, 2019
  3. Eliot5566 commented on Nov 12, 2026

    @Eliot5566

    Reproduced the hang described in the issue on current master (a78cbd81, release 4.0.0 prep). bysetpos=1 returns [2019-01-07 00:00] immediately; bysetpos=2 never yields:

    from datetime import datetime
    from dateutil.rrule import MINUTELY, rrule
    from dateutil.relativedelta import MO
    
    list(rrule(MINUTELY, dtstart=datetime(2019, 1, 1, 0, 0),
               bysetpos=2, count=1, byweekday=MO))  # hangs

    It is not stuck on one line: sampling the running generator shows it advancing minute by minute (2025-08, then 2032-03, still total=0, count=1). The loop only exits at year 9999, so MINUTELY takes ~40 minutes of CPU to return [].

    To be clear, I am not proposing to lift the 1–366 restriction in the issue title: RFC 5545 defines BYSETPOS as 1 to 366 or -366 to -1, so the validation is correct.

    Root cause: in rrule._iter, the BYSETPOS branch indexes divmod(pos - 1, len(timeset)) into the dayset slice, but for DAILY/HOURLY/MINUTELY/SECONDLY that slice holds exactly one day and timeset holds one time by default, so only positions 1 and -1 can ever match. For pos=2 the lookup raises IndexError, which is silently swallowed, so no iteration ever yields and count/until never fire. The same hang affects plain rrule(DAILY, bysetpos=2, count=1) with no BYxxx at all. Per the RFC, BYSETPOS selects within one interval (one day for DAILY, one minute for MINUTELY), so the correct result for these rules is an empty result, not a hang — WEEKLY already returns [] for the same shape of input.

    Proposed fix: detect the case where no requested position can ever match within one interval for DAILY and finer frequencies and return empty immediately, leaving satisfiable positions untouched. Would a PR along those lines be welcome, or do you prefer a different approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions