Home > OS >  std::chrono and missing (?) support for negative leap seconds
std::chrono and missing (?) support for negative leap seconds

Time:04-26

C 20 added time zone support to std::chrono, and this includes leap seconds. However, it appears as if only leap second insertion was supported, not leap second removal (or negative leap seconds).

This is evident from the way the std::chrono::leap_second class is described - there's no way to tell positive from negative leap seconds.

Now, since 1972 there only have been positive leap seconds, so it may appear as if support for negative leap seconds was redundant. But this may be premature optimization, indeed it seems that in the last few years the drift has reversed, and a negative leap second seems possible in the upcoming years.

It is too early to tell for sure, but would it not have been prudent to include the possibility in std::chrono? Or is it supported already and I have missed something?

Note: The MSVC implementation seems to have a nonstandard feature that allows negative leap seconds.

CodePudding user response:

[time.zone.leap.members]/2 actually does specify negative leap seconds.

Returns: 1s to indicate a positive leap second or -1s to indicate a negative leap second. [Note 1: All leap seconds inserted up through 2019 were positive leap seconds. — end note]

When the standard speaks of insertion of a leap second, that leap second could be negative, which would be the same as a removal.

Note: I am unsure if this is new to C 23 or also in C 20, I don't have a copy of the C 20 draft with me right now.

CodePudding user response:

struct leap_second_info {
    bool is_leap_second;
    std::chrono::seconds elapsed;
};

is_leap_second means the time now is between 23:59:60 and 00:00:00 of the next day. This can only happen during insertion of a positive leap second. There is no special time period of the opposite operation. If a negative leap second is ever introduced, you will notice that it's 23:59:58, and the next second it's 00:00:00. You cannot be in 23:59:59-00.00.00 time interval that day.

elapsed can perfectly well be negative, even though this is very unlikely to ever happen.

  • Related