我timestamp without time zone default now()
在PostgreSQL数据库中有一列“创建”的类型。
如果我选择“ colums”,则默认情况下它的格式很好看:
SELECT created FROM mytable;
created
---------------------------
2011-05-17 10:40:28.876944
但是我只想以毫秒为单位获取时间戳(以Long为单位)。像这样:
从mytable中选择myformat(创建);
created
-----------------
2432432343876944
我怎样才能在几毫秒内从PostgreSQL获取时间戳列?
对杰克的回应:
我确实得到了与您(-3600)相同的区别,但是如果我使用的话,timestamp with time zone
我会看到“错误”或区别是因为'1970-01-01'获得了时区+01
。
create table my_table_2(created timestamp with time zone);
CREATE TABLE
insert into my_table_2 (created) values (now()), ('1970-01-01');
INSERT 0 2
select created, extract(epoch from created) from my_table_2;
created | date_part
-------------------------------+------------------
2011-05-18 11:03:16.909338+02 | 1305709396.90934
1970-01-01 00:00:00+01 | -3600
(2 rows)
区别是错误吗?我现在可能是因为“夏令时”吗?
to_timestamp()
在插入时间戳0和1时也很有趣。
insert into my_table_2 (created) values (to_timestamp(0));
INSERT 0 1
insert into my_table_2 (created) values (to_timestamp(1));
INSERT 0 1
select created, extract(epoch from created) from my_table_2;
created | date_part
-------------------------------+------------------
2011-05-18 11:03:16.909338+02 | 1305709396.90934
1970-01-01 00:00:00+01 | -3600
1970-01-01 01:00:00+01 | 0
1970-01-01 01:00:01+01 | 1