Skip to content

exdate and rdate fail if passed a date object, but dtstart does not. #275

Open

Description

@pganssle

Discovered this when creating an example for #274, it turns out that you can add a bare datetime.date object as an exdate or and rdate, but when attempting to actually use the rule set, it will throw a TypeError.

Example with exdate:

from dateutil.rrule import rrule, rruleset, DAILY
from datetime import datetime
from copy import deepcopy

rr = rrule(freq=DAILY, dtstart=datetime(2016, 4, 11, 17, 50).date(), interval=2)
ex_date = datetime(2016, 4, 15)

rs_ex = rruleset()
rs_ex.rrule(rr)
rs_ex_good = deepcopy(rs_ex)
rs_ex_good.exdate(ex_date)
print(rs_ex_good.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

rs_ex.exdate(ex_date.date())
print(rs_ex.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

Result:

[datetime.datetime(2016, 4, 17, 0, 0)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-ce61539b8c80> in <module>()
     14 
     15 rs_ex.exdate(ex_date.date())
---> 16 print(rs_ex.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

/usr/lib/python3.5/site-packages/dateutil/rrule.py in between(self, after, before, inc, count)
    306                     l.append(i)
    307         else:
--> 308             for i in gen:
    309                 if i >= before:
    310                     break

/usr/lib/python3.5/site-packages/dateutil/rrule.py in _iter(self)
   1376             ritem = rlist[0]
   1377             if not lastdt or lastdt != ritem.dt:
-> 1378                 while exlist and exlist[0] < ritem:
   1379                     exitem = exlist[0]
   1380                     advance_iterator(exitem)

/usr/lib/python3.5/site-packages/dateutil/rrule.py in __lt__(self, other)
   1313 
   1314         def __lt__(self, other):
-> 1315             return self.dt < other.dt
   1316 
   1317         def __gt__(self, other):

TypeError: can't compare datetime.datetime to datetime.date

And with rdate:

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

rr = rrule(freq=DAILY, dtstart=datetime(2016, 4, 11, 17, 50).date(), interval=2)
ex_date = datetime(2016, 4, 15, 12, 00)
r_date = datetime(2016, 4, 16, 13, 21)

rs_r = rruleset()
rs_r.rrule(rr)
rs_r_good = deepcopy(rs_r)
print(rs_r_good.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

rs_r.rdate(r_date.date())
print(rs_r.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

Result:

[datetime.datetime(2016, 4, 15, 0, 0), datetime.datetime(2016, 4, 17, 0, 0)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-8869ad467e3f> in <module>()
     12 
     13 rs_r.rdate(r_date.date())
---> 14 print(rs_r.between(datetime(2016, 4, 13), datetime(2016, 4, 18)))

/usr/lib/python3.5/site-packages/dateutil/rrule.py in between(self, after, before, inc, count)
    306                     l.append(i)
    307         else:
--> 308             for i in gen:
    309                 if i >= before:
    310                     break

/usr/lib/python3.5/site-packages/dateutil/rrule.py in _iter(self)
   1371         lastdt = None
   1372         total = 0
-> 1373         heapq.heapify(rlist)
   1374         heapq.heapify(exlist)
   1375         while rlist:

/usr/lib/python3.5/site-packages/dateutil/rrule.py in __lt__(self, other)
   1313 
   1314         def __lt__(self, other):
-> 1315             return self.dt < other.dt
   1316 
   1317         def __gt__(self, other):

TypeError: can't compare datetime.datetime to datetime.date

I think there are basically two options here:

  1. Support date objects as if they were datetime objects at midnight. Automatically combine all date objects with time(0, 0) in the constructor of rdate and exdate.
  2. Throw a TypeError in the rdate and/or exdate constructors.

I think I would prefer to go with 1. The one issue I could see with that approach would be that dates can't really have time zones, so if the other stuff in the rule is not timezone aware, we may have some ambiguous situations, but I don't think this is an issue since timezone-aware rrules aren't really supported anyway because there's too much ambiguous behavior, so this isn't a real issue (and it also would be an issue with adding any naive datetime as an rdate, not just a date).

Activity

  1. added this to the 2.6.0 milestone on Jun 15, 2016
  2. modified the milestones: Bugfix release, 2.6.0 on Nov 6, 2016
  3. zorun commented on May 5, 2017

    @zorun

    I had this issue while parsing real ICS data. To reproduce (this is very similar to your example):

    from dateutil.rrule import rrule, rruleset, YEARLY
    from datetime import datetime, date
    
    rr = rrule(freq=YEARLY, dtstart=date(2016, 1, 1), count=5)
    ex_date = date(2016, 1, 1)
    
    rs = rruleset()
    rs.rrule(rr)
    
    print(list(rs)) # working
    
    rs.exdate(ex_date)
    
    print("With exdate", list(rs)) # not working
    

    gives:

    [datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2018, 1, 1, 0, 0), datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2020, 1, 1, 0, 0)]
    Traceback (most recent call last):
      File "rrule.py", line 14, in <module>
        print("With exdate", list(rs))
      File "/usr/lib/python3.6/site-packages/dateutil/rrule.py", line 1373, in _iter
        while exlist and exlist[0] < ritem:
      File "/usr/lib/python3.6/site-packages/dateutil/rrule.py", line 1310, in __lt__
        return self.dt < other.dt
    TypeError: can't compare datetime.datetime to datetime.date
    
  4. zorun commented on May 5, 2017

    @zorun

    By the way, exdate and rdate are allowed to be dates without time, so solution 2. above would be incorrect:
    https://tools.ietf.org/html/rfc5545#section-3.8.5.1
    https://tools.ietf.org/html/rfc5545#section-3.8.5.2

    I think the cleanest approach would be to have two versions of the rrule machinery: one which works only with datetime, and one which works only with dates. That is, DTSTART, EXDATE and RDATE must all have the same type (either all datetimes, or all dates). I have no idea if this is conforming with RFC5545 though, since it is not very clear on this point.

  5. jeinarsson commented on Jul 8, 2018

    @jeinarsson

    Hi, I also just ran into this issue. I work around by combining any date with a time(0,0) as per pganssle's recommendation. With that I successfully parse a number of real world calendars (Google and Fastmail).

    When debugging I realized that iterating over a rrule always returns datetime, even if the DTSTART and all other ingredients are date. Intuitively I expected those to return date. I suppose changing that behavior would be way to much of a breaking change, but is it sound in principle?

  6. lissizza commented on Jan 23, 2019

    @lissizza

    I agree with jeinarsson, I need to work with dates and first thought rrule is ideal for my purposes, but I cannot understand how can I get dates without final back converting. It would be great to have such ability to choose the format or to get the same type what you have used on input.

  7. flyinGaijin commented on Feb 19, 2020

    @flyinGaijin

    The problem is still there, it is very confusing, very documentation lacking and one needs to either find this ticket or dig all the way to the rrules.py file to find out what is going on.
    If exdate() isn't capable of handling dates, it should at least throw an exception when called, no on iteration on the ruleset !

  8. duduklein commented on Mar 24, 2020

    @duduklein

    I just came across this issue. What is the expected behavior if a ruleset specifies a certain rule with datetime dtstart and we have exdate with a date object?

    I would expect that all occurrences that fall on that exdate should be excluded from the output, regardless the exact time from the dtstart.

    Solution 1, proposed above to combine time(0,0) would probably not only work for exclusions if dtstart also have time(0,0) Wouldn't a comparison with the date itself be more appropriate?

  9. pganssle commented on Mar 24, 2020

    @pganssle
    MemberAuthor

    @duduklein That's a good point, I tend to agree that it would make sense that specifying an exdate as a date should exclude any datetimes during that date, but I can see no mention of this in RFC 5545, nor any examples where DATE-TIME and DATE values are mixed in a single rule or rule set. I'm not sure what the intended behavior was. If anyone knows the RFC better and can give a ready determination please let me know.

  10. duduklein commented on Mar 25, 2020

    @duduklein

    I could not find any example in the in RFC 5545 either. it does not seem to specify the behavior of date types EXDATE, nor it gives any examples. It only specifies that EXDATE can be a datetime or a date object

    In the case where the RFC does not specify the behavior, is "any one" free to implement whatever he thinks it's not reasonable/logic or no one should implement other behaviors? What are the community standards?

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