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


Groups > comp.lang.python > #104834 > unrolled thread

Re: Missing something about timezones

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2016-03-14 10:20 -0600
Last post2016-03-14 10:20 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Missing something about timezones Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-14 10:20 -0600

#104834 — Re: Missing something about timezones

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-03-14 10:20 -0600
SubjectRe: Missing something about timezones
Message-ID<mailman.113.1457972497.12893.python-list@python.org>
On Mon, Mar 14, 2016 at 9:32 AM, Skip Montanaro
<skip.montanaro@gmail.com> wrote:
> On Mon, Mar 14, 2016 at 10:26 AM, Ian Kelly <ian.g.kelly@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?

Use the timezone to localize a specific time, and then if it's
specifically the timezone that you're interested in, you can get it
from the datetime.

>>> from datetime import datetime
>>> import pytz
>>> tz = pytz.timezone('America/Chicago')
>>> tz
<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>
>>> dt = datetime.now()
>>> dt
datetime.datetime(2016, 3, 14, 10, 16, 40, 404608)
>>> loc_dt = tz.localize(dt)
>>> loc_dt
datetime.datetime(2016, 3, 14, 10, 16, 40, 404608, tzinfo=<DstTzInfo
'America/Chicago' CDT-1 day, 19:00:00 DST>)

Note that the above example is technically incorrect since
datetime.now() returns local time and I'm not in the Chicago timezone,
but it demonstrates the process.

Also, if you haven't already read the pytz documentation, you should.
http://pythonhosted.org/pytz/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web