python - 为什么时区感知日期时间的 tzinfo 不等于时区?

标签 python datetime timezone pytz

>>> import pytz
>>> tz = pytz.timezone('America/Chicago')
>>> dt_naive = datetime(year=2017, month=6, day=6)
>>> dt_aware = tz.localize(dt_naive)
>>> dt_aware.tzinfo == tz
False

这些不同的原因是什么?

>>> dt_aware.tzinfo
<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>
>>> tz
<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD>

最佳答案

第一个已调整为提供的日期和时间,2016-06-06T00:00:00。中部夏令时 (CDT) 此时生效。它比 UTC 晚 5 小时(24:00 - 05:00 = 19:00)。

第二个尚未本地化,因此它为您提供了可用时区数据中的第一个偏移量,恰好是 Local Mean Time (LMT)入口。 You can see this in the tzdata sources here . LMT 比 UTC 晚 5 小时 50 分 36 秒。 LMT 偏移量的秒数在 pytz 中的某处四舍五入,因此 18:09 正确反射(reflect)了这一点(24:00 - 05:51 = 18:09)

关于python - 为什么时区感知日期时间的 tzinfo 不等于时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44397094/

相关文章:

python - 使用 self.methods 进行多重处理

MySQL TIMESTAMP 停止处理夏令时转换

ruby - 如何使用 DST 偏移量获取 time_zone_options_for_select?

python - pandas系列功能构成

python - 从 MultiIndex 中的索引列获取唯一值

java - 将日期转换为 SimpleDateFormat

python - 构建和比较时间

java - 如何使用时区 ID 获取 GMT 等效值

python - Django:某些页面的 CSS 未扩展?

javascript - 如何在 JavaScript 中将数字转换为日期