Java 为什么日期从 1970 年 1 月 1 日开始计算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are dates calculated from January 1st, 1970?
提问by Vijay Shanker Dubey
Is there any reason behind using date(January 1st, 1970) as default standard for time manipulation? I have seen this standard in Java as well as in Python. These two languages I am aware of. Are there other popular languages which follows the same standard?
使用日期(1970 年 1 月 1 日)作为时间操作的默认标准是否有任何原因?我已经在 Java 和 Python 中看到过这个标准。这两种语言我都知道。是否有其他流行语言遵循相同的标准?
Please describe.
请描述。
采纳答案by soldier.moth
It is the standard of Unix time.
Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.
Unix 时间或 POSIX 时间是一种用于描述时间点的系统,定义为自 1970 年 1 月 1 日午夜预测协调世界时 (UTC) 以来经过的秒数,不包括闰秒。
回答by Péter T?r?k
Yes, C (and its family). This is where Java took it too.
是的,C(及其家族)。这也是Java采用的地方。
回答by Benjamin Bannier
January 1st, 1970 00:00:00 am is the zero-point of POSIX time.
1970 年 1 月 1 日 00:00:00 am 是POSIX 时间的零点。
回答by S.Lott
Is there any reason behind using date(January 1st, 1970) as standard for time manipulation?
使用日期(1970 年 1 月 1 日)作为时间操作的标准有什么原因吗?
No reason that matters.
没有理由那么重要。
Python's time
module isthe C library. Ask Ken Thompson why he chose that date for an epochal date. Maybe it was someone's birthday.
Python 的time
模块是C 库。问问肯·汤普森,他为什么选择那个日期作为划时代的日期。也许是某人的生日。
Excel uses two different epochs. Any reason why different version of excel use different dates?
Excel 使用两个不同的时代。为什么不同版本的excel使用不同的日期?
Except for the actual programmer, no one else will ever know whythose those kinds of decisions were made.
除了真正的程序员,没有人会知道为什么做出这些决定。
And...
和...
It does not matter whythe date was chosen. It just was.
为什么选择日期并不重要。它只是。
Astronomers use their own epochal date: http://en.wikipedia.org/wiki/Epoch_(astronomy)
天文学家使用自己的划时代日期:http: //en.wikipedia.org/wiki/Epoch_(astronomy )
Why? A date has to be chosen to make the math work out. Any random date will work.
为什么?必须选择一个日期来计算。任何随机日期都可以。
A date far in the past avoids negative numbers for the general case.
对于一般情况,过去的日期避免了负数。
Some of the smarter packages use the proleptic Gregorian year 1. Any reason why year 1?
There's a reason given in books like Calendrical Calculations: it's mathematically slightly simpler.
一些更智能的包使用预测的格里高利年 1。有什么理由为什么是第 1 年?
像日历计算这样的书中给出了一个原因:它在数学上稍微简单一些。
But if you think about it, the difference between 1/1/1 and 1/1/1970 is just 1969, a trivial mathematical offset.
但是如果你仔细想想,1/1/1 和 1/1/1970 之间的区别只是 1969,一个微不足道的数学偏移。
回答by Yezpahr
Q)"Why are dates calculated from January 1st, 1970?"
问)“为什么从 1970 年 1 月 1 日开始计算日期?”
A)It had to be as recent as possible, yet include somepast. There was most likely no significant other reason as a lot of people feel that same way.
A)它必须尽可能最近,但包括一些过去。很可能没有其他重要的原因,因为很多人都有同样的感觉。
They knew it posed a problem if they placed it too far into the past and they knew it gave negative results if it was in the future. There was no need to go deeper in the past as events will most likely take place in the future.
他们知道如果把它放在过去太远会带来问题,他们知道如果放在未来会产生负面结果。没有必要深入过去,因为事件很可能会在未来发生。
Notes:The mayans, on the other hand, had the need to place events into the past, since the had the knowledge of a lot of past, for which they made a long-term calender. Just to place all the routine phenomena on the calender.
笔记:另一方面,玛雅人需要将事件置于过去,因为他们了解很多过去,为此他们制作了一个长期日历。只是为了把所有的日常现象都放在日历上。
The timestamp was not meant as a calender, it's an Epoch. And I believe the mayans made their long-term calender using that same perspective. (meaning they knew damn well they didn't have any relations with the past, they just had the need to see it in a bigger scale)
时间戳不是日历,它是一个纪元。我相信玛雅人使用同样的观点制作了他们的长期日历。(这意味着他们非常清楚他们与过去没有任何关系,他们只是需要在更大的范围内看到它)
回答by Friyank
why its always 1st jan 1970 , Because - '1st January 1970' usually called as "epoch date" is the date when the time started for Unix computers, and that timestamp is marked as '0'. Any time since that date is calculated based on the number of seconds elapsed. In simpler words... the timestamp of any date will be difference in seconds between that date and '1st January 1970' The time stamp is just a integer which started from number '0' on 'Midnight 1st January 1970' and goes on incrementing by '1' as each second pass For conversion of UNIX timestamps to readable dates PHP and other open source languages provides built in functions.
为什么它总是 1st jan 1970 ,因为 - '1st January 1970' 通常称为“纪元日期”是 Unix 计算机的时间开始日期,并且该时间戳记标记为“0”。自该日期以来的任何时间都是根据经过的秒数计算的。简而言之......任何日期的时间戳将是该日期与“1970 年 1 月 1 日”之间的秒数差异时间戳只是一个整数,它从“1970 年 1 月 1 日午夜”的数字“0”开始并继续递增by '1' as each second pass 为了将 UNIX 时间戳转换为可读日期 PHP 和其他开源语言提供了内置函数。
回答by Basil Bourque
using date(January 1st, 1970) as default standard
使用日期(1970 年 1 月 1 日)作为默认标准
The Question makes two false assumptions:
这个问题做出了两个错误的假设:
- All time-tracking in computing is done as a count-since-1970.
- Such tracking is standard.
- 计算中的所有时间跟踪都是作为 1970 年以来的计数完成的。
- 这种跟踪是标准的。
Two Dozen Epochs
两打时代
Time in computing is not alwaystracked from the beginning of 1970 UTC. While that epoch referenceis popular, various computing environments over the decades have used at least nearly two dozen epochs. Some are from other centuries. They range from year 0 (zero) to 2001.
计算中的时间并不总是从 1970 UTC开始跟踪。虽然该epoch 参考很流行,但几十年来的各种计算环境至少使用了近两打epoch。有些来自其他世纪。它们的范围从 0 年(零)到 2001 年。
Here are a few.
这里有一些。
January 0, 1 BC
January 1, AD 1
October 15, 1582
January 1, 1601
December 31, 1840
November 17, 1858
December 30, 1899
December 31, 1899
January 1, 1900
January 1, 1904
December 31, 1967
January 1, 1980
January 6, 1980
January 1, 2000
January 1, 2001
公元前 1 年 1 月 0 日
公元 1 年 1 月 1 日
1582 年 10 月 15 日
1601 年 1 月 1 日
1840 年 12 月 31 日
1858 年 11 月 17 日
1899 年 12 月 30 日
1899 年 12 月 31 日
1900 年 1 月 1 日
1904 年 1 月 1 日
1967 年 12 月 31 日
1980 年 1 月 1 日
1980 年 1 月 6 日
2000 年 1 月 1 日
2001 年 1 月 1 日
Unix Epoch Common, But Not Dominant
Unix Epoch 普遍但不占主导地位
The beginning of 1970 is popular, probably because of its use by Unix. But by no means is that dominant. For example:
1970 年初很流行,可能是因为它被 Unix 使用。但绝不是占主导地位。例如:
- Countless millions (billions?) of Microsoft Excel & Lotus 1-2-3 documents use
January 0, 1900
(December 31, 1899). - The world now has over a billion iOS/OS X devicesusing the Cocoa (NSDate) epochof
1 January 2001, GMT
. - The GPSsatellite navigation system uses
January 6, 1980
while the European alternative Galileouses22 August 1999
.
- 无数(数十亿?)Microsoft Excel 和 Lotus 1-2-3 文档的使用
January 0, 1900
(1899 年 12 月 31 日)。 - 现在世界上有超过十亿的iOS / OS X设备使用可可(NSDate的)划时代的
1 January 2001, GMT
。 - 在GPS卫星导航系统的使用
January 6, 1980
而欧洲替代伽利略用途22 August 1999
。
ISO 8601
ISO 8601
Assuming a count-since-epoch is using the Unix epoch is opening a big vulnerability for bugs. Such a count is impossible for a human to instantly decipher, so errors or issues won't be easily flagged when debugging and logging. Another problem is the ambiguity of granularity explained below.
假设一个自 epoch 使用 Unix epoch 的计数正在为错误打开一个大漏洞。人类不可能立即破译这样的计数,因此在调试和记录时不会轻易标记错误或问题。另一个问题是下面解释的粒度的模糊性。
I strongly suggest instead serializing date-time values as unambiguous ISO 8601strings for data interchange rather than an integer count-since-epoch: YYYY-MM-DDTHH:MM:SS.SSSZ
such as 2014-10-14T16:32:41.018Z
.
我强烈建议将日期时间值序列化为用于数据交换的明确ISO 8601字符串而不是整数计数,YYYY-MM-DDTHH:MM:SS.SSSZ
例如2014-10-14T16:32:41.018Z
.
Count Of WhatSince Epoch
算什么,自纪元
Another issue with count-since-epoch time tracking is the time unit, with at least four levels of resolution commonly used.
计数自纪元时间跟踪的另一个问题是时间单位,通常使用至少四个级别的分辨率。
- Seconds
The original Unix facilities used whole seconds, leading to the Year 2038 Problemwhen we reach the limit of seconds since 1970 if stored as a 32-bit integer. - Milliseconds
Used by older Java libraries, including the bundled java.util.Date class and the Joda-Timelibrary. - Microseconds
Used by databases such as Postgres. - Nanoseconds
Used by the new java.time packagein Java 8.
- 秒
最初的 Unix 工具使用整秒,如果存储为 32 位整数,则当我们达到自 1970 年以来的秒数限制时,就会导致2038 年问题。 - 毫秒
由较旧的 Java 库使用,包括捆绑的 java.util.Date 类和Joda-Time库。 - 微秒
的数据库,如使用的Postgres。 - 纳秒
由Java 8 中的新java.time 包使用。