【问题标题】:postgresql: how to convert UTC timestamp to Sydney timepostgresql:如何将 UTC 时间戳转换为悉尼时间
【发布时间】:2017-02-20 02:02:00
【问题描述】:

我有一个 PostgreSQL 数据库,其中包含许多存储悉尼/澳大利亚当地时间的时间戳的表。此外,我有一张以 UTC 格式存储时间戳的表。这是无法更改的。

我的问题是—— 我需要使用什么 SQL 操作来获取悉尼/澳大利亚本地时间的 UTC 时间戳?

我知道悉尼有夏令时,我不确定如何编写我的 SQL 以便转换始终正确 - 无论时间戳是否在夏令时期间获取。

请指教。

谢谢, 阿米特

【问题讨论】:

    标签: postgresql timezone timestamp utc


    【解决方案1】:

    使用at time zone 喜欢:

    t=# select now() at time zone 'AEDT';
    -[ RECORD 1 ]------------------------
    timezone | 2016-10-11 08:20:39.067256
    
    Time: 2.439 ms
    

    例如有一个outdated list of timezones 或者直接使用pg_timezone_names:

    t=# select * from pg_timezone_names()  where name ilike '%sydney%';
    -[ RECORD 1 ]----------------------
    name       | posix/Australia/Sydney
    abbrev     | AEDT
    utc_offset | 11:00:00
    is_dst     | t
    -[ RECORD 2 ]----------------------
    name       | Australia/Sydney
    abbrev     | AEDT
    utc_offset | 11:00:00
    is_dst     | t
    
    Time: 171.649 ms
    

    【讨论】:

    • 所以假设我有以下 2 条记录:
    • 如果您有 9.0 之前的版本(有 pg_timezone_names),那么请使用上面的网络链接
    猜你喜欢
    • 2016-06-11
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2013-06-30
    • 2019-03-09
    相关资源
    最近更新 更多