Missing something about timezones
Peter Pearson
pkpearson at nowhere.invalid
Tue Mar 15 20:12:28 EDT 2016
On Mon, 14 Mar 2016 10:19:23 -0500, Skip Montanaro wrote:
> Is this correct (today, with Daylight Savings in effect)?
>
>>>> import pytz
>>>> i.timezone
> 'America/Chicago'
>>>> pytz.timezone(i.timezone)
><DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>
>>>> ot
> datetime.datetime(2016, 3, 14, 9, 30, tzinfo=<DstTzInfo
> 'America/New_York' EDT-1 day, 20:00:00 DST>)
>>>> ot.tzinfo
><DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>
I've stubbed my toe many times on timezones and DST. Here are
some notes I've accumulated:
* datetime.datetime(..., tzinfo=tz) doesn't work right for timezones
that involve daylight-saving time, but nobody gets around to fixing
it because it's written in C; and
* knowing this, the creators of pytz thoughtfully furnished a
workaround, in the form of the "localize" method of a pytz.timezone
object:
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print(loc_dt.strftime(fmt))
2002-10-27 06:00:00 EST-0500
$ cat temp2.py
from datetime import datetime
from pytz import timezone
print(str(datetime(2015, 1, 31, 12, tzinfo=timezone("US/Pacific"))))
print(str(datetime(2015, 7, 31, 12, tzinfo=timezone("US/Pacific"))))
print(str(timezone("US/Pacific").localize(datetime(2015, 1, 31, 12))))
print(str(timezone("US/Pacific").localize(datetime(2015, 7, 31, 12))))
$ python temp2.py
2015-01-31 12:00:00-08:00
2015-07-31 12:00:00-08:00 <- wrong
2015-01-31 12:00:00-08:00
2015-07-31 12:00:00-07:00 <- right
$
Good luck!
--
To email me, substitute nowhere->runbox, invalid->com.
More information about the Python-list
mailing list