myusuf3 / delorean

Delorean: Time Travel Made Easy
http://delorean.rtfd.org/
MIT License
1.84k stars 128 forks source link

zoneinfo.ZoneInfo AttributeError while parsing within the Django context #110

Open mmulich opened 3 years ago

mmulich commented 3 years ago

I'm running into an error when using Delorean within a Django app context. This only happens when the Django app is loaded. For example, in the interpreter setup by django:

$ python manage.py shell
Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> t = '2020-11-30T05:17:05.000000Z'
>>> import delorean
>>> delorean.parse(t).datetime

This results in:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../lib/python3.9/site-packages/delorean/interface.py", line 87, in parse
    do = Delorean(dt, timezone=tz)
  File ".../lib/python3.9/site-packages/delorean/dates.py", line 202, in __init__
    self._dt = localize(datetime, self._tzinfo)
  File ".../lib/python3.9/site-packages/delorean/dates.py", line 157, in localize
    return tz.localize(dt)
AttributeError: 'zoneinfo.ZoneInfo' object has no attribute 'localize'

The same procedure done outside of django results in a successful parsing of the datetime.

$ python
Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> t = '2020-11-30T05:17:05.000000Z'
>>> import delorean
>>> delorean.parse(t).datetime
datetime.datetime(2020, 11, 30, 5, 17, 5, tzinfo=<UTC>)

Note, it's the same environment, just not within the django shell setup.

AFAICT, the only difference is that Django is using a zoneinfo timezone instance, while pure python is finding any using a pytz timezone instance.

wickeym commented 2 years ago

There was an api change in tzinfo starting with version 3 it no longer returns a pytz object but rather a zoneinfo object. Delorean will either need to update to handle the new object or limit tzlocal to <= 2.1

I just ran into this issue. Friday afternoon is always a great time to debug issues 😄