Datetime timezone trouble (was: Matplotlib X-axis timezone trouble)
Peter Pearson
pkpearson at nowhere.invalid
Wed Jul 1 15:09:03 EDT 2015
On Wed, 1 Jul 2015 10:55:02 -0600, Ian Kelly <ian.g.kelly at 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.
More information about the Python-list
mailing list