【发布时间】:2021-04-13 07:52:22
【问题描述】:
我发现 Python 中不同时区的行为非常奇怪。以下代码:
import datetime as dtm
import pytz
def test_tz():
america_ny_tz: dtm.tzinfo = pytz.timezone("America/New_York")
est_tz: dtm.tzinfo = pytz.timezone("EST5EDT")
today = dtm.date.today()
ny_dtm = dtm.datetime(
year=today.year, month=today.month, day=today.day, tzinfo=america_ny_tz
)
est_dtm = dtm.datetime(
year=today.year, month=today.month, day=today.day, tzinfo=est_tz
)
print(f"New York: {ny_dtm.timestamp()}, EST: {est_dtm.timestamp()}")
if __name__ == "__main__":
test_tz()
输出:
New York: 1609995360.0, EST: 1609995600.0
您可能会注意到,差异大约是 4 分钟,人们不得不假设时间应该是相同的。
我是否错误地访问了时区信息,或者我认为时区信息应该相同?
在 Linux、Ubuntu 20.04 上运行,但在 18.04 上的行为是相同的。
附:我没有尝试过其他语言或不同的操作系统来查看行为是否相同。
【问题讨论】:
-
关于您的 p.s.,您遇到的问题是特定于库的,而不是特定于操作系统的。在 Windows 上遇到同样的问题 ;-) 顺便说一下,请注意数据库中 Olson 先生的 this comment -
EST5EDT已过时。 -
@MrFuppes 我知道
EST5EDT已过时,但仍在使用中,因此是我的问题。
标签: python linux timezone ubuntu-20.04 pytz