Select 当数据库使用 PostgreSQL 时在我的时区中的日期
Select a date in my time zone when database is in UTC with PostgreSQL
我想在我的时区 select 一天,例如:
从 2021-09-09 00:00 +02 到 2021-09-10 00:00 +02。但是下面的代码:
set timezone TO 'Europe/Berlin';
SELECT TIMESTAMP::timestamptz,sensor_id,value
FROM my_table
WHERE sensor_id IN (1,2,3)
AND TIMESTAMP > '2021-09-09' AND TIMESTAMP < '2021-09-10'
ORDER BY TIMESTAMP
给我正确的值但时区错误例如第一行是:
timestamp,sensor_id,value
2021-09-09 02:00 +02,1,21
2021-09-09 02:00 +02,2,34
2021-09-09 02:00 +02,3,54
但应该是 2021-09-09 00:00 +02 或 2021-09-08 22:00 +00
冬季和夏季与utc的差异变化时问题更大
任何人都可以帮助我吗?
PostgreSQL v.12
SELECT TIMESTAMP::timestamptz AT TIME ZONE 'CETDST',sensor_id,value
FROM my_table
WHERE sensor_id IN (1,2,3)
AND TIMESTAMP AT TIME ZONE 'CETDST' > '2021-09-09'
AND TIMESTAMP AT TIME ZONE 'CETDST' < '2021-09-10'
ORDER BY TIMESTAMP
我想在我的时区 select 一天,例如:
从 2021-09-09 00:00 +02 到 2021-09-10 00:00 +02。但是下面的代码:
set timezone TO 'Europe/Berlin';
SELECT TIMESTAMP::timestamptz,sensor_id,value
FROM my_table
WHERE sensor_id IN (1,2,3)
AND TIMESTAMP > '2021-09-09' AND TIMESTAMP < '2021-09-10'
ORDER BY TIMESTAMP
给我正确的值但时区错误例如第一行是:
timestamp,sensor_id,value
2021-09-09 02:00 +02,1,21
2021-09-09 02:00 +02,2,34
2021-09-09 02:00 +02,3,54
但应该是 2021-09-09 00:00 +02 或 2021-09-08 22:00 +00
冬季和夏季与utc的差异变化时问题更大
任何人都可以帮助我吗?
PostgreSQL v.12
SELECT TIMESTAMP::timestamptz AT TIME ZONE 'CETDST',sensor_id,value
FROM my_table
WHERE sensor_id IN (1,2,3)
AND TIMESTAMP AT TIME ZONE 'CETDST' > '2021-09-09'
AND TIMESTAMP AT TIME ZONE 'CETDST' < '2021-09-10'
ORDER BY TIMESTAMP