Java系统毫秒是否考虑到闰秒? [英] Do Java system milliseconds take account of leap seconds?

查看:625
本文介绍了Java系统毫秒是否考虑到闰秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java函数System。 currentTimeMillis ()显然返回自1970年1月1日以来的秒数。但是,根据 wikipedia.org/wiki/Leap_second ,自1972年以来已经有25次闰秒。这意味着自1970年1月1日以来的实际秒数比原始计算显示的还要多25次。系统。 currentTimeMillis ()执行天真的计算并忽略闰秒?

The java function System.currentTimeMillis() apparently returns the number of seconds since 1st January 1970. However, according to wikipedia.org/wiki/Leap_second, since 1972 there have been 25 leap seconds. This means the actual number of seconds since 1st January 1970 has been 25 more than a naive calculation would suggest. Does System.currentTimeMillis() do the naive calculation and ignore the leap seconds?

推荐答案

正式地,这取决于操作系统和实现 - 至少对于 Date 。从 java.util的文档日期

Officially, it's up to the OS and implementation - at least for Date. From the docs of java.util.Date:


尽管Date类旨在反映协调的世界时间(UTC) ,根据Java虚拟机的主机环境,它可能不会这样做。几乎所有现代操作系统都假设在所有情况下1天= 24×60×60 = 86400秒。然而,在UTC的时候,大概每一两年会有一秒钟的时间,叫做闰秒。闰秒总是作为一天的最后一秒,总是在12月31日或6月30日。例如,1995年的最后一分钟是61秒,由于增加了闰秒。大多数计算机时钟不够准确,无法反映出闰秒的区别。

Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

我怀疑你会发现,虽然你计算机时钟与UTC大致对齐,这是通过NTP等来定期更新时钟,而不是执行闰秒的OS

I suspect you'll find that although your computer clock is roughly aligned to UTC, that's done via NTP or the like correcting the clock periodically, rather than the OS really implementing leap seconds.

我相信JRE库通常是,假设是86400秒。这样使得生活变得更加简单,如果您要修正不正确的系统时钟,那么您也可以通过这种方式更正闰秒。

I believe the JRE libraries typically do assume the 86400-second day. It makes life so much simpler, and if you're going to correct for an inaccurate system clock anyway, you might as well correct for leap seconds that way too.

您真的想解决您感兴趣的内容。如果您需要一种表示使用闰秒的日期和时间的方法,那么标准Java库可能无法正常工作。即使 JSR-310 也不再支持闰秒可以告诉(对大多数开发者来说这是一个很明智的决定)。

You really want to work out what you're interested in. If you need a way of representing dates and times which use leap seconds, the standard Java libraries may not work well for you. Even JSR-310 no longer supports leap seconds as far as I can tell (which is a very sensible decision for most developers).

这篇关于Java系统毫秒是否考虑到闰秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆