【问题标题】:Postgres how to convert milliseconds to date with day light savingPostgres如何通过夏令时将毫秒转换为日期
【发布时间】:2021-02-02 01:57:16
【问题描述】:

我在 postgres DB 中将日期时间以毫秒为单位存储为 bigint。我需要将它们转换为日期时间。函数 to_timestamp() 没有考虑夏令时设置。 例如,我在 DB 中有以下值:

select username, created_timestamp, to_timestamp(created_timestamp/1000) from user_entity;

             username         | created_timestamp | to_timestamp
    --------------------------+-------------------+-----------------------
     user_15                  |     1567066962775 | 2019-08-29 08:22:42+00
     user_16                  |     1581145146219 | 2020-02-08 06:59:06+00

我希望看到以下结果作为夏令时效果:

user_15 | 29.08.2019, 09:22 
user_16 | 08.02.2020, 06:59

【问题讨论】:

    标签: postgresql timezone psql dst


    【解决方案1】:

    如果您将timezone 设置为您希望查看格式化字符串的时区,您将获得您想要的结果:

    SET timezone = 'Europe/London';
    

    PostgreSQL 将timestamp with time zone 格式化为当前会话时区。

    【讨论】:

    • 是否可以使用 jdbc url 参数设置客户端时区?
    • 当然,使用options参数传递timezone。见the documentation
    猜你喜欢
    • 2018-08-21
    • 2023-04-06
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2017-02-14
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多