最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL timestamp踩坑記錄與填坑指南

 更新時(shí)間:2021年01月04日 15:50:29   作者:JanFon  
這篇文章主要介紹了PostgreSQL timestamp踩坑記錄與填坑指南,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

項(xiàng)目Timezone情況

NodeJS:UTC+08

PostgreSQL:UTC+00

timestampTest.js
const { Client } = require('pg')
const client = new Client()
 
client.connect()
let sql = ``
client.query(sql, (err, res) => {
 console.log(err ? err.stack : res.rows[0].datetime)
 client.end()
})

不同時(shí)區(qū)to_timestamp查詢結(jié)果

測試輸入數(shù)據(jù)為1514736000(UTC時(shí)間2017-12-31 16:00:00,北京時(shí)間2018-01-01 00:00:00)

1、timezone=UTC

BEGIN;
SET TIME ZONE 'UTC';
SELECT to_timestamp(1514736000) as datetime;
END;

直接查詢:2017-12-31 16:00:00+00YES

pg查詢:2017-12-31T16:00:00.000ZYES

2、timezone=PRC

BEGIN;
SET TIME ZONE 'PRC';
SELECT to_timestamp(1514736000) as datetime;
END;

直接查詢:2018-01-01 00:00:00+08NO

pg查詢:2017-12-31T16:00:00.000ZYES

PostgreSQL官方文檔對(duì)timestamp的一個(gè)描述

詳見:8.5.1.3. Time Stamps

In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.

使用to_timestamp進(jìn)行時(shí)間轉(zhuǎn)換且DB時(shí)區(qū)非UTC時(shí),寫入**timestamp without time zone**類型的COLUMN則會(huì)與預(yù)期結(jié)果不符。

不同Timezone/columnType查詢結(jié)果

1、timezone=UTC,timestamp with timezone

BEGIN;
SET TIME ZONE 'UTC';
SELECT TIMESTAMP WITH TIME ZONE '2017-12-31T16:00:00+00' as datetime;
END;

直接查詢:2017-12-31 16:00:00+00YES

pg查詢:2017-12-31T16:00:00.000ZYES

2、timezone=UTC,timestamp without timezone

BEGIN;
SET TIME ZONE 'UTC';
SELECT TIMESTAMP '2017-12-31T16:00:00+00' as datetime;
END;

直接查詢:2017-12-31 16:00:00YES

pg查詢:2017-12-31T08:00:00.000ZNO

3、timezone=PRC,timestamp with timezone

BEGIN;
SET TIME ZONE 'PRC';
SELECT TIMESTAMP WITH TIME ZONE '2017-12-31T16:00:00+00' as datetime;
END;

直接查詢:2018-01-01 00:00:00+08YES

pg查詢:2017-12-31T16:00:00.000ZYES

4、timezone=PRC,timestamp without timezone

BEGIN;
SET TIME ZONE 'PRC';
SELECT TIMESTAMP '2017-12-31T16:00:00+00' as datetime;
END;

直接查詢:2017-12-31 16:00:00YES

pg查詢:2017-12-31T08:00:00.000ZNO

據(jù)以上結(jié)果可判定:

使用pg查詢**timestamp without time zone**類型的COLUMN時(shí),會(huì)將數(shù)據(jù)庫存儲(chǔ)的時(shí)間當(dāng)做北京時(shí)間而非UTC時(shí)間,與數(shù)據(jù)庫時(shí)區(qū)沒有關(guān)系。

總結(jié)

網(wǎng)上類似問題的解決辦法是將DB時(shí)區(qū)改為UTC+08。

原理:寫入DB的時(shí)間實(shí)際為北京時(shí)間,pg庫恰好是當(dāng)做北京時(shí)間讀取,所以時(shí)間戳就不會(huì)出問題了。

假如應(yīng)用部署在不同的地域,使用timestamp without time zone存儲(chǔ)timestamp這樣的設(shè)計(jì)簡直是災(zāi)難。

不要用timestamp without time zone存儲(chǔ)timestamp!

不要用timestamp without time zone存儲(chǔ)timestamp!

不要用timestamp without time zone存儲(chǔ)timestamp!

補(bǔ)充:pg查詢時(shí)間間隔(timestamp類型)

create_date timestamp(6) without time zone

1.從2015-10-12到2015-10-13 之間的4點(diǎn)到9點(diǎn)的數(shù)據(jù)

select * from schedule where create_date 
between to_date('2015-10-12','yyyy-MM-dd') 
and to_date('2015-10-13','yyyy-MM-dd')
and EXTRACT(hour from create_date) between 4 and 9;

結(jié)果:

2.2015-10-12五點(diǎn)的數(shù)據(jù)

select * from schedule where hospital_id='syzyyadmin' and date_trunc('hour',create_date)=to_timestamp('2015-10-12 05','YYYY-MM-DD HH24')

結(jié)果:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論

虎林市| 花莲县| 漳州市| 明星| 尼木县| 遂溪县| 克山县| 蒲江县| 滦平县| 砀山县| 合江县| 天全县| 安康市| 固安县| 松原市| 清流县| 抚州市| 全州县| 射洪县| 平塘县| 乡城县| 昂仁县| 中卫市| 库尔勒市| 武陟县| 金坛市| 海盐县| 通化市| 滨海县| 玉屏| 通海县| 南康市| 宁武县| 英吉沙县| 三穗县| 林州市| 怀宁县| 千阳县| 绥滨县| 绥德县| 龙南县|