Handle leap seconds correctly

Before and during the second jump, it seems that the call new Date()

will return 23:59:59 twice (once before the jump per second and once during the second jump), rather than 23:59:59 and 23:59 :. 60

Is there a way (without running the NTP client in the app or checking to see if the clock goes back or repeats) to determine if a given second is a second or not to correctly present 23:59:60 to the user?

For that matter, are there any interceptors on the host operating systems to determine if this is a pre-post or post-post second?

+3


source to share


2 answers


Check out the source code java.util.Date

:

public Date() {
    this(System.currentTimeMillis());
}

      

Thus, the question can be read as:

Would the System.currentTimeMillis()

second jump value give 60 or even 61 (as spec pretends)?

The answer is, strictly speaking: depends on the underlying operating system. But the fact is that all well-known operating systems like Windows, Linux, Apple, Android don't know about seconds. Instead, these operating systems perform any manipulation of the clock at any time (setting, etc., Synchronizing with an NTP server ...). This way you won't see a second jump using the Date

-API.
... By the way, 61 is not possible because UTC standard requires UTC to never deviate from UT1 by more than 0.9 seconds with no double hop inserted as a result.

The origin of "61" is just a gross misunderstanding from the early POSIX specifications (where this bug has been fixed now). Unfortunately, the old Java spec hasn't been fixed yet, causing misunderstandings to this day.

About Java-8:



Yes, the so-called "Java Timeline" is formally defined as UTC-SLS - based on an expired proposal whose intention was more towards internal implementations of NTP servers. There is no real world implementation of UTC-SLS. Even Java-8 doesn't implement UTC-SLS . This is confirmed by two facts:

  • Java-8 does not contain a second level table (which would be the backbone of any UTC-SLS implementation). The Threeten project was originally run this way, but it has been removed (now also removed from the backport ).

  • The conversion from java.util.Date

    to Instant

    is only 1: 1 (see source code - leaving aside different millis versus nano precisions). Note that java.util.Date

    there is no mention in the API Instant

    regarding the so called "Java timeline".

The Java timeline is used for all time classes. This includes Instant, LocalDate, LocalTime, OffsetDateTime, ZonedDateTime, and Duration.

Next up: The origins java.util.Date

date back to 1995 (when Java was invented), but UTC-SLS was proposed a few years later.

So what's left in Java-8 as second level support? Empty specs causing a lot of confusion, nothing else.

What else can you do? Within the JDK, there is simply nothing. You need an external third party library with inline second level data. An example is my Time4J library - see article . Another option could be the Threeten-Extra library with its UTCInstant class . But I haven't tested converting it to java.util.Date

(looks suspicious?).

+5


source


According to the Java8 documentation for the Date class, calling Date within the leap second will return / correctly: 60 or: 61.

So you don't have to do anything.



How it does this is a bit of a mystery, since the base Instant class propagates a second jump in the last 1000 seconds of the day - so each of those last seconds will actually be 1.001 seconds - and your application won't know if it will be in a second or not ...

+2


source







All Articles