【问题标题】:Wrong aware-datetime with pytz and 'America/Chicago' [duplicate]pytz 和 'America/Chicago' 的错误感知日期时间 [重复]
【发布时间】:2015-09-27 02:02:27
【问题描述】:
In [57]: datetime.datetime(2015,7,7,15,30,tzinfo=pytz.timezone('America/Chicago'))
Out[57]: datetime.datetime(2015, 7, 7, 15, 30, tzinfo=<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>)

注意偏移量,它说的是 UTC-6(例如 -(24-18) ),而 2015-7-7 芝加哥的正确偏移量是 UTC-5。

我错过了什么吗?

【问题讨论】:

  • 日期时间时区的 API 被破坏了,因为它没有给时区对象提供自我调整的机会。

标签: python datetime timezone pytz


【解决方案1】:

有些时区和 pytz 存在问题,使用 localize 可以解决:

import pytz
import datetime
d = datetime.datetime(2015,7,7,15,30)
dt = pytz.timezone('America/Chicago').localize(d)

print(dt)
2015-07-07 15:30:00-05:00

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    相关资源
    最近更新 更多