为什么无法用纳秒表示某些日期?

huangapple go评论81阅读模式
英文:

Why is it not possible to represent some of the dates in nanoseconds?

问题

我想将一个日期转换为自 Unix 纪元以来的纳秒数。但它有一个限制,即对于 1678 年之前或 2262 年之后的日期,它具有未定义的行为。这是什么原因呢?

英文:

I wanted to convert a date into nanoseconds since unix epoch.But it has the limitation that it has undefined behavior for dates before 1678 or after 2262. What is the reason for that?

答案1

得分: 2

只是回答问题:

((2^64) / (10^9)) / (60 * 60 *24 * 365.25) ~= 584.5 年

1678 + 584 = 2262

所以是的,选择的日期范围是因为这是 int64 的范围。

由于 584 / 2 = 292,1678 + 292 = 1970,我怀疑 0(旧的纪元日期:1970/01/01)是新范围的中间值。这是有道理的,因为这意味着可以通过将整数值乘以/除以10的幂来改变精度。

英文:

Just to answer the question:

((2^64) / (10^9)) / (60 * 60 *24 * 365.25) ~= 584.5 years

1678 + 584 = 2262

So yes, the date range chosen is because that is the range of an int64.

Since 584 / 2 = 292 and 1678 + 292 = 1970, I suspect that 0 (the old epoch date: 1970/01/01) is the middle the new range. That would make sense as it means it is possible to change precision just by multiplying / dividing the integer value by powers of 10

huangapple
  • 本文由 发表于 2023年7月27日 00:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76773208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定