Are leap seconds catered for by Calendar?

2020-01-31 01:56发布

问题:

Are leap seconds catered for by the GregorianCalendar class?

If not, does any 3rd party library cater for it?

回答1:

java.util.Date API says that

"...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. ... Most computer clocks are not accurate enough to be able to reflect the leap-second distinction."

Wiki says

"Because the Earth's rotation speed varies in response to climatic and geological events, UTC leap seconds are irregularly spaced and unpredictable. Insertion of each UTC leap second is usually decided about six months in advance by the International Earth Rotation and Reference Systems Service (IERS)"

that is, no class can know about future IERS decisions.



回答2:

  1. Leap seconds are not catered for by GregorianCalendar (a view of the source code at Oracle shows this - explicit assumption of 1 minute = 60 seconds always is given there). And furthermore: Oracle has now officially denied leap second support - see Bug-ID 4272347.
  2. In Java there is no standard 3rd party library supporting leap seconds - not even joda-time. Only specialized software like this does that.
  3. Note that many libraries do talk about leap seconds although not supporting, for example java.util.Date (see Dorofeevs answer). Also JSR 310 talks a lot, but does not support this feature. Officially JSR 310 gives support to UTC-SLS which does not count leap seconds and only describes smearing rubber seconds around a leap second event. Indeed JSR 310 is very confusing about if it supports UNIX time or UTC-SLS (see next point). And since leap second information (github/threeten/issues/197) has been removed from JSR 310 code base it is absolutely impossible to implement true UTC leap seconds within the scope of JSR 310. In best case you might expect a coming external module (Threeten-Extra) as supplement for JSR 310 which will give rudimentary support at best (it is rather a translation between UNIX time and TAI time scale, not more and uses in my opinion a fundamentally wrong domain model).
  4. System.currentTimeMillis() officially relates to OS timer. And since all OS I know including Microsoft, Linux and Apple are only based on UNIX specification this java system timer does not count leap seconds, only the normal milliseconds since 1970-01-01T00:00:00.000Z
  5. Because of all these facts I have decided to set up my own date and time java library named Time4J which fully supports leap seconds and is available as v1.0 with LGPLv2.1-licence. A dzone-article demonstrates how this support looks like with version v4.2.


标签: java calendar