Daily bit(e) of C++ | std::chrono - representing dates
Daily bit(e) of C++ ♻️74, Representing a manipulating dates using the C++20 extension to std::chrono.
With C++20, the std::chrono library received a major update, introducing (among other things) support for representing dates.
An abstract (not necessarily valid) date can be represented using the std::chrono::year_month_day type, which supports arithmetic operations for years and months.
An abstract date can be normalized to a system time, which also supports day arithmetic operations.
System time truncated to second precision conceptually represents a date + time (std::chrono::sys_seconds), and truncated to day precision represents a date only (std::chrono::sys_days).
Finally, std::chrono::weekday can be constructed from a system time to obtain the weekday.