Missing something about timezones

Carl Meyer carl at oddbird.net
Mon Mar 14 12:12:24 EDT 2016


Hi Skip,

On 03/14/2016 09:32 AM, Skip Montanaro wrote:
> On Mon, Mar 14, 2016 at 10:26 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> Why should it? You only asked pytz for the Chicago timezone. You
>> didn't ask for it relative to any specific time.
> 
> Thanks. I thought using America/Chicago was supposed to automagically
> take into account transitions into and out of Daylight Savings. Is
> there some way to get that?

Yes, pytz can handle DST correctly automatically when you give it
'America/Chicago', but until you apply that timezone to a particular
datetime, there is no DST to handle. There is no implicit assumption of
"today" when you do `pytz.timezone('America/Chicago').

If you apply the timezone to a particular datetime, you'll see that it
does reflect DST correctly:

>>> import datetime, pytz
>>> tz = pytz.timezone('America/Chicago')
>>> tz
<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD>
>>> import datetime
>>> dt = datetime.datetime.now()
>>> dtl = tz.localize(dt)
>>> dtl
datetime.datetime(2016, 3, 14, 10, 11, 13, 514375, tzinfo=<DstTzInfo
'America/Chicago' CDT-1 day, 19:00:00 DST>)

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20160314/0933b3fb/attachment.sig>


More information about the Python-list mailing list