Daily bit(e) of C++ | std::chrono — date manipulation
Daily bit(e) of C++ #74, The C++20 date manipulation using std::chrono
C++20 introduced a large extension to the std::chrono
library that added (among other things) full support for date representation and manipulation.
#include <chrono>
using namespace std::chrono;
// Day in a year can be specified using literals and operator/
auto christmas_eve = 2023y/December/24d;
// decltype(christmas_eve) == std::chrono::year_moth_day
auto day = weekday{sys_days{christmas_eve}};
// day == Sunday
for (auto date = 2023y/April/1d;
date.month() == April;
date = sys_days{date} + days{1}) {
// iterate over all days in April 2023
}