UTC "timezone" causing brain explosions

Skip Montanaro skip at pobox.com
Wed Jan 29 16:52:05 EST 2014


Following up on my earlier note about UTC v. GMT, I am having some
trouble grokking attempts to convert a datetime into UTC. Consider
these three values:

>>> import pytz
>>> UTC = pytz.timezone("UTC")
>>> UTC
<UTC>
>>> LOCAL_TZ = pytz.timezone("America/Chicago")
>>> LOCAL_TZ
<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2014, 1, 29, 15, 39, 35, 263666)

All well and good, right? The variable "now" is a naive datetime
object. I happen to be sitting in a chair in the city of Chicago, so
let's call it what it is, a datetime in the America/Chicago timezone:

>>> s = LOCAL_TZ.localize(now)
>>> s
datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo=<DstTzInfo
'America/Chicago' CST-1 day, 18:00:00 STD>)

That looks good to me. Now, let's normalize it to UTC:

>>> t = UTC.normalize(s)
>>> t
datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo=<UTC>)
>>> t.hour
15

WTF? Why isn't the t.hour == 21?

Okay, let's see what GMT does for us:

>>> GMT = pytz.timezone("GMT")
>>> u = GMT.normalize(s)
>>> u
datetime.datetime(2014, 1, 29, 21, 39, 35, 263666, tzinfo=<StaticTzInfo 'GMT'>)
>>> u.hour
21

That looks correct, but I don't understand why I don't get hour==21
out of the UTC.normalize call. It's like it's a no-op.

Skip



More information about the Python-list mailing list