PostgreSQL: epoch 新纪元时间的使用
2015-05-22 13:33
204 查看
新纪元时间 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
Here is how you can convert an epoch value back to a time stamp:
时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用会将时间存储成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.12 SELECT 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[/code]
相关文章推荐
- 使用Epoch进行日期时间转换和计算的几个Shell小函数
- postgreSql中时间格式化使用的形式
- PostgreSQL中常用的时间日期脚本使用教程
- Shell使用Epoch进行日期时间转换和计算的几个小函数
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用 转
- PostgreSQL中使用动态SQL-实现自动按时间创建表分区
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间函数使用
- [转] PostgreSQL的时间/日期函数使用
- PostgreSQL的时间函数使用整理
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- cocos2d-x 2.2 android 使用已有静态库(*.a),大幅减少编译时间
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- java 使用simpleDateFormat格式化日期 时间