星空网 > 软件开发 > 数据库

PostgreSQL: epoch 新纪元时间的使用

  新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00
时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用会将时间存储成epoch 时间形式,以提高读取效率,
下面演示下 pg 中 epoch 时间的使用换算方法。


--1 将 time stamp 时间转换成 epoch 时间
francs=> select extract(epoch from timestamp without time zone '1970-01-01 01:00:00');
 date_part 
-----------
      3600
(1 row)

francs=> select extract(epoch from timestamp without time zone '1970-01-01 02:00:00');
 date_part 
-----------
      7200
(1 row)

francs=> select extract(epoch from interval '+1 hours');
 date_part 
-----------
      3600
(1 row)


francs=> select extract(epoch from interval '-1 hours');
 date_part 
-----------
     -3600
(1 row)

 

--2 将epoch 时间转换成  time stamp  时间
francs=> select timestamp without time zone 'epoch' + 3600 * interval '1 second';
      ?column?       
---------------------
 1970-01-01 01:00:00
(1 row)

francs=> select timestamp without time zone 'epoch' + 7200 * interval '1 second';
      ?column?       
---------------------
 1970-01-01 02:00:00
(1 row)


--3 手册上关于 epoch 的解释
      For date and timestamp values, the number of seconds since 1970-01-01 00:00:00 UTC (can be negative); 
for interval values, the total number of seconds in the interval 

 

epoch

For date and timestamp values, the number of seconds since 1970-01-01 00:00:00 UTC (can be negative); for interval values, the total number of seconds in the interval

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');Result: 982384720.12SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');Result: 442800

Here is how you can convert an epoch value back to a time stamp:

SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720.12 * INTERVAL '1 second';

http://www.postgresql.org/docs/9.1/static/functions-datetime.html



原标题:PostgreSQL: epoch 新纪元时间的使用

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

林志玲都结婚了,shopee这波操作你还不会?:https://www.ikjzd.com/articles/97645
亚马逊卖家如何做选品?:https://www.ikjzd.com/articles/97646
亚马逊店铺注册3大雷区,千万别踩!:https://www.ikjzd.com/articles/97649
好消息!eBay将提供快速免费有保障的送货服务!:https://www.ikjzd.com/articles/9765
亚马逊选品:需要考虑哪些重要要素:https://www.ikjzd.com/articles/97651
亚马逊ACoS,卖家做到多少值是最好的?:https://www.ikjzd.com/articles/97652
珠海附近过年旅游景点 珠海附近过年旅游景点免费:https://www.vstour.cn/a/408243.html
韶关有组团到山西旅游 韶关到山西有多少公里:https://www.vstour.cn/a/408244.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流