“错误:时间戳超出范围”将存储为 bigint 的纪元转换为时间戳

Col*_*art 4 postgresql timestamp type-conversion postgresql-10

ERROR: timestamp out of range: "1.52701e+15"当我尝试将存储为 bigint 的纪元转换为时间戳(值取自真实数据库表)时,我得到了:

select to_timestamp(1527012834506374);
ERROR:  timestamp out of range: "1.52701e+15"
Run Code Online (Sandbox Code Playgroud)

其他转换方法也不起作用:

select 1527012834506374::abstime::timestamp;
ERROR:  cannot cast type bigint to abstime

select 1527012834506374::integer::abstime::timestamp;
ERROR:  integer out of range
Run Code Online (Sandbox Code Playgroud)

这是一个有效的时代;https://www.epochconverter.com/告诉我 1527012834506374 相当于 2018-05-22 06:13:54.506 UTC

如何在 Postgres 中使用 SQL 进行转换?

a_h*_*ame 6

当我将该值粘贴1527012834506374https://www.epochconverter.com/ 时,我看到了警告:

假设此时间戳以微秒为单位(1/1,000,000 秒):

Postgres'to_timestamp()假设一个以秒为单位的纪元,而不是微秒,因此您需要使用:

select to_timestamp(1527012834506374 / 1000000.0)
Run Code Online (Sandbox Code Playgroud)

  • 使 `to_timestamp(1527012834506374 / 1000000.0)` 避免整数除法截断小数位。 (2认同)