Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64952 > unrolled thread

UTC "timezone" causing brain explosions

Started bySkip Montanaro <skip@pobox.com>
First post2014-01-29 15:52 -0600
Last post2014-01-29 15:52 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  UTC "timezone" causing brain explosions Skip Montanaro <skip@pobox.com> - 2014-01-29 15:52 -0600

#64952 — UTC "timezone" causing brain explosions

FromSkip Montanaro <skip@pobox.com>
Date2014-01-29 15:52 -0600
SubjectUTC "timezone" causing brain explosions
Message-ID<mailman.6098.1391032788.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web