How can a 1 year old (java) lib correctly perform an UTC Time formatting, considering a newly introduced leap second

后端 未结 5 1784

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.currentTim         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-11 10:51

    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.

提交回复
热议问题