liveBook · Manning

Year_month_day

As explained in Learn C++ by Example

year_month_day is a C++ chrono calendar type representing a calendar date composed of a year, month, and day.

std::chrono::year_month_day represents a calendar date as a year, month, and day. Its whole-value components help the compiler catch swapped or incorrectly typed arguments early. section 4.1

Create New Year's Eve

auto new_years_eve = 2022y / December / 31; creates a readable date using chrono literals and operator/. The explicit form year_month_day(year(2022), month(12), day(31)) expresses the same values. section 4.2.3

Use year_month_day with time points when a calendar date must participate in a time calculation:

  • Convert it to a day-based system time with sys_days before subtracting it from another time point. section 4.1
  • Floor the current time point to days before extracting its year, preserving the calendar-date boundary. section 4.2.5
  • Read the year, month, and day fields through the type's accessors. section 4.5
std::chrono::time_point now = std::chrono::system_clock::now();

Pair the current system_clock time point with a constructed date to build a countdown to a calendar event. section 4.1

How Learn C++ by Example covers it

How deep it goes
Defines typed construction, readable literals, field access, and time-point conversion, while deferring broader date formatting and parsing.
Examples
Demonstrates New Year's Eve construction and counts days remaining until the last day of the current year.
What it assumes
Assumes basic time_point and duration usage, while introducing chrono calendar types and date-specific construction from typed components.
What you can do after
Apply it via C++20 chrono to construct typed dates, extract calendar fields, and calculate durations to calendar events.
How it connects
Connects calendar dates with time_point, duration, system_clock, sys_days, chrono literals, and operator/.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.