Skip to content

Confusing interaction between dtstart and exdate #274

Closed

Description

@andybak

This is probably my lack of understanding but the following behaviour seems surprising:

from datetime import datetime
from dateutil.rrule import rruleset, rrulestr

rules_good = rruleset()
rules_good.rrule(rrulestr("RRULE:FREQ=DAILY", dtstart=datetime(2016,12,20,17,30,00)))
excl_date = datetime(2016,12,25,17,30,00)
rules_good.exdate(excl_date)
rules_good.between(datetime(2016,12,24), datetime(2016,12,27))

Out: [datetime.datetime(2016, 12, 24, 17, 30),
 datetime.datetime(2016, 12, 26, 17, 30)]


rules_bad = rruleset()
rules_bad.rrule(rrulestr("RRULE:FREQ=DAILY"))
excl_date = datetime(2016,12,25,17,30,00)
rules_bad.exdate(excl_date)
rules_bad.between(datetime(2016,12,24), datetime(2016,12,27))

Out:
[datetime.datetime(2016, 12, 24, 11, 15, 42),
 datetime.datetime(2016, 12, 25, 11, 15, 42),
 datetime.datetime(2016, 12, 26, 11, 15, 42)]

The only difference between rules_good and rules_bad is whether I specify dtstart.

Activity

  1. andybak commented on Jun 15, 2016

    @andybak
    Author

    I've dug a bit deeper and it's somehow related to the time part of the datetime. If dtstart isn't supplied it defaults to datetime.now(). If I substitute various other times of day for the 05:30 in my example code I get various results that will probably make sense to someone else!

    I've been assuming I can ignore the time parts of datetime but there's an interaction that I can't fathom.

    What's the correct way to use rrules if one doesn't care about times - only days?

  2. pganssle commented on Jun 15, 2016

    @pganssle
    Member

    @andybak I think it's probably a matter of taste what the "correct" way to do it is. I'd personally prefer something like this:

    from dateutil.rrule import rrule, DAILY
    from datetime import datetime
    print(list(rrule(freq=DAILY, dtstart=datetime.today().date(), count=3)))

    But if you don't want to mess around with setting dtstart, you can do the same thing by setting the byhour, byminute and bysecond parameters:

    print(list(rrule(freq=DAILY, byhour=0, byminute=0, bysecond=0, count=3)))
  3. added this to the wontfix milestone on Jun 15, 2016
  4. pganssle commented on Jun 15, 2016

    @pganssle
    Member

    Also, not sure what the unfathomable interactions are about dates and times. If you don't specify anything to do with the time aspects, it will use whatever the time portion of dtstart is. For exrules, the date will only be excluded from the rruleset if it's an exact match, so if you create an exdate of datetime(2016, 12, 27, 17, 30), it won't match a daily recurrence that occurs at 11:42.

    If you don't care about the time aspects at all, then you may want to just create a helper function like this:

    from datetime import datetime, time
    def to_midnight(dt):
        return datetime.combine(dt, time(0, 0))

    Then you can either add that in as a wrapper in an rruleset subclass or just apply it to anything you're going to pass to rdate or exdate, and apply it to dtstart as well.

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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions