在Postgres中转换以毫秒为单位的时间戳

dou*_*yte 3 sql postgresql timestamp

我有一个以毫秒为单位的时间戳:

1420066991000
Run Code Online (Sandbox Code Playgroud)

转化成UTC:

Wed Dec 31 2014 23:03:11
Run Code Online (Sandbox Code Playgroud)

和当地时间:

Thu Jan 01 2015 00:03:11
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用to_timestamp将其转换为Postgres中的时间戳,则会给我错误的日期时间:

select to_timestamp(1420066991000);
46970-02-17 13:03:20+00
Run Code Online (Sandbox Code Playgroud)

由于to_timestamp 需要双精度输入,因此我也这样做:

select to_timestamp(1420066991000.0);
46970-02-17 13:03:20+00
Run Code Online (Sandbox Code Playgroud)

但是结果是一样的。

我在Postgres配置中是否缺少某些设置,例如某些时区设置?还是一个错误?

Jak*_*nia 7

to_timestamp()转换unix时间,即以秒为单位的时间。由于您的数据以毫秒为单位,因此应将其除以1000。

select to_timestamp(1420066991000/1000);
Run Code Online (Sandbox Code Playgroud)

  • 如果需要,除以“1000.0”将允许您保留毫秒级精度。 (7认同)