我得到以下输出。这是pytz的预期行为吗?顺便说一句,我住在美国/东部时区。为什么EST给出-04:56作为时区偏移?
import datetime
import pytz
a = datetime.datetime.now()
tz_est = pytz.timezone("US/Eastern")
a = a.replace(tzinfo=tz_est)
print("EST")
print(a)
print("\n")
b = datetime.datetime.now(pytz.timezone("US/Pacific"))
print("PST - version 1")
print(b)
print("\n")
tz_pst = pytz.timezone('US/Pacific')
c = tz_pst.normalize(a)
print("PST - version 2")
print(c)
print("\n")
EST 2017-03-16 22:52:27.616000-04:56
PST - 版本1 2017-03-16 19:52:27.617000-07:00
PST - 版本2 2017-03-16 20:48:27.616000-07:00
答案 0 :(得分:0)
import datetime
import pytz
a = datetime.datetime.now(pytz.timezone("US/Eastern"))
b = datetime.datetime.now()
pacific = pytz.timezone("US/Pacific")
c = pacific.localize(b)
d = pacific.normalize(a)
print(c)
print(d)
答案 1 :(得分:0)
使用 zoneinfo 而不是 pytz 来获得预期的行为。
https://docs.python.org/3/library/zoneinfo.html
from zoneinfo import ZoneInfo
dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))