【问题标题】:PostgreSQL Format Timestamp with Timezone Offset (Australia/Sydney) as JSON/String具有时区偏移(澳大利亚/悉尼)的 PostgreSQL 格式时间戳作为 JSON/字符串
【发布时间】:2021-05-04 16:48:39
【问题描述】:

我想在 Postgres 中获取一个 JSON 对象,该对象显示带有澳大利亚/悉尼时区偏移量的时间戳(夏令时期间为 +10:00 或 +11:00),例如,我希望返回具有以下值的 json 对象:

"2021-07-31T23:59:59.123456+10:00"   
"2021-01-31T23:59:59.123456+11:00"

但是当我使用 to_json() 或 to_char() 时,返回的时间戳值是 UTC,偏移量为 +00:00

select to_json(current_timestamp::timestamptz),
to_char(current_timestamp::timestamptz, 'YYYY-MM-DD"T"HH24:MI:SS:MSOF')

“2021-01-31T07:47:22.895185+00:00”
2021-01-31T07:47:22:895+00

我尝试添加“在时区 'AEDT'”,但它会移动时间戳值并将偏移量保持为 +00:00。

谢谢。

【问题讨论】:

  • 不相关,但是:current_timestamp::timestamptz可以简化为current_timestamp

标签: postgresql timezone-offset timestamp-with-timezone to-char to-json


【解决方案1】:

to_json 根据会话的当前时区设置进行格式化。建议你先把set的会话时区改成Australia/Sydney

set time zone 'Australia/Sydney';
select to_json('2021-01-31T07:47:22.895+00'::timestamptz);

产量 2021-01-31T18:47:22.895+11:00 我猜这就是你需要的。

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多