sql PostgreSQL相关timestamp.sql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql PostgreSQL相关timestamp.sql相关的知识,希望对你有一定的参考价值。
SHOW TIMEZONE; --> Japan
--- comparison between timestamptz and timestamp
SELECT timestamptz '1970-01-01 00:00:00' = timestamp '1970-01-01 00:00:00'; --> t
-- assumption #1
-- timestamptz '1970-01-01 00:00:00'
-- ↓ evaluated
-- timestamptz '1970-01-01 00:00:00+09'
-- ↓ cast to timestamp and then be truncated timezone-part.
-- timestamp '1970-01-01 00:00:00'
-- ↓
-- is equivalent to right hand side.
-- assumption #2
-- timestamp '1970-01-01 00:00:00'
-- ↓ cast to timestamptz and then be added default timezone
-- timestamptz '1970-01-01 00:00:00+09'
-- ↓
-- is equivalent to left hand side.
--- timezone setting doesn't affect `timestamp`.
CREATE TABLE timestamp_t (id serial, data timestamp);
INSERT INTO timestamp_t (data) VALUES (timestamp '1970-01-01 00:00:00');
INSERT INTO timestamp_t (data) VALUES (to_timestamp(0));
-- `to_timestamp(0)` is same as '1970-01-01 09:00:00+09' in timezone `Japan`.
-- Timezone-part of `to_timestamp(0)` `+09` will be truncated
-- thus '1970-01-01 09:00:00' will be stored.
-- note: to_timestamp() returns timestamptz not timestamp.
SELECT * FROM timestamp_t;
-- 1 | 1970-01-01 00:00:00
-- 2 | 1970-01-01 09:00:00
SET TIMEZONE TO 'UTC';
SELECT * FROM timestamp_t;
-- 1 | 1970-01-01 00:00:00
-- 2 | 1970-01-01 09:00:00
INSERT INTO timestamp_t (data) VALUES (timestamp '1970-01-01 00:00:00');
SELECT * FROM timestamp_t;
-- 1 | 1970-01-01 00:00:00
-- 2 | 1970-01-01 09:00:00
-- 3 | 1970-01-01 00:00:00
SET TIMEZONE TO 'Japan';
SELECT * FROM timestamp_t;
-- 1 | 1970-01-01 00:00:00
-- 2 | 1970-01-01 09:00:00
-- 3 | 1970-01-01 00:00:00
--- timezone setting is a mere input/output filter for `timestamptz`.
CREATE TABLE timestamptz_t (id serial, data timestamptz);
INSERT INTO timestamptz_t (data) VALUES (to_timestamp(0)); -- same as '1970-01-01 00:00:00-00'
INSERT INTO timestamptz_t (data) VALUES ('1970-01-01 00:00:00'); -- evaluated in timezone `Japan`
INSERT INTO timestamptz_t (data) VALUES ('1970-01-01 00:00:00-01');
INSERT INTO timestamptz_t (data) VALUES ('1970-01-01 00:00:00+01');
SELECT * FROM timestamptz_t;
-- 1 | 1970-01-01 09:00:00+09
-- 2 | 1970-01-01 00:00:00+09
-- 3 | 1970-01-01 10:00:00+09
-- 4 | 1970-01-01 08:00:00+09
SET TIMEZONE TO 'UTC';
SELECT * FROM timestamptz_t;
-- 1 | 1970-01-01 00:00:00+00
-- 2 | 1969-12-31 15:00:00+00
-- 3 | 1970-01-01 01:00:00+00
-- 4 | 1969-12-31 23:00:00+00
DROP TABLE timestamp_t;
DROP TABLE timestamptz_t;
以上是关于sql PostgreSQL相关timestamp.sql的主要内容,如果未能解决你的问题,请参考以下文章
postgresql 建模文件 LDM 转成PDM 生成 SQL问题
postgresql的timestamp对应java啥类型
带有时间戳的 postgreSQL 排序
PostgreSQL - invalid input syntax for type timestamp with time zone
PostgreSQL之日期时间小结
如何将 Joda-Time 与 java.sql.Timestamp 一起使用