【发布时间】:2012-05-18 10:40:49
【问题描述】:
我正在尝试使用 Python datetime 模块比较两次,但我似乎无法在 UTC 中创建时区感知 time 对象。
>>> import pytz, datetime
>>> UTC_TZ = pytz.utc
>>> EASTERN_TZ = pytz.timezone('America/New_York')
>>> d1 = datetime.time(10, tzinfo = UTC_TZ)
>>> d1
datetime.time(10, 0, tzinfo=<UTC>)
>>> d2 = datetime.time(10, tzinfo = EASTERN_TZ)
>>> d2
datetime.time(10, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)
>>> d1 < d2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware times
这是一个错误吗?我需要使用特殊的 UTC 时区吗?怎么回事?
【问题讨论】:
标签: python datetime timezone pytz