在 PostgreSQL 中,如何获取表日期字段的纪元(UTC 时间),其时区假定为 GMT,时、分、秒值假定为 '00:00:00'?
例如mytable.DT_GMT 的值类似于“2018-10-16”。
这是我到目前为止尝试过的方法。我没有使用 DT_GMT,而是使用了“2018-10-16”。一旦我有一些有用的东西,我会用表格字段替换它。
postgres=> select extract(epoch from TIMESTAMP WITH TIME ZONE concat('2018-10-16',' 00:00:00.00-00') );
ERROR: syntax error at or near "concat"
LINE 1: ...elect extract(epoch from TIMESTAMP WITH TIME ZONE concat('20...
尝试相关的东西:
postgres=> SELECT TIMESTAMP ('2018-10-16' || ' 00:00:00.00') AT TIME ZONE 'GMT';
ERROR: syntax error at or near "'2018-10-16'"
LINE 1: SELECT TIMESTAMP ('2018-10-16' || ' 20:38:40') AT TIME ZONE ...
请注意,小时...秒是必需的,因为没有它,这些值就不是必需的。
postgres=> SELECT TIMESTAMP '2018-10-16' AT TIME ZONE 'GMT';
timezone
------------------------
2018-10-15 17:00:00-07
(1 row)
最佳答案
而不是字符串连接,你 add them as date
and time with time zone
.
test=> select extract(epoch from date '2018-10-16' + time with time zone '00:00:00.00-00');
date_part
------------
1539648000
(1 row)
如果您只是添加 '00:00:00' 以使时区正常工作,则不需要将其与 date
一起使用。
test=> select date_part('epoch', date '2018-10-16');
date_part
------------
1539648000
$ date --date '@1539648000' --utc
Tue Oct 16 00:00:00 UTC 2018
这是将日期和时间存储为正确类型的一个很好的理由,特别是在 Postgres 中,它非常关心类型,而不是作为文本
。
关于postgresql - 如何在 PostgreSQL 中获取串联时间戳的 EPOCH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846362/