Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are time_of_day and hh_mm_ss two different types in C++20?

Tags:

c++

c++20

chrono

The C++20 standard defines 2 types that store a time during the day: chrono::hh_mm_ss and chrono::time_of_day. Both seem to store a duration since midnight, but because of DST-effects, callers should only use the hours, minutes, seconds and subseconds elements. www.cppreference.com gives exactly the same description for both types:

The class template ... splits a std::chrono::duration representing time since midnight into a "broken down" time such as hours:minutes:seconds, with the precision of the split determined by the Duration template parameter. ... It is primarily a formatting tool.

The only difference seems to be that chrono::time_of_day mentions 12-hour/24-hour formatting and chrono::hh_mm_ss doesn't.

In Howard Hinnant's GitHub library time_of_day is defined like this:

template <class Duration>
using time_of_day = hh_mm_ss<Duration>;

So why have 2 different types for this?

like image 918
Patrick Avatar asked Apr 28 '21 13:04

Patrick


1 Answers

https://en.cppreference.com/w/ is great, but not perfect. Ok, maybe it is. Where else can you get this kind of speed for fixes?! (note the second comment below this answer). :-)

Howard Hinnant's GitHub library originally had just time_of_day. And that was part of the proposal for C++20. During the standardization process, time_of_day was renamed to hh_mm_ss, and a few API adjustments were made.

Here is the paper that proposed these changes: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1466r3.html

Howard Hinnant's GitHub library "implemented" this change, but left a time_of_day type alias just for backwards compatibility with existing users of this library.

In short, there is no chrono::time_of_day, there is only chrono::hh_mm_ss: http://eel.is/c++draft/time.hms


Please note the very helpful comment below from Nicol Bolas.

like image 180
Howard Hinnant Avatar answered Sep 30 '22 03:09

Howard Hinnant