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


Groups > comp.lang.python > #64955

Re: UTC "timezone" causing brain explosions

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'url:sourceforge': 0.03; '21,': 0.07; 'correct,': 0.09; 'to:addr:pobox.com': 0.09; 'to:addr:skip': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; '39,': 0.16; 'timezones': 0.16; 'utc': 0.16; 'utc.': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; '"you': 0.24; 'instead.': 0.24; 'skip': 0.24; 'earlier': 0.24; 'cheers,': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; '15,': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'skip:p 30': 0.29; 'related': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '>>>>': 0.31; 'object.': 0.31; 'call.': 0.33; 'trouble': 0.34; 'skip:d 20': 0.34; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'chair': 0.36; 'right?': 0.36; "didn't": 0.36; 'pm,': 0.38; 'subject:" ': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'happen': 0.63; 'city': 0.66; 'side': 0.67; 'believe': 0.68; 'chicago,': 0.68; 'savings': 0.81; 'sender:addr:chris': 0.84; 'good,': 0.91; 'sitting': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=yEzfK/dRdK/nkNGswtgbQGIqiXBdsEsK5p13Yj6HhbU=; b=FrMiyrEsQNvd4WeE7E+WRTZ0zcDjz0H/aqEQjgYFVjveE47AGDefDq4Oabu6jEMdeX TXLqaW+oH7bfC1tRhbRn3lgGSNwhgDySBsYjfpNjdIPZyf5yjOtrms+gt5jy4iTDhOM7 BbPrsy5Ammp3Or3SQ0Md0nvlD1cUL3aQagiZs=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=yEzfK/dRdK/nkNGswtgbQGIqiXBdsEsK5p13Yj6HhbU=; b=dc0eXcxMf+2XcVX94DqNfm1EcVV3GL8MzG3O/KFqVUm8jO5HgdcfK6tE0tKhnyszZP CpELwRZOHs6khD4eEmigMBTdqglusZU/6PW5kjy7y9QmrThnSH+Cj+epJF+No5Yjwv0L nzFIlCzQBw8HctxVNvgSA1Vtxjwxq/fgdgVfchErwJbqSWI8/G+PitFB6fotUDAbpdeq cvte0ZPLxB2PyfyrBCRKLQ8fSFJOtAXazO+7w7hy12DJmitMC9IRGnJopZvfQbvFuBE9 F4xHmFRQLnhzZHQ54sQN8ewSfGIxoSNfnzbEMfINbs2jQTI2X1muDWLuUgfvVMggsktw 7eeQ==
X-Gm-Message-State ALoCoQm5uKuUYWjREMz+sYCU9d1DvyDSIPkK9npDUad196B1BYpufhpyKlLL0Q7ZZVlx9Mg+PEUH
MIME-Version 1.0
X-Received by 10.60.118.168 with SMTP id kn8mr8309183oeb.43.1391035969345; Wed, 29 Jan 2014 14:52:49 -0800 (PST)
Sender chris@rebertia.com
In-Reply-To <CANc-5UyOGNWdug-JvXe_RMn9V7BCGFyZ0NwtSjFMFaW1f_wfhw@mail.gmail.com>
References <CANc-5UyOGNWdug-JvXe_RMn9V7BCGFyZ0NwtSjFMFaW1f_wfhw@mail.gmail.com>
Date Wed, 29 Jan 2014 14:52:49 -0800
X-Google-Sender-Auth XJp9HyVGakvn3Sq58eI3S7FOlIo
Subject Re: UTC "timezone" causing brain explosions
From Chris Rebert <clp2@rebertia.com>
To Skip Montanaro <skip@pobox.com>
Content-Type text/plain; charset=UTF-8
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6101.1391035971.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391035971 news.xs4all.nl 2966 [2001:888:2000:d::a6]:50913
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64955

Show key headers only | View raw


On Wed, Jan 29, 2014 at 1:52 PM, Skip Montanaro <skip@pobox.com> wrote:
> Following up on my earlier note about UTC v. GMT, I am having some
> trouble grokking attempts to convert a datetime into UTC. Consider
> these three values:
>
>>>> import pytz
>>>> UTC = pytz.timezone("UTC")
>>>> LOCAL_TZ = pytz.timezone("America/Chicago")
>>>> LOCAL_TZ
> <DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>
>>>> now = datetime.datetime.now()
>>>> now
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666)
>
> All well and good, right? The variable "now" is a naive datetime
> object. I happen to be sitting in a chair in the city of Chicago, so
> let's call it what it is, a datetime in the America/Chicago timezone:
>
>>>> s = LOCAL_TZ.localize(now)
>>>> s
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo=<DstTzInfo
> 'America/Chicago' CST-1 day, 18:00:00 STD>)
>
> That looks good to me. Now, let's normalize it to UTC:

I don't think .normalize() doesn't do what you think it does; it's
related to timezones with DST.
I believe you want datetime.astimezone() instead.

>>>> t = UTC.normalize(s)
>>>> t
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo=<UTC>)
>>>> t.hour
> 15
>
> WTF? Why isn't the t.hour == 21?

Because you didn't actually perform a proper timezone conversion:
>>> t = s.astimezone(UTC)
>>> t
datetime.datetime(2014, 1, 29, 21, 39, 35, 263666, tzinfo=<UTC>)
>>> t.hour == 21
True

<snip>
> That looks correct, but I don't understand why I don't get hour==21
> out of the UTC.normalize call. It's like it's a no-op.

It is indeed a no-op:
"You can take shortcuts when dealing with the UTC side of timezone
conversions. normalize() and localize() are not really necessary when
there are no daylight savings time transitions to deal with."
    -- http://pytz.sourceforge.net

Cheers,
Chris

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


Thread

Re: UTC "timezone" causing brain explosions Chris Rebert <clp2@rebertia.com> - 2014-01-29 14:52 -0800

csiph-web