PostgreSQL:如何从Unix历元转换为日期?
声明给了我日期和时间 如何修改语句,使其只返回日期(而不返回时间)PostgreSQL:如何从Unix历元转换为日期?,sql,postgresql,date,type-conversion,epoch,Sql,Postgresql,Date,Type Conversion,Epoch,声明给了我日期和时间 如何修改语句,使其只返回日期(而不返回时间) 您可以使用来设置时间戳函数,然后将时间戳强制转换为日期 select to_timestamp(epoch_column)::date; 更多详情: /* Current time */ select now(); -- returns timestamp /* Epoch from current time; Epoch is number of seconds since 1970-01-01 00:00:0
您可以使用
来设置时间戳
函数,然后将时间戳强制转换为日期
select to_timestamp(epoch_column)::date;
更多详情:
/* Current time */
select now(); -- returns timestamp
/* Epoch from current time;
Epoch is number of seconds since 1970-01-01 00:00:00+00 */
select extract(epoch from now());
/* Get back time from epoch */
-- Option 1 - use to_timestamp function
select to_timestamp( extract(epoch from now()));
-- Option 2 - add seconds to 'epoch'
select timestamp with time zone 'epoch'
+ extract(epoch from now()) * interval '1 second';
/* Cast timestamp to date */
-- Based on Option 1
select to_timestamp(extract(epoch from now()))::date;
-- Based on Option 2
select (timestamp with time zone 'epoch'
+ extract(epoch from now()) * interval '1 second')::date;
就你而言:
select to_timestamp(epoch_ms / 1000)::date;
为我工作上述解决方案不适用于PostgreSQL的最新版本。我发现这种方法可以转换PostgreSQL 13中存储在number和int列类型中的历元时间:
SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;
选择时间戳'epoch'+(.field::int)*间隔'1秒'作为起始时间;
有关更多详细说明,请参见Postgres 10的以下内容:
选择时间戳(CAST(epoch\ms as bigint)/1000)
这对我来说很好:
SELECT t.*,
to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
FROM public.qrtz_triggers t;
似乎不起作用,我得到语法错误。它在2018年有变化吗?我运行了
select to_timestamp(extract(epoch_ms))::date
verbatim,它在epoch_ms附近给出了一个语法错误
。我去寻找其他解决方案,最终这对我有效选择时间戳'epoch'+(start_dt)*间隔'1秒'作为开始时间
。您能解释一下时间戳
和to_TIMESTAMP()
之间的区别吗?时间戳只是一种列类型,其中to_TIMESTAMP是一个内置函数,它将unix历元转换为时间戳,从'1970-01-01 00:00:00+00'开始计算。上述解决方案肯定适用于Postgres 12或13。但是您可以将解决方案简化为make\u timestamp(sec=>u列)
SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;
SELECT t.*,
to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
FROM public.qrtz_triggers t;