datetime.combine with timezone 不同于 datetime.now with timezone

datetime.combine with timezone is different from datetime.now with timezone

在下面的代码中:

from datetime import datetime
import pytz

EDT = pytz.timezone('US/Eastern')

d1 = datetime.now(tz=EDT)

d2 = datetime.combine(d1.date(), d1.time(), tzinfo=EDT)

为什么 d1d2 显示不同的时区信息?

>> d1
datetime.datetime(2021, 4, 8, 7, 0, 44, 316514, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
>> d2
datetime.datetime(2021, 4, 8, 7, 0, 44, 316514, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)

如何在使用 datetime.combine 时获得与 datetime.now 相同的日期时间?

datetime.now 使用 pytz 时区对象有效地转换(本地化)您的日期时间 - 来自 docs:

If tz is not None, it must be an instance of a tzinfo subclass, and the current date and time are converted to tz’s time zone.

datetime.combine does not do that. It is as if you would write something like datetime(2020,1,1, tzinfo=pytz.timezone('US/Eastern')) - effectively not adjusting the time zone to the provided date/time. See also e.g. pytz localize vs datetime replace and pytz: The Fastest Footgun in the West 了解更多背景信息。

pytz 得到 d2 的正确方法是

d2 = EDT.localize(datetime.combine(d1.date(), d1.time()))

如果使用来自 dateutilzoneinfo (Python 3.9+) 的时区对象,则不会出现此类问题:

from datetime import datetime
from zoneinfo import ZoneInfo

EDT = ZoneInfo('US/Eastern')
d1 = datetime.now(tz=EDT)
d2 = datetime.combine(d1.date(), d1.time(), tzinfo=EDT)

# d1
# Out[75]: datetime.datetime(2021, 4, 8, 7, 57, 18, 309209, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))

# d2
# Out[76]: datetime.datetime(2021, 4, 8, 7, 57, 18, 309209, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))

# d1 == d2
# Out[77]: True