Java – handle leap seconds correctly

Before and during leap seconds, it seems that calling new date() will return 23:59:59 twice (once before leap seconds and once during leap seconds), instead of 23:59:59 and 23:59:60

Is there any way (to implement the NTP client in the application, or check the clock back or repeat itself) to determine whether the given second is a leap second, so as to correctly present 23:59:60 to the user?

In this regard, does the host operating system have any hooks to determine whether it is before or after leap seconds?

Solution

Let's take a look at Java util. Source code of date:

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

So the question can be interpreted as:

System. Currenttimemillis() produces leap second values of 60 or even 61 (because the specification pretends)?

Strictly speaking, it depends on the underlying operating system But the truth is: all well-known operating systems, such as windows, Linux, apple and Android, are ignorant of leap seconds On the contrary, these operating systems can perform any clock operation at any time (return, synchronization with NTP server...) Therefore, you will not observe leap seconds using the date - API By the way, the value 61 is impossible because the UTC standard requires that UTC never deviates from UT1 by more than 0.9 seconds. As a result, double leap seconds will not be inserted

The origin of "61" is only a serious misunderstanding of the early POSIX specification (this error has now been corrected) Unfortunately, the old Java specification has not been corrected and has not been misunderstood until now

About java-8:

Yes, the so-called "java time scale" is officially designated as utc-sls - based on the expired proposal, which is mainly aimed at the internal implementation of NTP server There is no implementation of UTC - SLS in the real world Even java-8 does not implement utc-sls Two facts support this statement:

>Java-8 does not contain a leap second table (this will be the basis of any utc-sls Implementation) Threeten project originally held such a, but it has been deleted (now also removed from the backup). > From Java util. The conversion from date to instant is only 1:1 (see the source code - different precision millis and nanos are different) Please note that in terms of the so-called "java time scale", Java is not mentioned in the instant API util. Date.

In addition: Java util. Date originated in 1995 (when java was invented), but UTC - SLS was proposed a few years later

So what's left of java-8 to support leap seconds? Empty words in the specification cause a lot of confusion, nothing else

What else can you do? Not at all within the scope of JDK What you need is an external third party library with built - in leap - second data An example is my library time4j – see this article Another option might be its utcinstant like library threeten extra But I haven't tested its conversion to Java util. Date (seems suspicious?)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>