【问题标题】:Postgres saves 'timestamp with time zone' with default time zonePostgres 使用默认时区保存“带时区的时间戳”
【发布时间】:2022-11-22 22:33:44
【问题描述】:

我在 Postgres (13.3) 中有一张桌子:

create table owner (date_time timestamp with time zone);

我保存了一条带时区的记录:

insert into owner(date_time) values (timestamp with time zone '2010-10-10 10:10:10.000000 +03:00');

我希望该值与我定义的时区一起保存,但实际值是:

2010-10-10 07:10:10.000000 +00:00

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    timestamp with time zone 这个名字可能是不幸的,但几十年来一直如此,我们现在坚持使用它。

    最好将它命名为 absolute timestamp 以及它实际上是什么商店是 UTC 中的时间戳。它实际上是什么显示是您客户定义的时区中的时间戳。

    richard=> SELECT CURRENT_TIMESTAMP;
    ┌───────────────────────────────┐
    │       current_timestamp       │
    ├───────────────────────────────┤
    │ 2022-11-22 14:03:57.919421+00 │
    └───────────────────────────────┘
    (1 row)
    
    richard=> SET timezone = 'Europe/Paris';
    SET
    richard=> SELECT CURRENT_TIMESTAMP;
    ┌───────────────────────────────┐
    │       current_timestamp       │
    ├───────────────────────────────┤
    │ 2022-11-22 15:04:10.943315+01 │
    └───────────────────────────────┘
    (1 row)
    

    如果您真的想维护时区偏移量,则需要单独存储偏移量时间或时区名称(例如“欧洲/巴黎”)。

    请注意,它们在具有 DST 的区域中是不同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2019-11-08
      • 2021-09-22
      • 2018-11-26
      • 1970-01-01
      • 2020-09-19
      相关资源
      最近更新 更多