PostgreSQL: epoch 新紀元時間的使用

來源:互聯網
上載者:User

標籤:

  新紀元時間 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 新紀元時間的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.