数据类型 "timestamp with time zone" 中的时区存储

Time zone storage in data type "timestamp with time zone"

在 PostgreSQL 中,数据类型 timestamptimestamp with timezone 都使用 8 个字节。

我的问题是:

  1. 时间戳中使用什么格式存储日期和时间?
  2. timestamp with timezone 中的时区信息是如何存储的 类型,后面读取类型时又是如何解析的?

查看 documentation:

  • 时间戳存储为整数或(已弃用的)浮点数
  • 我不相信 timestamp with timezone 可以 如果它实际上存储了时区,则可以在 8 个字节内正确编码。只是时间戳需要 64 位,因为 log2(298989 * 365 * 24 * 60 * 60 * 1000000) 大于 63。注意 time with time zone 需要 12 字节,精度相同,但范围为一天。

查看 Erwin 的回答以解释它实际上是如何设法存储在 8 个字节中的 - 它应该被称为 "timestamp without a time zone, but stored in UTC and converted into the local time zone for display"。恶心

这只是由于有点误导的类型名称而产生的误解。时区本身根本没有存储。它只是作为计算实际存储的 UTC 时间戳(输入)的偏移量。或者作为根据 current or given 时区(输出)显示时间戳的装饰器。这些都是根据 SQL 标准。

只存储时间点,不存储区域信息。这就是为什么 64 位信息就足够了。时间戳根据会话的当前时区设置显示给客户端。

详情:

  • Ignoring time zones altogether in Rails and PostgreSQL

此外,由于 Jon 提到它,time with time zone 在 SQL 标准中定义,因此在 Postgres 中实现,but its use is discouraged:

time with time zone is defined by the SQL standard, but the definition exhibits properties which lead to questionable usefulness.

这是一种固有的歧义类型,无法正确处理 DST