Search code examples
python-dateutilrrule

rrule dates are offset by a week


I'm getting an unexpected result using the python-dateutil rrule module and I'm wondering if this is WAI.

I'm dynamically creating the rrule using:

dtstart = datetime.date(2019, 1, 7)
until = datetime.date(2029, 11, 29)
freq = MONTHLY
byweekday=MO(2)
interval = 4

This results in the following rrule

DTSTART:20190107T000000
RRULE:FREQ=MONTHLY;INTERVAL=4;UNTIL=20291129T000000;BYDAY=+2MO

However, when generating the dates (looping on the rrule for this python module), I get the following dates:

 [datetime.datetime(2019, 1, 14, 0, 0),
 datetime.datetime(2019, 5, 13, 0, 0),
 datetime.datetime(2019, 9, 9, 0, 0),
 datetime.datetime(2020, 1, 13, 0, 0),
 datetime.datetime(2020, 5, 11, 0, 0),
 datetime.datetime(2020, 9, 14, 0, 0),
 datetime.datetime(2021, 1, 11, 0, 0),
 datetime.datetime(2021, 5, 10, 0, 0),
 datetime.datetime(2021, 9, 13, 0, 0),
 datetime.datetime(2022, 1, 10, 0, 0),
 datetime.datetime(2022, 5, 9, 0, 0),
 datetime.datetime(2022, 9, 12, 0, 0),
 datetime.datetime(2023, 1, 9, 0, 0),
 datetime.datetime(2023, 5, 8, 0, 0),
 datetime.datetime(2023, 9, 11, 0, 0),
 datetime.datetime(2024, 1, 8, 0, 0),
 datetime.datetime(2024, 5, 13, 0, 0),
 datetime.datetime(2024, 9, 9, 0, 0),
 datetime.datetime(2025, 1, 13, 0, 0),
 datetime.datetime(2025, 5, 12, 0, 0),
 datetime.datetime(2025, 9, 8, 0, 0),
 datetime.datetime(2026, 1, 12, 0, 0),
 datetime.datetime(2026, 5, 11, 0, 0),
 datetime.datetime(2026, 9, 14, 0, 0),
 datetime.datetime(2027, 1, 11, 0, 0),
 datetime.datetime(2027, 5, 10, 0, 0),
 datetime.datetime(2027, 9, 13, 0, 0),
 datetime.datetime(2028, 1, 10, 0, 0),
 datetime.datetime(2028, 5, 8, 0, 0),
 datetime.datetime(2028, 9, 11, 0, 0),
 datetime.datetime(2029, 1, 8, 0, 0),
 datetime.datetime(2029, 5, 14, 0, 0),
 datetime.datetime(2029, 9, 10, 0, 0)]

Notice that the first date is offset by a week! Why is this the case? And is this a bug in the library?

Thanks, David


Solution

  • It's not a bug in the library. 2019-01-14 is the first date which matches your rule (it's the 2nd Monday of January 2019). Apparently python-dateutil has chosen to not include the start date you provide, which is completely legit.

    RRULE is specified in RFC 5545, which states in Section 3.8.5.3 (under "Description"):

    The recurrence set generated with a "DTSTART" property value not synchronized with the recurrence rule is undefined.

    Which essentially means there is no right or wrong interpretation because the input data is "broken" if the start date doesn't match the rule.

    Note, many other implementations would probably return both, your start date 2019-01-07 and the result 2019-01-14. I don't think any implementation would omit 2019-01-14, simply because it is the first date which matches the rule. It's debatable whether the start date 2019-01-07 should be in the results or not, but 2019-01-14 should definitely be in there.