带有时区偏移量的 PostgreSQL 格式时间戳澳大利亚/悉尼为 JSON/String

问题描述

我想在 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。

谢谢。

解决方法

to_json 根据会话的当前时区设置格式化。我建议您先将会话时区 set 设为 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 我猜这就是你需要的。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...