简体   繁体   English

PostgreSQL 将带有时区偏移(澳大利亚/悉尼)的时间戳格式化为 JSON/字符串

[英]PostgreSQL Format Timestamp with Timezone Offset (Australia/Sydney) as JSON/String

I would like to get a JSON object in Postgres that display timestamp with Australia/Sydney timezone offset (+10:00 or +11:00 during daylight saving), for example I would like json object returned with following values: I would like to get a JSON object in Postgres that display timestamp with Australia/Sydney timezone offset (+10:00 or +11:00 during daylight saving), for example I would like json object returned with following values:

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

But when I use either to_json() or to_char() the timestamp value returned is UTC with offset +00: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.895185+00:00”
2021-01-31T07:47:22:895+00 2021-01-31T07:47:22:895+00

I have tried to add "at time zone 'AEDT'" but it shifts the timestamp value and keep the offset to +00:00.我试图添加“在时区'AEDT'”,但它会改变时间戳值并将偏移量保持为 +00:00。

Thanks.谢谢。

to_json formats according to the current time zone setting of the session. to_json格式根据 session 的当前时区设置。 I suggest that you set the session time zone to Australia/Sydney first.我建议您先将 session 时区设置Australia/Sydney

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

Yields 2021-01-31T18:47:22.895+11:00 which I guess is what you need.产量 2021-01-31T18:47:22.895+11:00 我猜这就是你需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM