Difference between datetime.combine() and pytz.localize()

后端 未结 1 781
自闭症患者
自闭症患者 2021-01-20 16:24

I am a bit puzzled by the following behavior. Suppose I use datetime.combine() to construct a timezone-aware object:

>>> date
datetime.date(2018, 10         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 17:08

    You just found out (again) that you should never directly add timezone information when creating timezone-aware datetimes. Always use tz.localize().

    The problem you are seeing is because datetime.combine doesn't adjust the tzinfo object to the actual datetime. It still assumes the timezone information of the first valid date in this timezone, which was in the late 1800's and happened to be 0:53:00 off from UTC.

    0 讨论(0)
提交回复
热议问题