Why a timezone aware datetime's tzinfo does not equal the timezone?

后端 未结 2 1581
北荒
北荒 2021-01-19 13:00
>>> import pytz
>>> tz = pytz.timezone(\'America/Chicago\')
>>> dt_naive = datetime(year=2017, month=6, day=6)
>>> dt_aware = tz.         


        
2条回答
  •  日久生厌
    2021-01-19 13:36

    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
    

提交回复
热议问题