7

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, that is, negative leap seconds. (Admittedly, since 1972 there only have been positive leap seconds; but it seems that in the last few years the drift has reversed, and a negative leap second seems possible in the upcoming years.)

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.

Is it known why std::chrono doesn't support this possibility? Was this an optimization, or a mistake of some sort? 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.

ruakh
  • 175,680
  • 26
  • 273
  • 307
sh-
  • 941
  • 6
  • 13
  • While a good topic for discussion, this isn't really the right place for such discussions. Please take some time to refresh ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). – Some programmer dude Apr 25 '22 at 11:47

1 Answers1

10

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

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 3
    FWIW, you can get live drafts of all the modern standards here: https://github.com/timsong-cpp/cppwp – NathanOliver Apr 25 '22 at 12:06
  • 1
    Indeed, `std::chrono::leap_second` seems to have grown a further function member in C++23: `constexpr seconds value() const noexcept;` This allows the distinction between positive and negative leap seconds that was missing before. – sh- Apr 25 '22 at 12:11
  • 1
    @NathanOliver's link to Tim Song's repository of all modern standards indicates that `leap_second::value()`, with its reference to negative leap seconds, was present in C++20: https://timsong-cpp.github.io/cppwp/n4861/time.zone.leap#members-2 Clear language for negative leap seconds was introduced via LWG issue 3359 prior to and accepted in time for C++20: https://cplusplus.github.io/LWG/issue3359 – Howard Hinnant Apr 26 '22 at 01:47
  • @Howard Hinnant: Thanks for this! I was checking against n4800, and `value()` isn't in there, so it must have been a very late addition to C++20. – sh- Apr 26 '22 at 13:21