作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Postgresql 有一个 timestamp
数据类型,分辨率为 1 微秒,范围从公元前 4713 年到公元 294276 年,占用 8 个字节(参见 https://www.postgresql.org/docs/current/datatype-datetime.html)。
我计算出该范围内的总微秒数为 (294276 + 4713) × 365.25 × 24 × 60 × 60 × 1000000 = 9.435375266×10¹⁸。这小于 2⁶⁴ = 1.844674407×10¹⁹,但也大于 2⁶³ = 9.223372037×10¹⁸。
由于日历的异常和闰年,我可能会休息几天,但我认为这不足以将数字推到 2⁶³ 以下。
那么,为什么要这样选择限制?为什么不使用 64 位可用的全部范围?
最佳答案
时间戳的内部表示是自 2000-01-01 00:00:00
以来的微秒数,存储为 8 字节整数。所以最大可能的年份是这样的
SELECT (2::numeric^63 -1) / 365.24219 / 24 / 60 / 60 / 1000000 + 2000;
?column?
═════════════════════════
294277.2726976055146158
(1 row)
这解释了上限。
最小值由 src/include/datatype/timestamp.h
中的注释解释:
/*
* Range limits for dates and timestamps.
*
* We have traditionally allowed Julian day zero as a valid datetime value,
* so that is the lower bound for both dates and timestamps.
*
* The upper limit for dates is 5874897-12-31, which is a bit less than what
* the Julian-date code can allow. For timestamps, the upper limit is
* 294276-12-31. The int64 overflow limit would be a few days later; again,
* leaving some slop avoids worries about corner-case overflow, and provides
* a simpler user-visible definition.
*/
所以最小值取自儒略日期的下限。
关于postgresql - 为什么时间戳类型的范围是公元前 4713 年到公元 294276 年?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68350167/
我是一名优秀的程序员,十分优秀!