pytz 等/GMT-5

pytz and Etc/GMT-5(pytz 等/GMT-5)

本文介绍了pytz 等/GMT-5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解Etc/GMT-5"时区与 pytz 中的 UTC 之间的转换.

I'm having trouble understanding the conversion between the "Etc/GMT-5" timezone and UTC in pytz.

>>> 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:

Everything is fine so far, but then I try to convert that to UTC:

>>> gmt_5.localize(dt).astimezone(pytz.utc)
datetime.datetime(2009, 9, 9, 5, 0, tzinfo=<UTC>)

所以在我看来,当从 GMT-5 的 10:00 转换为 UTC 时,我得到了 05:00?我希望 pytz 给我 15:00.

So to me it seems that when converting from 10:00 in GMT-5 to UTC I get 05:00? I would expect pytz to give me 15:00 instead.

我错过了什么?

我已经确认美国/东部时区的时区转换正如我所期望的那样工作:

I have confirmed that timezone conversion for the US/Eastern timezone works just as I'd expect:

>>> 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 得到的.这是一个 pytz 错误吗?

EDIT 2: I have confirmed that when I use Etc/GMT+5 I get 15:00, which is what I'd expect to get from Etc/GMT-5. Is this a pytz bug?

推荐答案

这显然是 POSIX 的事情.来自 维基百科:

This is apparently a POSIX thing. From Wikipedia:

为了符合 POSIX 风格,以Etc/GMT"开头的区域的符号与大多数人的预期相反.在这种样式中,GMT 以西的区域为正号,以东的区域为负号.

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.

这篇关于pytz 等/GMT-5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:pytz 等/GMT-5

基础教程推荐