python 3.x - Is this intended behavior or a bug in datetime timedelta? -
from datetime import datetime timedelta import pytz ppt = pytz.timezone('us/pacific') first = ppt.localize(datetime(2013, 3, 10, 0, 0, 0)) first+=timedelta(hours=2) first
returns datetime.datetime(2013, 3, 10, 2, 0, tzinfo=<dsttzinfo 'us/pacific' pst-1 day, 16:00:00 std>)
it should return datetime.datetime(2013, 3, 10, 3, 0, tzinfo=<dsttzinfo 'us/pacific' pdt-1 day, 17:00:00 dst>)
you can workaround this, apparent, bug doing astimezone(ppt)
after adding hours.
so, bug? doing wrong? or intended have code refresh after adding time?
you need call normalize()
using timezone object again when doing datetime arithmetic:
>>> first datetime.datetime(2013, 3, 10, 2, 0, tzinfo=<dsttzinfo 'us/pacific' pst-1 day, 16:00:00 std>) >>> ppt.normalize(first) datetime.datetime(2013, 3, 10, 3, 0, tzinfo=<dsttzinfo 'us/pacific' pdt-1 day, 17:00:00 dst>)
as noted in docs:
in addition, if perform date arithmetic on local times cross dst boundaries, result may in incorrect timezone.
normalize()
method provided correct this.
Comments
Post a Comment