Skip to content Skip to sidebar Skip to footer

Pytz.timezone('asia/chongqing') Is Behaving Strangely

I am writing some Python (Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32, Windows 7) code which needs to handle timezones. For this I am using t

Solution 1:

You need to use the .localize() method to put a datetime in the correct timezone, otherwise a historical offset is chosen by mistake:

ChengduTZ = pytz.timezone('Asia/Chongqing')
MidnightInChengdu = ChengduTZ.localize(datetime.datetime(2013, 6, 5, 0, 0, 0, 0))
MidnightInNewYork = NewYorkTZ.localize(datetime.datetime(2013, 6, 5, 0, 0, 0, 0))

See the pytz documenation:

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

With this change, the output becomes:

When it's midnight in Chengdu it's:
2013-06-0500:00:00+08:002013-06-0417:00:00+01:002013-06-0418:00:00+02:002013-06-0412:00:00-04:00When it's midnight in New York it's:
2013-06-0500:00:00-04:002013-06-0505:00:00+01:002013-06-0506:00:00+02:002013-06-0512:00:00+08:00

Note that the New York offsets were also incorrect.

Post a Comment for "Pytz.timezone('asia/chongqing') Is Behaving Strangely"