Message236326
I encountered a strange bug involving timezone aware datetime objects and timedelta objects.
The crux of the matter is that daylight savings time is considered a different timezone, and therefore the timezone of a datetime object is date dependent. However, adding a timedelta object to a datetime object seems to do the simple thing which is preserve the timezone object. The bug manifests itself whenever adding a timedelta object crosses a daylight savings time boundary.
The following code illustrates the problem. Note that the transition between PDT (Pacific daylight time) and PST (Pacific savings time) occurs on March 9th (ish)
>>> from datetime import datetime, timedelta
>>> import pytz
>>> tz = pytz.timezone('America/Los_Angeles')
>>> before = tz.localize(datetime(year=2015, month=3, day=8))
>>> before
datetime.datetime(2015, 3, 8, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
>>> # notice PST timezone
>>> after_right = tz.localize(datetime(year=2015, month=3, day=10))
>>> after_right
datetime.datetime(2015, 3, 10, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
>>> # notice PDT timezone
>>> after_wrong = before + timedelta(days=2)
>>> after_wrong
datetime.datetime(2015, 3, 10, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
>>> # when calculated this way, the timezone remains at PST |
|
Date |
User |
Action |
Args |
2015-02-20 19:46:14 | gilsho | set | recipients:
+ gilsho, eric.araujo, dstufft |
2015-02-20 19:46:14 | gilsho | set | messageid: <1424461574.53.0.989762969133.issue23494@psf.upfronthosting.co.za> |
2015-02-20 19:46:14 | gilsho | link | issue23494 messages |
2015-02-20 19:46:14 | gilsho | create | |
|