Unix 时间戳是自 UTC 时间 1970 年 1 月 1 日午夜以来的秒数。
如何从 PostgreSQL 获取正确的 unix 时间戳?
与currenttimestamp.com和timestamp.1e5b.de相比时,我没有从 PostgreSQL 得到预期的时间:
这将返回正确的时间戳:
SELECT extract(epoch from now());
Run Code Online (Sandbox Code Playgroud)
虽然这不会:
SELECT extract(epoch from now() at time zone 'utc');
Run Code Online (Sandbox Code Playgroud)
我住在 UTC +02 时区。从 PostgreSQL 获取当前 unix 时间戳的正确方法是什么?
这将返回正确的时间和时区:
SELECT now();
now
-------------------------------
2011-05-18 10:34:10.820464+02
Run Code Online (Sandbox Code Playgroud)
另一个对比:
select now(),
extract(epoch from now()),
extract(epoch from now() at time zone 'utc');
now | date_part | date_part
-------------------------------+------------------+------------------
2011-05-18 10:38:16.439332+02 | 1305707896.43933 | 1305700696.43933
(1 …Run Code Online (Sandbox Code Playgroud)