std::chrono...ar_month_day(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 a year_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 object
  • month β€” (public member function)
  • day β€” (public member function)
  • operator sys_days β€” converts to a std::chrono::time_point
  • operator local_days β€” (public member function)
  • ok β€” checks if the year_month_day represents a valid date (public member function)

πŸ¦‹ Nonmember functions πŸ¦‹

  • operator== β€” compares two year_month_day values
  • operator<=> β€” (function) (C++20)
  • operator+ β€” adds or subtracts a year_month_day and some number of years or months
  • operator- β€” (function) (C++20)
  • operator<< β€” outputs a year_month_day into a stream (C++20) (function template)
  • from_stream β€” parses a year_month_day from a stream according to the provided format (C++20) (function template)

πŸŽ€ Helper classes πŸŽ€

  • std::formatter<std::chrono::year_month_day> β€” formatting support for year_month_day (C++20) (class template specialization)
  • std::hash<std::chrono::year_month_day> β€” hash support for std::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