Repository navigation
Description
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:
- Support
dateobjects as if they weredatetimeobjects at midnight. Automatically combine alldateobjects withtime(0, 0)in the constructor ofrdateandexdate. - Throw a
TypeErrorin therdateand/orexdateconstructors.
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
zorun commentedon May 5, 2017
I had this issue while parsing real ICS data. To reproduce (this is very similar to your example):
gives:
zorun commentedon May 5, 2017
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.
Workaround RRULE cases using dates (instead of datetimes)
jeinarsson commentedon Jul 8, 2018
Hi, I also just ran into this issue. I work around by combining any
datewith atime(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 aredate. Intuitively I expected those to returndate. I suppose changing that behavior would be way to much of a breaking change, but is it sound in principle?lissizza commentedon Jan 23, 2019
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.
flyinGaijin commentedon Feb 19, 2020
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 !
duduklein commentedon Mar 24, 2020
I just came across this issue. What is the expected behavior if a
rulesetspecifies a certainrulewithdatetime dtstartand we haveexdatewith adateobject?I would expect that all occurrences that fall on that
exdateshould be excluded from the output, regardless the exact time from thedtstart.Solution 1, proposed above to combine
time(0,0)would probably not only work for exclusions ifdtstartalso havetime(0,0)Wouldn't a comparison with the date itself be more appropriate?pganssle commentedon Mar 24, 2020
@duduklein That's a good point, I tend to agree that it would make sense that specifying an
exdateas adateshould exclude anydatetimesduring thatdate, but I can see no mention of this in RFC 5545, nor any examples whereDATE-TIMEandDATEvalues 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.duduklein commentedon Mar 25, 2020
I could not find any example in the in RFC 5545 either. it does not seem to specify the behavior of
datetypesEXDATE, nor it gives any examples. It only specifies thatEXDATEcan be adatetimeor adateobjectIn 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?