【问题标题】:Why is 'UTC-4" timezone in Postgres leads to +04, and 'UTC+4' leads to -04?为什么 Postgres 中的“UTC-4”时区导致 +04,而“UTC+4”导致 -04?
【发布时间】:2021-11-28 09:25:06
【问题描述】:

对我来说,将“UTC-4”与“-04”相关联似乎更直观,反之亦然。

我在此处附上了一张图片,说明我在尝试选择相应时区的当前时间时得到的结果。

【问题讨论】:

  • 您当地的时区是多少?顺便说一句:time with time zone(因此current_time)的使用是discouraged
  • 请将代码发布为文本,而不是图像。

标签: postgresql time


【解决方案1】:

根据@a_horse_with_no_name 评论中的链接,UTC-4/UTC+4 是 POSIX 风格的时区名称,因此来自 UTC 的方向是相反的。换句话说,+ 从格林威治向西,- 向东。您的选择是:

  1. 认识到这一点并根据需要反转你的标志。

  2. 使用已知时区,例如 America/Porto_Velho(-04) 或 Asia/Baku(+04)。尽管如果 DST 规则针对该时区发生变化,此偏移量可能会发生变化。

  3. 执行以下操作。我目前在美国PST

select current_time;
    current_time    
--------------------
 09:05:28.464408-08

 select (split_part((current_time at time zone 'UTC' - interval '4 hours')::varchar, '+', 1) || '-04')::timetz;
       timetz       
--------------------
 13:05:39.056446-04

select (split_part((current_time at time zone 'UTC' + interval '4 hours')::varchar, '+', 1) || '+04')::timetz;
       timetz       
--------------------
 21:05:50.624686+04

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多