pytz.astimezone not accounting for daylight savings?

前端 未结 1 1462
臣服心动
臣服心动 2020-12-10 11:53

On 2013 Jun 1 I expect the \"PST8PDT\" timezone to behave like GMT+7, as it is daylight savings in that timezone. However, it behaves like GMT+8:

>>>         


        
相关标签:
1条回答
  • 2020-12-10 12:16

    You can't assign the timezone in the datetime constructor, because it doesn't give the timezone object a chance to adjust for daylight savings - the date isn't accessible to it. This causes even more problems for certain parts of the world, where the name and offset of the timezone have changed over the years.

    From the pytz documentation:

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

    Use the localize method with a naive datetime instead.

    >>> Pacific.localize(datetime.datetime(2013, 6, 1, 12)).astimezone(pytz.utc)
    datetime.datetime(2013, 6, 1, 19, 0, tzinfo=<UTC>)
    >>> Pacific.localize(datetime.datetime(2013, 1, 1, 12)).astimezone(pytz.utc)
    datetime.datetime(2013, 1, 1, 20, 0, tzinfo=<UTC>)
    
    0 讨论(0)
提交回复
热议问题