Do Java system milliseconds take account of leap seconds?

前端 未结 5 669
情话喂你
情话喂你 2021-01-11 15:13

The java function System.currentTimeMillis() apparently returns the number of seconds since 1st January 1970. However, according to wikipedia.org/wiki/Leap_second,

相关标签:
5条回答
  • 2021-01-11 15:38

    One easy way to check if leap seconds are accounted for or not is to compute the number of seconds elapsed since the Epoch for 00:00 on any given day in the current year.

    If that number of seconds is congruent to 00 modulo 60, then the leap seconds are not accounted for as in 2013 you should have a modulus of 25 (to account for the past 25 leap seconds).

    0 讨论(0)
  • 2021-01-11 15:44

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

    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.

    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.

    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.

    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).

    0 讨论(0)
  • POSIX requires that the system clock not admit the existence of leap seconds. MS Windows cannot guarantee the quality (nor existence) of the system clock hardware, and it has eschewed guarantee of 1-second accuracy. Java cannot easily do anything that the underlying system refuses to do. The operating systems are hamstrung by the history of the international regulations that result in one IEEE standard (PTP) that requires leap seconds and another (POSIX) that denies them.

    0 讨论(0)
  • 2021-01-11 15:52

    Looking at the Javadoc for currentTimeMillis(), it referes to the documentation of the Date class, which has this to say:

    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.

    So to answer your question: Yes, leap seconds are accounted for.

    0 讨论(0)
  • 2021-01-11 15:55

    I did a small experiment on javarepl:

    java> new Date(1000L * 86400 * (365 * 4 + 1) * 12)
    java.util.Date res0 = Mon Jan 01 00:00:00 UTC 2018
    

    As you can see, a simple "naive" arithmetics that just regards leap year, but not leap seconds, is used. No extra seconds are added or subtracted.

    Update:

    Same for the new Instant class:

    java> Instant.ofEpochMilli(1000L * 86400 * (365 * 4 + 1) * 12)
    java.time.Instant res0 = 2018-01-01T00:00:00Z
    
    0 讨论(0)
提交回复
热议问题