欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PostgreSQL timestamp踩坑记录与填坑指南

程序员文章站 2022-04-13 10:04:39
项目timezone情况nodejs:utc+08postgresql:utc+00timestamptest.jsconst { client } = require('pg')const clie...

项目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()
})

不同时区to_timestamp查询结果

测试输入数据为1514736000(utc时间2017-12-31 16:00:00,北京时间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官方文档对timestamp的一个描述

详见: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进行时间转换且db时区非utc时,写入**timestamp without time zone**类型的column则会与预期结果不符。

不同timezone/columntype查询结果

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

据以上结果可判定:

使用pg查询**timestamp without time zone**类型的column时,会将数据库存储的时间当做北京时间而非utc时间,与数据库时区没有关系。

总结

网上类似问题的解决办法是将db时区改为utc+08。

原理:写入db的时间实际为北京时间,pg库恰好是当做北京时间读取,所以时间戳就不会出问题了。

假如应用部署在不同的地域,使用timestamp without time zone存储timestamp这样的设计简直是灾难。

不要用timestamp without time zone存储timestamp!

不要用timestamp without time zone存储timestamp!

不要用timestamp without time zone存储timestamp!

补充:pg查询时间间隔(timestamp类型)

create_date timestamp(6) without time zone

PostgreSQL timestamp踩坑记录与填坑指南

1.从2015-10-12到2015-10-13 之间的4点到9点的数据

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;

结果:

PostgreSQL timestamp踩坑记录与填坑指南

2.2015-10-12五点的数据

select * from schedule where hospital_id='syzyyadmin' and date_trunc('hour',create_date)=to_timestamp('2015-10-12 05','yyyy-mm-dd hh24')

结果:

PostgreSQL timestamp踩坑记录与填坑指南

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。