【问题标题】:pytz and Etc/GMT-5pytz 等/GMT-5
【发布时间】:2011-04-29 20:48:24
【问题描述】:

我无法理解“Etc/GMT-5”时区与 pytz 中的 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:

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

我错过了什么?

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

>>> 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 错误吗?

【问题讨论】:

    标签: python timezone utc pytz


    【解决方案1】:

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

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

    【讨论】:

    • 你比我自己的答案早了 23 秒! :)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 2015-07-30
      • 2021-09-14
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多