1. Problem It Solves
C++20 chrono models civil dates, clock-based instants, and time-zone interpretation with distinct types instead of unlabelled integers. It makes an important assumption visible and checkable.
2. Prerequisites
Chrono durations, time points, and calendar basics.
You should be able to compile a short program and read its output.
3. Core Idea
A calendar date is a page label, a time point is a position on a clock timeline, and a time zone is the rulebook translating an instant for a region. Read std::chrono::year_month_day as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
std::chrono::year_month_day date{2026y, August, 30d};5. How It Works
The program introduces the smallest relevant form of
std::chrono::year_month_day.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
A civil date alone is not a unique instant, daylight-saving transitions can be ambiguous, and library time-zone database support may be incomplete.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when code needs typed dates, duration-safe conversion, or locale-aware interpretation supported by the platform.
Avoid it when a monotonic elapsed-time measurement only needs
steady_clock.
8. Simple Example
The example converts a fixed calendar date to sys_days; guarded code reports whether this libstdc++ exposes C++20 time-zone support. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/37_chrono_calendar_time_zones_clocks/main.cpp
// Day 37: Chrono Calendars, Time Zones, and Clock Conversion
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono;
year_month_day date{year{2026}, month{8}, day{30}};
sys_days instant{date};
std::cout << int(date.year()) << '-'
<< unsigned(date.month()) << '-'
<< unsigned(date.day()) << '\n';
std::cout << "days since Unix epoch = "
<< instant.time_since_epoch().count() << '\n';
#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
std::cout << "C++20 time-zone API advertised\n";
#else
std::cout << "C++20 time-zone database unavailable in this libstdc++\n";
#endif
}
9. Key Takeaways
std::chrono::year_month_dayexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
std::chrono::year_month_dayin the minimal example?Medium — What information is lost if a
year_month_dayis treated as though it already identified a local wall-clock instant?Hard — Why is converting between clock epochs different from choosing a time zone for a civil date?