You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
raiseValueError("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.
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'
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.
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:
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?
Activity
matthewgilbert commentedon Apr 11, 2019
Somewhat related to the above, although possibly a separate issue, is that
bysetposwith minutely data seems to be broken. Withdateutil.__version__ == '2.8.0'whereas the following runs indefinitely
Not exactly sure what's going on here, but at first glance looks like
rrule._iternever reaches itsyieldstatement. This buggy behaviour with minutely data is consistent with the restriction above to (1, 366) which suggestsbysetposwas not initially designed with minutely or secondly data in mind.Eliot5566 commentedon Nov 12, 2026
Reproduced the hang described in the issue on current master (
a78cbd81, release 4.0.0 prep).bysetpos=1returns[2019-01-07 00:00]immediately;bysetpos=2never yields: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 indexesdivmod(pos - 1, len(timeset))into the dayset slice, but for DAILY/HOURLY/MINUTELY/SECONDLY that slice holds exactly one day andtimesetholds one time by default, so only positions 1 and -1 can ever match. Forpos=2the lookup raisesIndexError, which is silently swallowed, so no iteration ever yields andcount/untilnever fire. The same hang affects plainrrule(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?