std::chrono..._day::month,(3)

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

✨ std::chrono::year_month_day::year, std::chrono::year_month_day::month, std::chrono::year_month_day::day ✨

okay bestie so this is a page from the C++ Standard Library manual πŸ’… (yeah, that std::chrono..._day::month,(3) thing at the top is just the header, we're not doing that whole bit again). sit down, hydrate, let's get into it 🫢


🌸 NAME 🌸

std::chrono::year_month_day::year, std::chrono::year_month_day::month, std::chrono::year_month_day::day β€” the three little accessor besties that pull the year, the month, and the day right back out of a year_month_day object πŸ’–


✨ Synopsis ✨

constexpr std::chrono::year year() const noexcept;   (1) (since C++20)
constexpr std::chrono::month month() const noexcept; (2) (since C++20)
constexpr std::chrono::day day() const noexcept;     (3) (since C++20)

so basically, these three are like the group chat of a year_month_day object β€” they retrieve the year, month and day values stored in this year_month_day object. periodt. πŸ’… Three separate functions, three separate receipts, all constexpr, all noexcept, all iconically C++20 and later πŸ‘‘


πŸŽ€ Return value πŸŽ€

  1. Returns the stored std::chrono::year value. πŸ—“οΈ (yes, that's the whole sentence, she's lowkey simple but she ATE)
  2. Returns the stored std::chrono::month value. 🌸
  3. Returns the stored std::chrono::day value. ⭐

like… each one gives you exactly the piece it says on the tin, no cap 🫢


πŸ¦‹ Example πŸ¦‹

// Run this code

#include <chrono>

int main()
{
    constexpr std::chrono::year_month_day ymd{std::chrono::July/1/2021};

    static_assert(ymd.year() == std::chrono::year(2021));
    static_assert(ymd.month() == std::chrono::month(7));
    static_assert(ymd.day() == std::chrono::day(1));
}

ok this example is literally the main character 🀩 you build a constexpr std::chrono::year_month_day out of std::chrono::July/1/2021, and then static_assert is out here doing a whole vibe check on all three getters β€” ymd.year() == std::chrono::year(2021), ymd.month() == std::chrono::month(7), ymd.day() == std::chrono::day(1). all three pass at compile time. slay πŸ’€βœ¨

ngl the static_asserts are the point here β€” they compile, or your program does not. no runtime drama, just pure compile-time confidence πŸ’œ

and that's the whole page, bestie! 🧚 source is http://cppreference.com, version date 2024.06.10, and the page itself is std::chrono..._day::month,(3). mwah πŸ’‹ go forth and get your year, month and day πŸ’–πŸ”₯

πŸ“¦ where this page comes from