How can a 1 year old (java) lib correctly perform an UTC Time formatting, considering a newly introduced leap second
date, gps, java, time
Solution
Java and the Unix "epoch" (number of seconds since Jan 1, 1970 00:00:00 UTC) both ignore leap seconds entirely. They both assume every day (measured in UTC) has had exactly 86400 seconds. A simple block of code to verify:
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("UTC"));
c.set(2014, 0, 1, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
System.out.println(c.getTimeInMillis());
You will see that the number of seconds from 1/1/1970 to 1/1/2014 is an exact multiple of 86400 (it's actually exactly 44 years * 365.25 days/year * 86400 seconds/day); it shouldn't be, because there have been 25 leap seconds introduced in that interval.
If you need to take leap seconds into account, you need to find a library that will do so, or come up with your own adjustment.
Problem
A timestamp expressed in milliseconds since 1.1.1970 UTC is a common way to store timestamps, e.g in Java. e.g: ``` long timestampUtc = System.currentTimeMillis(); ``` Such a timestamp can be formated in human readle time format, e.g using this code ``` SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); df.setTimeZone(TimeZone.getTimeZone("UTC")); String humanTimeUtc = df.format(new Date(timestampUtc)); System.out.println(humanTimeUtc); ``` which gives output: `2014-02-14 14:58:05` Now imagine that today at midnight the time administration introduces a new UTC leap second. If I run the code above tommorow, the java JRE on my system cannot know that leap second introduction, and would format the time wrongly (by one second). Is my asumption correct? How to correctly format the time (e.g in a log file) in systems that cannot always use an up to date JRE?. Background info: This is used in an embedded device, which synchronizes its system clock via GPS, having the GPS number of leap seconds offset to UTC.