django-recurrence
django-recurrence copied to clipboard
deserialize offset-naive and offset-aware datetimes
Should https://github.com/django-recurrence/django-recurrence/blob/master/recurrence/base.py#L992 not retrun the datetime
based on settings.USE_TZ
? I'm working exclusively with offset-aware datetimes and when I call recurrences.after(dt, inc=inc)
I run into TypeError: can't compare offset-naive and offset-aware datetimes
.
I propose replacing the following return
line in base.py
def deserialize_tz()
return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second`)
with
if not settings.USE_TZ:
tzinfo = None
return datetime.datetime(
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, tzinfo=tzinfo)
The current implementation returns a offset-naive datetime after serializing/deserializing.
python shell
from django.utils import timezone
import recurrence
today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
dtstart = timezone.now().replace(hour=23, minute=59, second=59, microsecond=0)
rule = recurrence.Rule(recurrence.DAILY, count=1)
schedule = recurrence.Recurrence(dtstart=dtstart, rrules=(rule,))
[occurrence for occurrence in schedule.occurrences()] # [datetime.datetime(2017, 11, 17, 23, 59, 59, tzinfo=<UTC>)]
serialized = recurrence.serialize(schedule)
deserialized = recurrence.deserialize(serialized)
[occurrence for occurrence in deserialized.occurrences()] # [datetime.datetime(2017, 11, 17, 23, 59, 59)]
schedule.after(today, inc=True) # datetime.datetime(2017, 11, 17, 23, 59, 59, tzinfo=<UTC>)
deserialized.after(today, inc=True) # TypeError: can't compare offset-naive and offset-aware datetimes
The expected behavior is a offset-aware datetime after serializing/deserializing.
from django.utils import timezone
import recurrence
today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
dtstart = timezone.now().replace(hour=23, minute=59, second=59, microsecond=0)
rule = recurrence.Rule(recurrence.DAILY, count=1)
schedule = recurrence.Recurrence(dtstart=dtstart, rrules=(rule,))
[occurrence for occurrence in schedule.occurrences()] # [datetime.datetime(2017, 11, 17, 23, 59, 59, tzinfo=<UTC>)]
serialized = recurrence.serialize(schedule)
deserialized = recurrence.deserialize(serialized)
[occurrence for occurrence in deserialized.occurrences()] # [datetime.datetime(2017, 11, 17, 23, 59, 59)]
schedule.after(today, inc=True) # datetime.datetime(2017, 11, 17, 23, 59, 59, tzinfo=<UTC>)
deserialized.after(today, inc=True) # datetime.datetime(2017, 11, 17, 23, 59, 59, tzinfo=<UTC>)
#102