[Python-Dev] Status on PEP-431 Timezones

Lennart Regebro regebro at gmail.com
Wed Apr 15 10:09:05 CEST 2015


Yeah, I just realized this. As long as you use timedelta, the
difference is of course not one day, but 24 hours. That solves the
problem, but it is surprising in other ways.

In US/Eastern datetime.datetime(2002, 10, 27, 1, 0) -
datetime.timedelta(1) needs to become datetime.datetime(2002, 10, 26,
2, 0)
(Note the hour change)

I was thinking in calendrial arithmetic, which the datetime module
doesn't need to care about.


On Wed, Apr 15, 2015 at 12:59 AM, Stuart Bishop <stuart at stuartbishop.net> wrote:
> On 14 April 2015 at 21:04, Lennart Regebro <regebro at gmail.com> wrote:
>> OK, so I realized another thing today, and that is that arithmetic
>> doesn't necessarily round trip.
>>
>> For example, 2002-10-27 01:00 US/Eastern comes both in DST and STD.
>>
>> But 2002-10-27 01:00 US/Eastern STD minus two days is 2002-10-25 01:00
>> US/Eastern DST
>> However, 2002-10-25 01:00 US/Eastern DST plus two days is 2002-10-27
>> 01:00 US/Eastern, but it is ambiguous if you want DST or not DST.
>> And you can't pass in a is_dst flag to __add__, so the arithmatic must
>> just pick one, and the sensible one is to keep to the same DST.
>
>>>> import pytz
>>>> from datetime import datetime, timedelta
>>>> tz = pytz.timezone('US/Eastern')
>>>> dt = tz.localize(datetime(2002, 10, 27, 1, 0), is_dst=False)
>>>> dt2 = tz.normalize(dt - timedelta(days=2) + timedelta(days=2))
>>>> dt == dt2
> True
>>>>
>>>> tz.normalize(dt - timedelta(days=2))
> datetime.datetime(2002, 10, 25, 2, 0, tzinfo=<DstTzInfo 'US/Eastern'
> EDT-1 day, 20:00:00 DST>)
>>>> tz.normalize(tz.normalize(dt - timedelta(days=2)) + timedelta(days=2))
> datetime.datetime(2002, 10, 27, 1, 0, tzinfo=<DstTzInfo 'US/Eastern'
> EST-1 day, 19:00:00 STD>)
>
>
> 2002-10-27 01:00 US/Eastern is_dst=0 is after the DST transition
> (EST). Subtracting 48 hours from it crosses the DST boundary and
> should give you 2002-10-27 02:00 US/Eastern is_dst=1, prior to the DST
> transition (EDT). Adding 48 hours again goes past 2002-10-27 01:00
> EDT, crosses the DST boundary, and gives you back 2002-10-27 01:00
> EST.
>
>
> --
> Stuart Bishop <stuart at stuartbishop.net>
> http://www.stuartbishop.net/


More information about the Python-Dev mailing list