Calendar and Time Zones
Use C++20 calendar types.
C++20 Calendar Types
C++20 added rich calendar support to <chrono>. You can now express years, months, and days directly as types rather than juggling integers.
year,month,dayyear_month_dayfor a full date
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
year y{2026};
std::cout << "Year value: " << static_cast<int>(y) << '\n';
return 0;
}Calendar Literals
Calendar literals like 2026y and 15d plus named months such as std::chrono::May make dates read naturally.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto y = 2026y;
auto d = 15d;
std::cout << static_cast<int>(y) << " / " << static_cast<unsigned>(d) << '\n';
return 0;
}All lessons in this course
- Durations and Clocks
- Time Points
- Measuring Code Performance
- Calendar and Time Zones