gpt4 book ai didi

postgresql - 在格林威治标准时间时区插入 current_timestamp 会给出本地时间

转载 作者:行者123 更新时间:2023-11-29 14:29:07 25 4
gpt4 key购买 nike

我有一个 PostgreSQL 数据库,其中发票表中有一个日期字段:

Column           |           Type           |
payment_date | timestamp with time zone |

服务器位于 GMT-5,如您所见:

$date
Tue Jan 22 17:33:01 EST 2019

通过 PostgreSQL 命令获取 GMT 时间给我:

select current_timestamp at time zone 'GMT';
timezone
----------------------------
2019-01-22 22:33:01.087354

问题是当我进行插入/更新时:

update invoices set payment_date = current_timestamp at time zone 'GMT'

然后,当我得到查询结果时...

select payment_date from invoices

它给了我:

2019-01-22 22:33:01.087354-05 

错了! 应该给我 2019-01-22 22:33:01.087354-00

我做错了什么?

最佳答案

您的字段 payment_date 是(正确的)类型 timestamp with time zone,因此 timestamp without time zone 结果是AT TIME ZONE 操作使用您的 session 时区进行转换。

PostgreSQL 的timestamp with time zone 数据类型存储时区信息,它存储转换为显示的 session 时区的 UTC 时间戳。

最好将时间戳存储为带时区的时间戳,并在显示时将其转换为所需的时区。

关于postgresql - 在格林威治标准时间时区插入 current_timestamp 会给出本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com