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


Groups > comp.lang.python > #104971

Re: Missing something about timezones

From Peter Pearson <pkpearson@nowhere.invalid>
Newsgroups comp.lang.python
Subject Re: Missing something about timezones
Date 2016-03-16 00:12 +0000
Message-ID <dkrmncFhk2uU1@mid.individual.net> (permalink)
References <mailman.103.1457968773.12893.python-list@python.org>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Missing something about timezones Skip Montanaro <skip.montanaro@gmail.com> - 2016-03-14 10:19 -0500
  Re: Missing something about timezones Peter Pearson <pkpearson@nowhere.invalid> - 2016-03-16 00:12 +0000
  Re: Missing something about timezones wxjmfauth@gmail.com - 2016-03-16 02:29 -0700

csiph-web