将Julian日期转换为PostgreSQL中的时间戳

问题描述 投票:0回答:1

我有一张满是“Julian Dates”的桌子,就是距离1/1/2035的天数和秒数。我需要将这些转换为正常的postgres时间戳。有人可以帮忙吗?

--Converts '2000-06-20 12:30:15' into an Epoch time base which gives a result of -12612.478993055556
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp - '2035-01-01 00:00:00'))/86400.00) as run_ts

--Question, how to convert -12612.478993055556 back into '2000-06-20 12:30:15'
select -12612.478993055556 ??? as run_ts
postgresql datetime epoch julian-date
1个回答
1
投票

您可以使用to_timestamp()将纪元转换为时间戳。

您发布的纪元与2000-06-20不符,因为您已从中删除了另一个日期2035-01-01。

select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp )));
 date_part
-----------
 961504215
(1 row)

select to_timestamp(961504215);
      to_timestamp
------------------------
 2000-06-20 08:30:15-04
(1 row)


select to_timestamp(-12612.478993055556);
         to_timestamp
-------------------------------
 1969-12-31 15:29:47.521007-05
(1 row)

编辑

由于您没有考虑真正的时代,但实际上两个日期之间存在差异,您只需将此差异添加到参考日期即可。你可以使用day间隔来消除乘以86400(秒/天)的需要

select  '2035-01-01 00:00:00'::timestamp + interval '1' day *  -12612.478993055556;
      ?column?
---------------------
 2000-06-20 12:30:15
© www.soinside.com 2019 - 2024. All rights reserved.