Sebabouche Sebabouche - 14 days ago 5
Python Question

Python datetime and tzinfo objects (changing minutes instead of hours)

I'm trying to apply a tzinfo to a datetime object.

In [1]: from datetime import datetime
In [2]: import pytz

In [3]: london = pytz.timezone("Europe/London")
In [4]: london
Out[5]: <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>

In [6]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, london)
In [7]: localized_date_object
Out[8]: datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>)

In [9]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [10]: utc_date_object
Out[11]: datetime.datetime(2016, 1, 1, 11, 31, 0, 5000, tzinfo=<UTC>)

In [16]: paris = pytz.timezone("Europe/Paris")
In [17]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, paris)
In [18]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [19]: utc_date_object
Out[19]: datetime.datetime(2016, 1, 1, 11, 21, 0, 5000, tzinfo=<UTC>)


As you can see, it's applying delta to minutes instead of hours.

Can someone explain me what I'm doing wrong here.

gzc gzc
Answer

The timezone Europe/London is after UTC 1 minute. The timezone Europe/Paris is before UTC by 9 minutes. London and Paris is close to Greenwich geographically, so timezone offset is small.

If you try

pytz.timezone("Asia/Shanghai")

, you will see hours change.