On 14 April 2015 at 21:04, Lennart Regebro <rege...@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 <stu...@stuartbishop.net> http://www.stuartbishop.net/ _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com