我正试图绕过
Postgresql时区,我似乎无法弄清楚这一点. EST是美国的“东部标准时间”,通常为
UTC-5.
实施例1:基础测试
select '08/31/2011 12:00 pm EST'::timestamptz at time zone 'EST';
timezone
---------------------
2011-08-31 12:00:00
示例2:偏移量为5
select '08/31/2011 12:00 pm EST' at time zone '+5';
timezone
---------------------
2011-08-31 12:00:00
例3:偏移量为-5
select '08/31/2011 12:00 pm EST' at time zone '-5';
timezone
---------------------
2011-08-31 22:00:00
显然,一切都是倒退的. EST再次…… supposed to be UTC-5.现在,我确实搜索了文档,它确实解释了事物是“POSIX”,which is backwards.(正偏移是GMT以西,而负偏移是GMT以东).
但是,我该如何解决这个问题呢?在应用程序层,我总是可以将符号反转为 – 符号,但这对我来说似乎有些混乱.因此,我的终极问题.
在数据库层(Postgres),有没有办法使用“At Time Zone”语法,以便GMT-5对应EST?或者我只需要在应用层反转所有内容?
最佳答案 使用
documentation中编写的interval数据类型来获得正确的行为:
In these expressions, the desired time zone
zone
can be specified
either as a text string (e.g., ‘PST’) or as an interval (e.g.,
INTERVAL ‘-08:00’).
基础测试:
SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE 'EST';
timezone
---------------------
2011-08-31 12:00:00
(1 row)
时区信息:
SELECT * FROM pg_timezone_abbrevs WHERE abbrev LIKE 'EST';
abbrev | utc_offset | is_dst
--------+------------+--------
EST | -05:00:00 | f
(1 row)
适当的偏移-5:
SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE '-05:00'::interval;
timezone
---------------------
2011-08-31 12:00:00
(1 row)
适当的偏移量5:
SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE '+05:00'::interval;
timezone
---------------------
2011-08-31 22:00:00
(1 row)