17

Are leap seconds catered for by the GregorianCalendar class?

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    What real-world application do you see for requiring leap seconds? Also, wouldn't it be really tough to account for them because of their infrequent addition? – Makoto Dec 28 '12 at 06:19
  • It shows the way to implement it: http://joda-time.sourceforge.net/faq.html#leapseconds – Azodious Dec 28 '12 at 06:19
  • 1
    It was more curiosity, but it could have implications for time critical operations like trading. Leap seconds are "official", so they could be added to time zone data. – Bohemian Dec 28 '12 at 06:44

2 Answers2

14
  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.
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • +1 for thorough first answer. Welcome to stackoverflow and good luck with your project! – Bohemian Jun 17 '13 at 04:48
  • Danke! I’m using that in our project now, works like a charm (i.e. correctly recognises that, for instance, `20150701_015960` was and `20150701_025960` wasn’t, a valid date in Europe/Berlin). The documentation is really sparse, though. – mirabilos Jul 06 '16 at 12:17
  • @mirabilos Bitte schön, about sparse doc, I am really graceful for any kind of suggestions, issues, questions, pull request for better documentation and so on. Time4J has so many features that it is not always easy to keep the doc on the same level, but I try my best. – Meno Hochschild Jul 06 '16 at 15:16
6

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 2
    A javadoc comment IMHO that deflects responsibility away from a poorly implemented class. If I add a second just before a leap second the time should advance 2 seconds, but I don't think it does. Besides, date arithmetic has nothing to do with current time. – Bohemian Dec 28 '12 at 06:49
  • 3
    @Bohemian I agree full with you. And Oracle knows also about this poor implementation, I think that's the reason why in java 8 we will have new Time/Date API that supports also leap seconds (http://jcp.org/aboutJava/communityprocess/edr/jsr310/guide-0.7.html) – Walery Strauch Dec 28 '12 at 07:03
  • +1 I thought/assumed that leap seconds were predictable. Good answer – Bohemian Dec 28 '12 at 21:32
  • @Evgeniy, what's with this answer? `Calendar` isn't `Date`. (`Date` was from JDK 1.0 among others.) – Pacerier May 25 '20 at 00:27