【问题标题】:Postgres AT TIME ZONE function shows wrong time?Postgres AT TIME ZONE 函数显示错误时间?
【发布时间】:2015-03-12 09:29:39
【问题描述】:

我正在使用转换到等于 EAT 时区的新时区 UTC+3,但 Postgres (9.1) 显示错误的时间

select '2015-01-13 08:40:00.0'::timestamp with time zone AT TIME ZONE 'UTC+03', 
       '2015-01-13 08:40:00.0'::timestamp with time zone AT TIME ZONE 'EAT';

(这里的默认时区是斯德哥尔摩)

结果是

"2015-01-13 04:40:00",
"2015-01-13 10:40:00"

为什么?

应该是 2015-01-13 10:40:00

如果将 JodaTime 与两个时区一起使用,则会显示相同的正确结果“2015-01-13 10:40:00”。

【问题讨论】:

    标签: postgresql timezone jodatime


    【解决方案1】:

    从 Postgres documentation 可以选择使用 ::timestamptz 而不是 ::timestamp WITH TIME ZONE,我在进行转换时发现了首选结果;因为它是可用选项中最简洁的,同时仍然可读。

    SELECT created_at
          ,created_at::timestamp AT TIME ZONE 'EDT' -- yields bad result
          ,created_at::timestamp WITH TIME ZONE AT TIME ZONE 'EDT'
          ,created_at AT TIME ZONE 'UTC' AT TIME ZONE 'EDT'
          ,created_at::timestamptz AT TIME ZONE 'EDT'
    

    2019-03-29 18:49:25.250431 -- raw UTC data
    2019-03-29 22:49:25.250431 -- erroneous result  
    2019-03-29 14:49:25.250431 -- accurate result    
    2019-03-29 14:49:25.250431 -- accurate result   
    2019-03-29 14:49:25.250431 -- accurate result
    

    【讨论】:

    • SELECT created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Mexico_City',这给了我正确的结果。谢谢兄弟!
    【解决方案2】:

    拼写为“UTC+3:00”的时区名称是 POSIX 时区规范。 在这种风格中,格林威治标准时间以西的区域有一个正号,东部的区域有一个负号(例如,“Etc/GMT-14”是格林威治标准时间之前/东部的 14 小时。)

    http://www.postgresql.org/docs/9.3/static/datatype-datetime.html#DATATYPE-TIMEZONES

    【讨论】:

      【解决方案3】:

      我有类似的问题,它给了我错误的日期和时间,但这里的答案让我清楚地理解并解决了我的问题。 PostgreSQL wrong converting from timestamp without time zone to timestamp with time zone

      所以我所做的就是改变

      SELECT timestamp AT TIME ZONE '+08' FROM orders;
      

      SELECT timestamp AT TIME ZONE 'UTC' AT TIME ZONE '+08' FROM orders;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-12
        • 2011-03-14
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-28
        • 2017-06-01
        相关资源
        最近更新 更多