4 ms·
The problem is not really with leap seconds themselves but that they are virtually universally implemented in computer systems as part of the "seconds-since-epo
by ambrop7 10y ago
The problem is not really with leap seconds themselves but that they are virtually universally implemented in computer systems as part of the "seconds-since-epoch time" as the lowest level interface to the clock (e.g. POSIX clock_gettime). This is apparently nice because then you can convert this to a calendar date without considering leap seconds (assuming the Gregorian calendar).
But for anything that actually needs accurate timekeeping (time keeping in natural terms not calendar time) this is a disaster because the time will jump by a full second, and if it jumped backward, this will also result in ambiguous times. So with leap seconds inserted into your clock, your clock may as well be worthless: it does not unambiguously define time, and you cannot subtract values to get durations.
Note that the current approach is not fully correct even when you care about calendar dates, because in case of a positive leap second, your clock will never show the :60 second tick but repeat :59 twice (due to ambiguity).
By the way, it is easy to convert a leap-second-uncorrected seconds-since-epoch value to a leap-second-corrected one (for compatibility with SW that assumes this). I think this will do it:
T_corrected = T - N_positive(T) + N_negative(T)
where N_positive is the number of positive leap seconds up to time T, and N_negative is the number of negative ones. (yes, positive ones are subtracted and negative are added - I hope I didn't mess that up)
Summary: leap seconds are not wrong per se, but they are implemented in a very stupid manner in common operating systems - the time values the OS gives allow neither correct conversion to calendar time nor calculation of relative times.
- hetman 10y agoThis is why clock_gettime() allows you to specify which clock to use. According to POSIX, CLOCK_MONOTONIC is not supposed to have any sudden jumps due to time adjustments or leap seconds, but can be adjusted for the gradual drift the clock experiences with respect to real world time. It seems perfect for your use case (if you don't want the skew adjustment there's also CLOCK_MONOTONIC_RAW, but since there is no skew adjustment it's only reasonably accurate for events that are close together).
- ambrop7 10y agoCLOCK_MONOTONIC is good, but it's a local clock without a defined reference. So you can't use it on the network to synchronize different computers or otherwise communicate absolute timestamps. There should be a CLOCK_REALTIME_RAW or something that gives you the TAI time (UTC without correction for leap seconds).
- hetman 10y agoAh, I see what you mean. A standard API for synchronising non-local events would definitely make life easier.
- bonzini 10y agoLinux has CLOCK_TAI.
- ambrop7 10y agoThat's great to know. I had no idea since it's not mentioned in the man page for clock_gettime (part of the "Linux Programmer's Manual"). Apparently the man pages are out of date.