gpt4 book ai didi

PostgreSQL select now()::timestamp 不同于默认的 now()::timestamp

转载 作者:行者123 更新时间:2023-11-29 13:12:00 26 4
gpt4 key购买 nike

在我的程序中,每个表都有一列 last_modified:

last_modified int8 DEFAULT (date_part('epoch'::text, now()::timestamp) * (1000)::double precision) NOT NULL

为了更新,我添加了一个触发器:

CREATE OR REPLACE FUNCTION sync_lastmodified() RETURNS trigger AS $$
BEGIN
NEW.last_modified := (date_part('epoch'::text, now()::timestamp) * (1000)::double precision);

RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER
sync_lastmodified
BEFORE UPDATE ON
ourtable
FOR EACH ROW EXECUTE PROCEDURE
sync_lastmodified();

他们应该在更新/插入时将当前时间作为长值写入 last_modified 列。但是,它没有像我预期的那样工作。

为了重现该问题,我进行了更新并获得了以下信息:

last_modified value equals 1543576224455 (Friday November 30, 2018 16:10:24 (pm) in time zone Asia/Tashkent (+05))

几乎同时我从 pgAdmin 运行函数 now:

SELECT now()

得到结果:

2018-11-30 11:10:36.891426+05

为了在几秒钟内检查系统时间,我从终端运行 timedatectl status 并得到以下结果:

enter image description here

The question is why the function now() gives a time with 5 hours difference when I run it from trigger or as a default value when insert?

最佳答案

epoch 将为您提供自纪元以来的秒数。作为the documentation说:

epoch

For timestamp with time zone values, the number of seconds since 1970-01-01 00:00:00 UTC (can be negative)

由于您与 UTC 相差 5 小时,这就解释了差异。

关于PostgreSQL select now()::timestamp 不同于默认的 now()::timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552343/

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