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


Groups > comp.lang.python > #93383

Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble)

From Peter Pearson <pkpearson@nowhere.invalid>
Newsgroups comp.lang.python
Subject Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble)
Date 2015-07-01 19:09 +0000
Message-ID <cvis6fFpbnqU1@mid.individual.net> (permalink)
References <cve7ppFk1viU1@mid.individual.net> <cvhdcpFdnliU1@mid.individual.net> <mailman.207.1435734941.3674.python-list@python.org> <cvij86Fnb08U1@mid.individual.net> <mailman.222.1435769750.3674.python-list@python.org>

Show all headers | View raw


On Wed, 1 Jul 2015 10:55:02 -0600, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Wed, Jul 1, 2015 at 10:36 AM, Peter Pearson [wrote]:
[snip]
>> Here's a very simple demonstration that either something is wrong
>> or I don't understand how datetime and tzinfo are supposed to work:
>>
>> $ python
>> Python 2.7.3 (default, Mar 13 2014, 11:03:55)
>>>>> from pytz import timezone
>>>>> from datetime import datetime
>>>>> pacific = timezone("US/Pacific")
>>>>> print(datetime(2014, 7, 7, 12, tzinfo=pacific))
>> 2014-07-07 12:00:00-08:00
>>>>> print(datetime(2014, 1, 7, 12, tzinfo=pacific))
>> 2014-01-07 12:00:00-08:00
>>>>>
>>
>> The "-08:00" is appropriate in the second (January) case, but the
>> first case is in July, and should have "-07:00".
>
> Use this instead:
>
>>>> print(pacific.localize(datetime(2014, 7, 7, 12)))
> 2014-07-07 12:00:00-07:00
>
> See http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

Excellent.  Thank you.

To summarize, for any similarly afflicted person who finds this thread:

 - http://pytz.sourceforge.net/#localized-times-and-date-arithmetic says:

    Unfortunately using the tzinfo argument of the standard datetime
    constructors ‘’does not work’’ with pytz for many timezones.

    It is safe for timezones without daylight saving transitions though,
    such as UTC:

 - PEP 0431 says (approximately) that datetime doesn't work as well
   as we might like with DST, but will be slow to change.

 - The "localize" function is pytz's way of working around this
   shortcoming of datetime's


-- 
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

Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-06-30 00:56 +0000
  Re: Matplotlib X-axis timezone trouble Laura Creighton <lac@openend.se> - 2015-06-30 04:03 +0200
    Re: Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-06-30 04:57 +0000
  Re: Matplotlib X-axis timezone trouble Chris Angelico <rosuav@gmail.com> - 2015-06-30 12:11 +1000
    Re: Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-06-30 04:49 +0000
      Re: Matplotlib X-axis timezone trouble Chris Angelico <rosuav@gmail.com> - 2015-06-30 17:01 +1000
        Re: Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-06-30 16:42 +0000
          Re: Matplotlib X-axis timezone trouble Chris Angelico <rosuav@gmail.com> - 2015-07-01 02:55 +1000
  Re: Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-07-01 05:50 +0000
    Re: Matplotlib X-axis timezone trouble Chris Angelico <rosuav@gmail.com> - 2015-07-01 17:15 +1000
      Datetime timezone trouble (was: Matplotlib X-axis timezone trouble) Peter Pearson <pkpearson@nowhere.invalid> - 2015-07-01 16:36 +0000
        Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble) Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-01 10:55 -0600
          Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble) Peter Pearson <pkpearson@nowhere.invalid> - 2015-07-01 19:09 +0000
        Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble) Chris Angelico <rosuav@gmail.com> - 2015-07-02 03:08 +1000
  Re: Matplotlib X-axis timezone trouble Akira Li <4kir4.1i@gmail.com> - 2015-07-04 07:29 +0300
    Re: Matplotlib X-axis timezone trouble [SOLVED] Peter Pearson <pkpearson@nowhere.invalid> - 2015-07-04 17:37 +0000
  Re: Matplotlib X-axis timezone trouble Tony the Tiger <tony@tiger.invalid> - 2015-07-09 19:50 +0000
    Re: Matplotlib X-axis timezone trouble Peter Pearson <pkpearson@nowhere.invalid> - 2015-07-10 16:15 +0000

csiph-web