代码之家  ›  专栏  ›  技术社区  ›  Yevgeniy Afanasyev

时区“Etc/GMT”,为什么是相反的?

  •  1
  • Yevgeniy Afanasyev  · 技术社区  · 7 年前

    php中的时区是这样工作的 https://www.gsp.com/support/virtual/admin/unix/tz/gmt/

    当它被命名为 Etc/GMT+11 GMT-11

    Etc/GMT-11 GMT+11

    为什么?这是什么意思 Etc/GMT ?

    我在PHP中找到它,它是PHP中的一个bug还是无处不在?

    1 回复  |  直到 7 年前
        1
  •  6
  •   Matt Johnson-Pint    7 年前

    这不是虫子。表单的数据库标识符 Etc/GMT±* 故意用一个倒号,而不是我们在ISO8601下所期望的通常形式。也就是说,它们的正值在GMT以西,而不是正值在GMT以东。

    原因是与POSIX样式的时区标识符向后兼容,例如与第一种格式的 the TZ environment variable . 当符合POSIX的系统解释这个变量时 America/Los_Angeles 很明显,会变成第三种格式(在同一个文档中描述),但是 Etc/GMT+11 对于应该应用哪种格式规则是不明确的。因此,区域标识符必须将其符号反转以符合要求。

    From the tz database where these zones are defined :

    # Be consistent with POSIX TZ settings in the Zone names,
    # even though this is the opposite of what many people expect.
    # POSIX has positive signs west of Greenwich, but many people expect
    # positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
    # the abbreviation "-04" and corresponds to 4 hours behind UT
    # (i.e. west of Greenwich) even though many people would expect it to
    # mean 4 hours ahead of UT (i.e. east of Greenwich).
    

    本文也对此进行了讨论 the Wikipedia article on the tz database .

    至于实际问题,tz数据库评论还说:

    # These entries are mostly present for historical reasons, so that
    # people in areas not otherwise covered by the tz files could "zic -l"
    # to a time zone that was right for their area.  These days, the
    # tz files cover almost all the inhabited world, and the only practical
    # need now for the entries that are not on UTC are for ships at sea
    # that cannot use POSIX TZ settings.
    

    所以,如果你不为海上的船只计时,我强烈建议你使用一个基于位置的标识符(也许 Australia/Melbourne ?)

    the one on Wikipedia .

    the PHP documentation has a list as well ,在 "Others" 佩奇,它实际上也解释了这一点:

    警告 请不要使用此处列出的任何时区(UTC除外),它们只存在于向后兼容的原因,并且可能暴露错误行为。

    警告

    例如,在这个数据库中,中国和西澳大利亚(以及其他地方)使用的比格林尼治标准时间早8小时的时区实际上是Etc/GMT-8,而不是您通常预期的Etc/GMT+8。

    再次,强烈建议您使用正确的时区作为您的位置,如以上示例中的亚洲/上海或澳大利亚/珀斯。

    推荐文章