zoukankan      html  css  js  c++  java
  • 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.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
  • 相关阅读:
    分布式爬虫
    前端页面展示
    fillter根据value来匹配字段
    element ui 怎么去修改el-date-picker的时间
    element ui,input框输入时enter健进行搜索
    element ui 里面的table怎么弹出一个框让表中数据点击出现弹框
    修改数据结构记录,将同级数据改成父子集数据
    h5的复制功能的使用,Clipboard.js的使用,主要是在app里面使用
    在安卓手机下按钮会悬浮在键盘上,怎么解决vue.js
    last-child为啥不生效
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/4383882.html
Copyright © 2011-2022 走看看