myusuf3 / delorean

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

Don't store timezone as tzinfo.tzname #36

Closed alanhamlett closed 10 years ago

alanhamlett commented 11 years ago

This causes problems when using last_* methods off the Delorean, for example:

>>> import pytz
>>> from delorean import Delorean
>>> from datetime import datetime, timedelta
>>> pdt = pytz.timezone('America/Los_Angeles')
>>> dt = datetime.utcnow()
>>> dt = pdt.normalize(pytz.utc.localize(dt))
>>> delorean = Delorean(datetime=dt)
>>> delorean.last_week()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/blue/git/wakatime/venv/local/lib/python2.7/site-packages/delorean/dates.py", line 267, in _shift_date
    return Delorean(datetime=dt.replace(tzinfo=None), timezone=self._tz)
  File "/home/blue/git/wakatime/venv/local/lib/python2.7/site-packages/delorean/dates.py", line 198, in __init__
    self._dt = localize(datetime, timezone)
  File "/home/blue/git/wakatime/venv/local/lib/python2.7/site-packages/delorean/dates.py", line 141, in localize
    tz = timezone(tz)
  File "/home/blue/git/wakatime/venv/local/lib/python2.7/site-packages/pytz/__init__.py", line 182, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'PDT'
swistakm commented 11 years ago

This also happens when you create Delorean from others Delorean's datetime if timezone is different than UTC:

>>> import delorean
>>> d1 = delorean.Delorean(timezone='America/Chicago')
>>> d2 = delorean.Delorean(d1.datetime)
>>> d1.next_day(1)
Delorean(datetime=2013-10-24 05:28:02.624143-05:00, timezone=America/Chicago)
>>> d2.next_day(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/swistakm/dev/env-tebe-packets/local/lib/python2.7/site-packages/delorean/dates.py", line 311, in next_day
    return Delorean(datetime=dt, timezone=self._tz)
  File "/home/swistakm/dev/env-tebe-packets/local/lib/python2.7/site-packages/delorean/dates.py", line 198, in __init__
    self._dt = localize(datetime, timezone)
  File "/home/swistakm/dev/env-tebe-packets/local/lib/python2.7/site-packages/delorean/dates.py", line 141, in localize
    tz = timezone(tz)
  File "/home/swistakm/dev/env-tebe-packets/local/lib/python2.7/site-packages/pytz/__init__.py", line 180, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'CDT'
>>>