zoukankan      html  css  js  c++  java
  • 为什么编程语言以及数据库要从1970年1月1日开始计算时

    今天在看Python  API时,看到time模块:


    The epoch is the point where the time starts. On January 1st of that year, at 0 hours,the “time since the epoch” is zero. For Unix, the epoch is 1970. To find out what the epoch is, look at gmtime(0).


    定义time197011日开始,忽然想到在JAVA里,Oracle数据库时间也是从1970
    11日开始计算。比如java类代码

    Date date = new Date(0);
    System.out.println(date);

    打印出来的结果:

    Thu Jan 01 08:00:00 CST 1970

    也是197011日,实际上时分秒是000(这里打印出来是8点,稍后会作解释)

    为什么这个时间会定义在197011日这个时候呢?

    于是开始了Google,中文网页根本找不到答案。于是试着搜索英文关键字,Sun java论坛总算找到准确的帖子:

    http://forums.sun.com/thread.jspa?threadID=595140&start=15

    其中有一个回复:


    I suspect that Java was born and raised on a UNIX system.
    UNIX considers the epoch (when did time begin) to be midnight, January 1, 1970.

    是说java起源于UNIX系统,而UNIX认为1970110点是时间纪元.

    但这依然没很好的解释"为什么",出于好奇,继续Google,总算找到了答案:

    http://en.wikipedia.org/wiki/Unix_time

    这里的解释是:

    最初计算机操作系统是32位,而时间也是用32位表示。

    System.out.println(Integer.MAX_VALUE);
    2147483647



    IntegerJAVA内用32位表示,因此32位能表示的最大值是2147483647。另外1365天的总秒数是31536000,

    2147483647/31536000 = 68.1

    也就是说32位能表示的最长时间是68年,而实际上到20380119031407
    秒,便会到达最大时间,过了这个时间点,所有32位操作系统时间便会变为
    10000000 00000000 00000000 00000000

    也就是19011213204552秒,这样便会出现时间回归的现象,很多软件便会运行异常了。

    到这里,我想问题的答案已经出来了:

    因为用32位来表示时间的最大间隔是68年,而最早出现的UNIX操作系统考虑到计算
    机产生的年代和应用的时限综合取了
    197011日作为UNIX TIME的纪元时间(开始
    时间
    ),而java自然也遵循了这一约束。

    至于时间回归的现象相信随着64为操作系统的产生逐渐得到解决,因为用64位操作
    系统可以表示到
    292,277,026,596124153008秒,

  • 相关阅读:
    Lambda+Stream替换集合中每个对象的指定字段值
    bootstrap table的属性sidePagination设置不当导致数据不显示
    fullcalendar从后台获取events报Uncaught TypeError: callback is not a function
    com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User
    js中复制功能的实现
    List列表排序报空指针异常
    springboot+tomcat不同环境采用不同配置文件
    装饰模式
    Ambari和大数据集群部署(精华)
    Ambari安装和汉化(转)
  • 原文地址:https://www.cnblogs.com/roboot/p/4820908.html
Copyright © 2011-2022 走看看