std::chrono...ar_month_day(3)
π NAME π
std::chrono::year_month_day β std::chrono::year_month_day
okay so welcome to the std::chrono...ar_month_day(3) page from the C++ Standard Libary (their spelling, not mine, and honestly? iconic π«Ά). We're talking about std::chrono::year_month_day, and bestie, it's giving calendar-core main character energy. obsessed. β¨
β¨ Synopsis β¨
Defined in header <chrono> β yes, that's the one, no cap.
class year_month_day; (since C++20)
okay so real talk: the class year_month_day represents a specific year, month, and day. It is a field-based time point, with a resolution of std::chrono::days β so the fields are literally the moment, iykyk. π
std::chrono::years- and std::chrono::months-oriented arithmetic are supported directly. no extra conversion era needed, the class handles that vibe.
There's also an implicit conversion to and from std::chrono::sys_days, and that allows std::chrono::days-oriented arithmetic to be performed efficiently. efficient era for real. β‘
year_month_day is a TriviallyCopyable StandardLayoutType. periodt. π
πΈ Member functions πΈ
the member-function roster, in order, no skips:
constructorβ constructs ayear_month_day(public member function) β¨operator+=β modifies the time point by some number of months or years πoperator-=β(public member function)(the table says it, we're not hiding it π«Ά)yearβ accesses the year, month, and day stored in this objectmonthβ(public member function)dayβ(public member function)operator sys_daysβ converts to astd::chrono::time_pointoperator local_daysβ(public member function)okβ checks if theyear_month_dayrepresents a valid date (public member function)
π¦ Nonmember functions π¦
operator==β compares twoyear_month_dayvaluesoperator<=>β(function)(C++20)operator+β adds or subtracts ayear_month_dayand some number of years or monthsoperator-β(function)(C++20)operator<<β outputs ayear_month_dayinto a stream (C++20) (function template)from_streamβ parses ayear_month_dayfrom a stream according to the provided format (C++20) (function template)
π Helper classes π
std::formatter<std::chrono::year_month_day>β formatting support foryear_month_day(C++20) (class template specialization)std::hash<std::chrono::year_month_day>β hash support forstd::chrono::year_month_day(C++26) (class template specialization)
π Example π
run this code, bestie:
// Run this code
#include <chrono>
#include <iostream>
int main()
{
const std::chrono::time_point now{std::chrono::system_clock::now()};
const std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(now)};
std::cout << "Current Year: " << static_cast<int>(ymd.year()) << ", "
"Month: " << static_cast<unsigned>(ymd.month()) << ", "
"Day: " << static_cast<unsigned>(ymd.day()) << "\n"
"ymd: " << ymd << '\n';
}
π Possible output π
Current Year: 2023, Month: 9, Day: 4
ymd: 2023-09-04
okay that's the page, bestie. It came from http://cppreference.com with date/version 2024.06.10, and the footer tag is std::chrono...ar_month_day(3). mwah, slay, bye ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms