【发布时间】:2021-01-13 19:29:18
【问题描述】:
服务器位于 EST,我将 timestamptz 存储为 lastlogin。
似乎从 UTC 到 EST 的转换正在倒退。
select lastlogin at time zone 'EST' as lastlogin from users where id = 1;
-- > 2021-01-13 18:56:28
select lastlogin at time zone 'UTC' as lastlogin from users where id = 1;
-- > 2021-01-13 13:56:28
如果从 UTC 转换为 EST 我减去 5 小时,为什么未来 EST 是 5 小时?就好像转换为 UTC 实际上给了我 EST 时间,而转换为 EST 给了我 UTC 时间。
但这有效:
select NOW() at time zone 'EST', NOW() at time zone 'UTC';
-- > 2021-01-13 14:23:21 2021-01-13 19:23:21
编辑:
我发现进行两次转换似乎可行:
select lastlogin at time zone 'UTC' at time zone 'EST' as lastlogin from users where id = 1;
-- > 2021-01-13 13:56:28
这显示在Popsql How to Convert UTC to Local Time Zone in PostgreSQL。是不是第一个at time zone会设置时间的时区,而第二个会实际进行转换?
【问题讨论】:
-
select lastlogin from users where id = 1;的值是多少? -
是
2021-01-13 18:56:28 -
很高兴您找到了答案,但请注意。不要使用 EST 作为时区;它是时区缩写。而是使用完整的时区名称;美国/东部或美国/纽约,或其他偏移 -05:00 - 其中有 34***)。这是因为全名会根据夏令时自动调整,而缩写不会。 *** select * from pg_timezone_names where abbrev = 'EST';
标签: postgresql timezone