pytz和Etc/GMT5

2024-10-01 00:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难理解pytz中“Etc/GMT-5”时区和UTC之间的转换。在

>>> dt = datetime(2009, 9, 9, 10, 0) # September 9 2009, 10:00
>>> gmt_5 = pytz.timezone("Etc/GMT-5")
>>> gmt_5.localize(dt)
datetime.datetime(2009, 9, 9, 10, 0, tzinfo=<StaticTzInfo 'Etc/GMT-5'>)

到目前为止一切都很好,但我尝试将其转换为UTC:

^{pr2}$

所以在我看来,从GMT-5的10:00转换到UTC时,我得到的是05:00?我希望派兹给我15点的时间。在

我错过了什么?在

编辑:我已经确认美国/东部时区的时区转换与我预期的一样:

>>> eastern = pytz.timezone("US/Eastern")
>>> eastern.localize(dt)
datetime.datetime(2009, 9, 9, 10, 0, tzinfo=...) # Too long
>>> pytz.utc.normalize(eastern.localize(dt).astimezone(pytz.utc))
datetime.datetime(2009, 9, 9, 14, 0, tzinfo=<UTC>)

编辑2:我已经确认,当我使用Etc/GMT+5时,我会得到15:00,这是我希望从Etc/GMT-5得到的。这是皮兹虫吗?在


Tags: 编辑datetimedtetcutctimezonetzinfoeastern
2条回答

这显然是POSIX的事情。来自Wikipedia

In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.

This bug report解释此行为。显然,他们知道这一切都是颠倒的,但那是因为任何其他东西都会破坏兼容性。在

相关问题 更多 >