如何从 'timestamp with time zone' 列读取时区?

How to read timezone from 'timestamp with time zone' column?

我无法找到读取类型为 timestamp with time zone.

的 PostgreSQL 列中的时区值的方法

JDBC 提供方法 java.sql.ResultSet#getTimestamp(int, java.util.Calendar) 但我必须提供我自己的日历。我无法从我正在阅读的时间戳字段中获取该日历。

我正在研究存储多个时区时间数据的系统。我需要保存包含时区信息的数据,并能够从数据库中读回该时区。

没有像

这样的黑客攻击是否可能

我正在使用 JDBC 41(JDBC4 Postgresql 驱动程序,版本 9.4-1201),java 8.

PostgreSQL 文档 here 说:

For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT).

所以没有必要 "store the time zone [that corresponds to the timestamp value]" 本身 ; timestamp with time zone 值始终存储为 UTC。

另外,没必要"obtain the Calendar from the timestamp field"。日历的用途是让您定义 想要在您的 Java 应用程序中使用的特定时区。

换句话说,timestamp with timezone 不存储来自不同时区的值(例如,一些在 EST 中,另一些在 PST 中,等等),它会在插入时间戳值时将所有内容转换为 UTC。

接受的答案真实准确。 timestamp with time 类型不在字段中存储时区信息,并且无法将其提取出来。

如果对时间戳的时区感兴趣,必须单独存储(在其他字段中,或自定义列类型)。

乍一看,似乎可以使用函数 extract(timezone from field)timestamp with timezone 中提取时区,但事实并非如此。

该函数只给出 'time zone offset from UTC, measured in seconds'。重要的(并且未在文档中说明)部分是 偏移量是从当前时区 开始测量的(由会话 SET SESSION TIME ZONE 设置,如果未设置,则为服务器时区)。它不是保存字段时使用的偏移量。