Normalize datetime object

I'm doing some timing magic and trying to figure out why the clock is a little off.

3pm Central on June 3, 2014 UTC:

>>> chicago = pytz.timezone('US/Central')
>>> chicago.localize(datetime(2014,6,3,15,0,0)).astimezone(pytz.utc)
datetime.datetime(2014, 6, 3, 20, 0, tzinfo=<UTC>)

      

3pm Central on Dec 2, 2014 UTC:

>>> chicago.localize(datetime(2014,12,2,15,0,0)).astimezone(pytz.utc)
datetime.datetime(2014, 12, 2, 21, 0, tzinfo=<UTC>)

      

The hour in the second example is 21, not 20 in the first example. I thought the date was not normalized, so I tried this:

>>> chicago.normalize(chicago.localize(datetime(2014,12,2,15,0,0))).astimezone(pytz.utc)
datetime.datetime(2014, 12, 2, 21, 0, tzinfo=<UTC>)

      

It's all the same 21. What's going on here?

+3


source to share


1 answer


Summer time.

In the first case (June 3), Chicago is under central summer time. The UTC offset is five hours.

>>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
>>> clt=chicago.localize(datetime(2014,6,3,15,0,0))
>>> clt.strftime(fmt)
'2014-06-03 15:00:00 CDT-0500'
                     ^^^^^^^^

      



In the second case (December 2), Chicago is in Central Standard Time. The UTC offset is six hours.

>>> clt=chicago.localize(datetime(2014,12,2,15,0,0))
>>> clt.strftime(fmt)
'2014-12-02 15:00:00 CST-0600'
                     ^^^^^^^^

      

Your call normalize()

is not helping here because you are not doing date or time arithmetic at local times crossing DST boundaries.

+1


source







All Articles