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-05 00:00:00+08:00
2013-06-04 17:00:00+01:00
2013-06-04 18:00:00+02:00
2013-06-04 12:00:00-04:00

When it's midnight in New York it's:
2013-06-05 00:00:00-04:00
2013-06-05 05:00:00+01:00
2013-06-05 06:00:00+02:00
2013-06-05 12:00:00+08:00

Note that the New York offsets were also incorrect.


Post a Comment for "Pytz.timezone('Asia/Chongqing') Is Behaving Strangely"