Compute the number of SI seconds between “2021-01-01 12:56:23.423 UTC” and “2001-01-01 00:00:00.000 UTC” as example.
Which libraries can count seconds correctly and for which dates?
157 Views Asked by user894319twitter At
2
There are 2 best solutions below
0
On
My library Time4J can realize this task in Java language. Example using your input:
ChronoFormatter<Moment> f =
ChronoFormatter.ofMomentPattern(
"uuuu-MM-dd HH:mm:ss.SSS z",
PatternType.CLDR,
Locale.ROOT,
ZonalOffset.UTC);
Moment m1 = f.parse("2001-01-01 00:00:00.000 UTC");
Moment m2 = f.parse("2021-01-01 12:56:23.423 UTC");
MachineTime<TimeUnit> durationPOSIX = MachineTime.ON_POSIX_SCALE.between(m1, m2);
MachineTime<SI> durationSI = MachineTime.ON_UTC_SCALE.between(m1, m2);
System.out.println("POSIX: " + durationPOSIX); // without leap seconds
// output: 631198583.423000000s [POSIX]
System.out.println("SI: " + durationSI); // inclusive 5 leap seconds
// output: 631198588.423000000s [UTC]
// if you only want to count full seconds...
System.out.println(SI.SECONDS.between(m1, m2));
// output: 631198588
// first leap second after 2001
System.out.println(m1.with(Moment.nextLeapSecond()));
// output: 2005-12-31T23:59:60Z
The result of 5 seconds difference is in agreement with officially listed leap seconds between the years 2001 and 2021.
In general: This library supports leap seconds only since 1972.
C++20 can do it with the following syntax:
This outputs:
The first number includes leap seconds, and is 5s greater than the second number which does not.
This C++20 chrono preview library can do it in C++11/14/17. One just has to
#include "date/tz.h", change theysuffix to_yin two places, and addusing namespace date;.