What's wrong with my conversion from local time to UTC

According to timeanddate.com, Chicago is currently 5 hours behind UTC. However, my Python application looks differently:

import datetime
import pytz

    local_tz = pytz.timezone('America/Chicago')
    local_time = datetime.datetime(2015, 8, 6, 0, 0, tzinfo=local_tz)
    utc_time = local_time.astimezone(pytz.utc)
    print(local_time)
    print(utc_time)

2015-08-06 00:00:00-05:51
2015-08-06 05:51:00+00:00

      

I get the same results with both "America / Chicago" and "US / Central". Why is the offset -05: 51 instead of -05: 00?

+3


source to share


1 answer


pytz

timezone objects must be initialized with a specific time before being used, and creating a parameter datetime

with a parameter tzinfo=

does not allow this. You must use an localize

object method pytz

to add the timezone to the datetime

.



>>> local_tz = pytz.timezone('America/Chicago')
>>> local_time = local_tz.localize(datetime.datetime(2015, 8, 6, 0, 0))
>>> print local_time
2015-08-06 00:00:00-05:00
>>> utc_time = local_time.astimezone(pytz.utc)
>>> print utc_time
2015-08-06 05:00:00+00:00

      

+2


source







All Articles