database/gdb: PostgreSQL timetz loses its time zone offset on read, +08 and -05 read back the same
Author: lingcoderCreated Sep 22, 2026Updated Sep 23, 2026
问题
PostgreSQL / openGauss 的 timetz(time with time zone)经 gdb 读出后偏移量被丢弃,只剩时分秒:
| 写入 | PG 存储(::text) |
gf 读回 |
|---|---|---|
'12:34:56+08'::timetz |
12:34:56+08 |
12:34:56 |
'12:34:56-05'::timetz |
12:34:56-05 |
12:34:56 |
两个相差 13 小时的值读回完全一样。对照 timestamptz 是正确的(+08 → 04:34:56、-05 → 17:34:56,归一到 UTC)。
复现(pgsql 驱动):
one, _ := db.GetOne(ctx, `SELECT '12:34:56+08'::timetz AS a, '12:34:56-05'::timetz AS b`)
fmt.Println(one["a"].String(), one["b"].String()) // 12:34:56 12:34:56原因
timetz 被判为 LocalTypeDatetime,ConvertValueForLocal 走 gtime.StrToTime 后再格式化,偏移量在这一步丢失。
改判成 LocalTypeTime 没有用:那条路走 Format("H:i:s"),同样扔掉偏移量。两个现有的时间类 LocalType 都不保留时区。
这与 #4845 修的问题不同:#4845 是 gdb 交出正确的值后,gconv 在 map→struct 转换中丢了 location;这里是值在 gdb 层就已经不完整,#4845 合并后仍可复现(master dbaf57f60)。也与 #4578 不同:那是裸 timestamp 的 Unix 瞬间偏移,这里是 timetz 的偏移量字段被丢弃。
两条修法,各有代价
timetz→LocalTypeString,原文12:34:56+08返回。无损;但目前把它扫进*gtime.Time字段的调用方会失败。- 新增一个保留偏移量的 LocalType(如
LocalTypeTimeTz)并配套转换。兼容现有调用方;但是加特性,且需要决定gtime.Time如何表示"只有时间、带偏移、无日期"。
PostgreSQL 文档对 timetz 的评价是 "usefulness questionable" 并建议用 timestamptz,所以影响面有限,但丢弃偏移量是静默的。
环境
- master
dbaf57f60 - PostgreSQL 17、openGauss 7.0.0-RC1
Source: gogf/gf