Ernesto
Ernesto

Reputation: 185

Time localized wrongly in Python

Using python Im trying to localize a datetime value to timezone "America/Chicago" which is -06:00 currently. I get the timezone the following way:

>>> import pytz
>>> tz = pytz.timezone("America/Chicago")
<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>

when I localize a date:

>>> my_date = tz.localize(datetime.now())
datetime.datetime(2016, 9, 24, 17, 4, 43, 439824, tzinfo=<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>)

Notice it is the wrong timezone after localize:

<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>

And later when i ask for the offset, you can see it is confirmed it has the wrong offset:

>>> my_date.strftime("%z")
'-0500'

Exactly the same happend if I use astimezone instead:

>>>my_date
datetime.datetime(2016, 9, 24, 22, 15, 1, 620364, tzinfo=<UTC>)

>>>my_date.astimezone(tz)
datetime.datetime(2016, 9, 24, 17, 15, 1, 620364, tzinfo=<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>)

Upvotes: 1

Views: 106

Answers (1)

s7vr
s7vr

Reputation: 75934

Btw Chicago is observing DST now. So -05.00 is the right offset. Pytz timezone by default has the standard time, but when localized can account for day light saving based on the date(as in your case).

Upvotes: 0

Related Questions