日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
PostgreSQLtimestamp有哪些坑要注意一下-創(chuàng)新互聯(lián)

這篇文章主要介紹PostgreSQL timestamp有哪些坑要注意一下,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供臨西企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、H5建站、小程序制作等業(yè)務(wù)。10年已為臨西眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

項(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é)果

測(cè)試輸入數(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ù)庫(kù)存儲(chǔ)的時(shí)間當(dāng)做北京時(shí)間而非UTC時(shí)間,與數(shù)據(jù)庫(kù)時(shí)區(qū)沒(méi)有關(guān)系。

總結(jié)

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

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

假如應(yīng)用部署在不同的地域,使用timestamp without time zone存儲(chǔ)timestamp這樣的設(shè)計(jì)簡(jiǎn)直是災(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

PostgreSQL timestamp有哪些坑要注意一下

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é)果:

PostgreSQL timestamp有哪些坑要注意一下

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é)果:

PostgreSQL timestamp有哪些坑要注意一下

以上是“PostgreSQL timestamp有哪些坑要注意一下”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


網(wǎng)站名稱:PostgreSQLtimestamp有哪些坑要注意一下-創(chuàng)新互聯(lián)
新聞來(lái)源:http://www.dlmjj.cn/article/ccgpoe.html