Python 时区:为什么美国东部标准时间差一小时? (pytz/日期工具)

Python time zones: Why is EST off by one hour? (pytz / dateutil)

我正在尝试打印 EST 中的当前时间,但我的结果偏差了一个小时(与 Google 的“est local time”和“nyc local time”相比)。我试过同时使用 pytzdateutil:

import datetime
import dateutil.tz
import pytz

# Correct
print(datetime.datetime.now())

# Correct
print(datetime.datetime.now(dateutil.tz.UTC))

# Off by one hour
print(str(datetime.datetime.now(dateutil.tz.gettz("EST"))))

# Off by one hour
print(str(pytz.utc.localize(datetime.datetime.utcnow()).astimezone(pytz.timezone("EST"))))

我已经尝试研究执行此操作的正确方法,以上是我发现的内容,但结果是错误的(例如,当 Google 说是上午 11 点时,它显示美国东部标准时间上午 10 点).我的当地时间配置正确。我错过了什么吗?

我正在使用:

你的时间差一个小时,因为此时夏令时有效,当你指定 EST.

时不表示

如问题评论中所述 - 您应该使用特定的 locality-based 时区名称,例如 America/New_York 而不是 EST.

EST 在 pytz 和 dateutil 中都有效的原因是它 定义为 IANA 时区名称:

发件人:https://github.com/eggert/tz/blob/2020a/northamerica#L188-L206

We generate the files specified below to guard against old files with obsolete information being left in the time zone binary directory. We limit the list to names that have appeared in previous versions of this time zone package...

# Zone  NAME     STDOFF  RULES  FORMAT  [UNTIL]
Zone    EST       -5:00  -      EST
Zone    MST       -7:00  -      MST
Zone    HST      -10:00  -      HST
Zone    EST5EDT   -5:00  US     E%sT
Zone    CST6CDT   -6:00  US     C%sT
Zone    MST7MDT   -7:00  US     M%sT
Zone    PST8PDT   -8:00  US     P%sT

如您所见,ESTMSTHST 是使用固定偏移量定义的。他们不遵守任何 DST 规则。 PSTCST 没有类似条目,EDT.

等日光变体也没有类似条目

名称如 EST5EDT 的时区向后兼容旧的 POSIX-style 时区,但只定义了这 4 个。

一般来说,您不应使用这些区域中的任何一个。他们只供守卫使用。