如何强制 pytz 使用当前标准时区?

How can I force pytz to use currently standard timezones?

考虑以下因素:

from datetime import datetime

import pytz


new_years_in_new_york = datetime(
    year=2020,
    month=1, 
    day=1,
    hour=0,
    minute=0,
    tzinfo = pytz.timezone('US/Eastern'))

我现在有一个 datetime 对象代表 1 月 1 日,午夜,在纽约。奇怪的是,如果我使用 pytz 将其转换为 UTC,我会得到一个奇怪的日期时间几分钟:

new_years_in_new_york.astimezone(pytz.utc)
# datetime.datetime(2020, 1, 1, 4, 56, tzinfo=<UTC>)

请注意,在纽约的午夜,在 pytz,UTC 是 4:56。在 Stack Overflow 的其他地方,我了解到这是因为 数据使用 本地平均时间 来说明标准化之前的时区。这可以在这里显示:

pytz.timezone('US/Eastern')
# <DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

看到 LMK-1 day, 19:04:00 STD 了吗?那是本地平均时间偏移量,不是我想要的偏移量,在夏令时期间是US/Eastern 不是

有什么方法可以强制 pytz 使用当前基于当前日期的标准偏移量集?在 2020 年新年,它应该只是 UTC-5。如果我提供的日期是在夏令时期间,我会想要 UTC-4。我很困惑为什么 pytz 会为 2020 年的日期使用基于 LMT 的偏移量。

>>> new_years_in_new_york
datetime.datetime(2020, 1, 1, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)

注意那个日期时间的奇数偏移量。您没有正确创建此日期时间。

This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):

>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print(loc_dt.strftime(fmt))
2002-10-27 06:00:00 EST-0500

The second way of building a localized time is by converting an existing localized time using the standard astimezone() method:

>>> ams_dt = loc_dt.astimezone(amsterdam)
>>> ams_dt.strftime(fmt)
'2002-10-27 12:00:00 CET+0100'

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

>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
'2002-10-27 12:00:00 LMT+0020'

http://pytz.sourceforge.net/#localized-times-and-date-arithmetic