pytz 和 Etc/GMT-5 [英] pytz and Etc/GMT-5

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

问题描述

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

<预><代码>>>>dt = datetime(2009, 9, 9, 10, 0) # 2009 年 9 月 9 日 10:00>>>gmt_5 = pytz.timezone("Etc/GMT-5")>>>gmt_5.localize(dt)datetime.datetime(2009, 9, 9, 10, 0, tzinfo=)

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

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

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

我错过了什么?

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

<预><代码>>>>东部 = pytz.timezone("美国/东部")>>>东方本地化(DT)datetime.datetime(2009, 9, 9, 10, 0, tzinfo=...) # 太长>>>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错误吗?

解决方案

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

<块引用>

为了符合 POSIX 风格,那些以Etc/GMT"开头的区域的符号与大多数人的预期相反.在这种样式中,格林威治标准时间以西的区域带有正号,东部的区域带有负号.

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'>)

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

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.

What am I missing?

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

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?

解决方案

This is apparently a POSIX thing. From 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.

这篇关于pytz 和 Etc/GMT-5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆