design rationale for the chrono library
play

Design Rationale for the <chrono> Library Howard Hinnant - PowerPoint PPT Presentation

Design Rationale for the <chrono> Library Howard Hinnant Ripple Meeting C++ 2019 Structure of <chrono> Durations: Introduced in C++11 These six durations represent the hours convenient high- minutes level access.


  1. Design Rationale for the <chrono> Library Howard Hinnant Ripple Meeting C++ 2019

  2. Structure of <chrono> • Durations: • Introduced in C++11 • These six durations represent the hours convenient high- minutes level access. seconds • lower-level access milliseconds is available to microseconds clients for creating any duration unit nanoseconds they need. • Durations are the heart of the <chrono> library.

  3. Structure of <chrono> • Introduced in C++11 duration

  4. Structure of <chrono> • Time points: • Introduced in C++11 duration time_point

  5. Structure of <chrono> • Clocks: • Introduced in C++11 duration time_point clocks

  6. Evolution of <chrono> • Coming in C++20 Calendrical types: calendar duration time_point clocks

  7. Evolution of <chrono> • Coming in C++20 Time zone management: calendar time zones duration time_point clocks

  8. Evolution of <chrono> • Coming in C++20 And more clocks: calendar time zones duration time_point clocks

  9. Evolution of <chrono> • Coming in C++20 Formatting and parsing: formatting and parsing calendar time zones duration time_point clocks C++20 provides a complete time handling library.

  10. chrono in C++20 • Everything talked about today, whether it is old types from C++11 (e.g. durations and time_points) or new types in C++20, has a streaming operator in C++20: cout << system_clock::now() << '\n'; • C++20 <chrono> becomes much easier to work with because you can easily print values out, even without knowing their type. auto t0 = steady_clock::now(); ... auto t1 = steady_clock::now(); cout << "That took " << t1-t0 << '\n'; // That took 657ns

  11. duration template<class Rep, class Period = ratio<1>> class duration; • duration represents a duration of time, and can come in any unit. • duration s are represented by an arithmetic type, or a class type emulating an arithmetic type. • int , long , double , safe<int> , etc. • duration::period is a compile-time fraction representing the time in seconds between each integral value stored in the duration. • <chrono> defines several convenience type aliases for common units.

  12. duration template<class Rep, class Period = ratio<1>> class duration; • <chrono> defines several convenience type aliases for common units. New in C++20 nanoseconds days microseconds weeks milliseconds months seconds years minutes hours

  13. duration template<class Rep, class Period = ratio<1>> class duration; • Clients can define any custom unit they want. using dsec = duration<double>; using frame_rate = duration<int, ratio<1, 60>>; using safe_ns = duration<safe_int<int64_t>, nano>;

  14. duration template<class Rep, class Period = ratio<1>> class duration; • Durations implicitly convert from coarse to fine: auto limit = 2h; milliseconds x = limit; // 7'200'000ms

  15. duration template<class Rep, class Period = ratio<1>> class duration; • Durations have a named conversion from fine to coarse: auto limit = 2h; milliseconds x = limit; // 7'200'000ms auto y = duration_cast<hours>(x); // 2h

  16. duration template<class Rep, class Period = ratio<1>> class duration; • If the destination is floating-point-based, converts implicitly auto limit = 2h; milliseconds x = limit; // 7'200'000ms auto y = duration_cast<hours>(x); // 2h duration<double> z = x; // 7'200.0s • Implicit truncation error is a compile-time error. • Round-o ff error is not a compile-time error.

  17. time_point template<class Clock, class Duration = typename Clock::duration> class time_point; • time_point represents a point in time. • time_point is a wrapper around a duration. • Same value, same representation, just a di ff erent meaning. • time_point o ff ers only a subset of arithmetic algebra so as to catch logic errors at compile-time.

  18. time_point template<class Clock, class Duration = typename Clock::duration> class time_point; • time_point o ff ers only a subset of arithmetic algebra so as to catch logic errors at compile-time. auto tp1 = system_clock::now(); // tp1 is a time_point auto tp2 = system_clock::now(); // tp2 is a time_point auto diff = tp2 - tp1; // diff is a duration auto sum = tp2 + tp1; // compile-time error

  19. time_point template<class Clock, class Duration = typename Clock::duration> class time_point; • time_point is templated on Clock to catch the error of mixing time_points from di ff erent clocks. auto tp1 = system_clock::now(); // tp1 is a time_point auto tp2 = steady_clock::now(); // tp2 is a time_point auto diff = tp2 - tp1; // compile-time error

  20. What is the difference between a time point and a date? Time points can have • Example time points: arbitrarily fine precision. • 2019-11-14 10:30:15 • 2019-11-14 10:30:15.123 • 2019-11-14 10:30:15.123456 • 2019-11-14 10:30:15.123456789

  21. What is the difference between a time point and a date? Time points can • Example time points: have arbitrarily • 2019-11-14 10:30:15 coarse precision. • 2019-11-14 10:30:15.123 • 2019-11-14 10:30:15.123456 • 2019-11-14 10:30:15.123456789 • 2019-11-14 10:30 • 2019-11-14 10

  22. What is the difference between a time point and a date? When the time point • Example time points: has a precision of a • 2019-11-14 10:30:15 day, we call it a date. • 2019-11-14 10:30:15.123 • 2019-11-14 10:30:15.123456 • 2019-11-14 10:30:15.123456789 • 2019-11-14 10:30 • 2019-11-14 10 • 2019-11-14

  23. What is the difference between a time point and a date? Each precision has a type in the chrono system. • Example time points: • 2019-11-14 10:30:15 time_point<system_clock, seconds> • 2019-11-14 10:30:15.123 time_point<system_clock, milliseconds> • 2019-11-14 10:30:15.123456 time_point<system_clock, microseconds> • 2019-11-14 10:30:15.123456789 time_point<system_clock, nanoseconds> • 2019-11-14 10:30 time_point<system_clock, minutes> • 2019-11-14 10 time_point<system_clock, hours> • 2019-11-14 time_point<system_clock, days>

  24. What is the difference between a time point and a date? sys_time<Duration> is a type alias for • Example time points: time_point<system_clock, Duration> • 2019-11-14 10:30:15 sys_time<seconds> • 2019-11-14 10:30:15.123 sys_time<milliseconds> • 2019-11-14 10:30:15.123456 sys_time<microseconds> • 2019-11-14 10:30:15.123456789 sys_time<nanoseconds> • 2019-11-14 10:30 sys_time<minutes> • 2019-11-14 10 sys_time<hours> • 2019-11-14 sys_time<days>

  25. What is the difference between a time point and a date? sys_time<Duration> is a type alias for • Example time points: time_point<system_clock, Duration> • 2019-11-14 10:30:15 sys_seconds • 2019-11-14 10:30:15.123 sys_time<milliseconds> • 2019-11-14 10:30:15.123456 Additional convenience sys_time<microseconds> type aliases • 2019-11-14 10:30:15.123456789 sys_time<nanoseconds> • 2019-11-14 10:30 sys_time<minutes> • 2019-11-14 10 sys_time<hours> • 2019-11-14 sys_days

  26. What is a calendar? • A calendar is a collection of dates, where each date has a unique name. Civil calendar 30.12.1969 31.12.1969 01.01.1970 02.01.1970 03.01.1970

  27. What is a calendar? • A calendar is a collection of dates, where each date has a unique name. Civil calendar Julian calendar 30.12.1969 17.12.1969 31.12.1969 18.12.1969 01.01.1970 19.12.1969 02.01.1970 20.12.1969 03.01.1970 21.12.1969 • Di ff erent calendars can refer to the same physical date, but have di ff erent names for that date.

  28. What is a calendar? • A calendar is a collection of dates, where each date has a unique name. Civil calendar sys_days 30.12.1969 -2 31.12.1969 -1 01.01.1970 0 02.01.1970 1 03.01.1970 2 • sys_days is a calendar too!

  29. Calendar Interoperability sys_days • sys_days is the canonical calendar in <chrono>.

  30. Calendar Interoperability civil calendar Julian calendar ISO Week-based year sys_days Islamic calendar Chinese calendar Hebrew calendar • sys_days is the canonical calendar in <chrono>. • As long as each calendar can convert to and from sys_days, then each calendar can convert to any other calendar.

  31. Calendar Interoperability civil calendar Julian calendar ISO Week-based year sys_days Islamic calendar Chinese calendar Hebrew calendar • Only these two calendars are in C++20 <chrono>. • Clients can write their own calendars. • I've written several of them as proof of concept.

  32. The civil calendar class year_month_day; data structure: {year, month, day} • year_month_day implicitly converts to and from sys_days , with no loss of information ( constexpr and noexcept ). • Constructible from a year , month and day . • Has year , month and day getters. • Equality and less-than comparable. • Does year and month-oriented arithmetic. • Does not do day-oriented arithmetic. sys_days does day- oriented arithmetic very e ffi ciently.

Recommend


More recommend