为什么知道时区的日期时间的tzinfo不等于时区? [英] Why a timezone aware datetime's tzinfo does not equal the timezone?

查看:78
本文介绍了为什么知道时区的日期时间的tzinfo不等于时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> 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

这些差异的原因是什么?

What's the reason for these to differ?

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


推荐答案

从<$ c $确定时区的键c> pytz 是您创建对象时传递的字符串:'America / Chicago'。该键可通过 .zone 属性获得。

The key that determines the timezone from pytz is the string you passed to create the object: 'America/Chicago'. That key is available through the .zone attribute.

>>> 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
>>> tz.zone
'America/Chicago'
>>> dt_aware.tzinfo.zone == tz.zone
True

这篇关于为什么知道时区的日期时间的tzinfo不等于时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆