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

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

✨ NAME ✨

hey bestie πŸ’– welcome to the std::chrono...kday::month,(3) page from the C++ Standard Libary manual (and yeah, the header literally spells it β€œLibary” β€” we’re keeping that energy πŸ’€). this page is all about the accessors on std::chrono::year_month_weekday, and the title is a little chaotic but we move 🫢.

the NAME section is exactly this string, no edits, no cap:

std::chrono::year_month_weekday::year,std::chrono::year_month_weekday::month, - std::chrono::year_month_weekday::year,std::chrono::year_month_weekday::month,

so yeah β€” it lists year and month and then repeats itself. iconic behavior, honestly πŸ’….


🌸 Synopsis 🌸

okay so the synopsis starts with this floating line, and we are not gonna question it:

                            std::chrono::year_month_weekday::weekday_indexed

then it hits you with five constexpr / noexcept accessors, all since C++20 ✨. here’s the synopsis exactly as it appears (formatting chaos included, because we respect the lore 😌):

          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::weekday weekday() const noexcept;
       (3) (since C++20)
          constexpr unsigned index() const noexcept;
       (4) (since C++20)
          constexpr std::chrono::weekday_indexed weekday_indexed() const
       (5) (since C++20)
          noexcept;

so that’s:

  • (1) constexpr std::chrono::year year() const noexcept; since C++20
  • (2) constexpr std::chrono::month month() const noexcept; since C++20
  • (3) constexpr std::chrono::weekday weekday() const noexcept; since C++20
  • (4) constexpr unsigned index() const noexcept; since C++20
  • (5) constexpr std::chrono::weekday_indexed weekday_indexed() const noexcept; since C++20

they all retrieve the field values stored in your year_month_weekday object. simple, clean, slay πŸ’….


πŸŽ€ Return value πŸŽ€

here’s what each one hands back to you, bestie:

  1. returns the stored std::chrono::year value πŸ’–
  2. returns the stored std::chrono::month value 🌸
  3. returns the stored std::chrono::weekday value πŸ¦‹
  4. returns the stored weekday index πŸ”’
  5. returns weekday()[index()] β€” literally that expression, no cap πŸ’…

πŸ’– Example πŸ’–

the example starts with the classic // Run this code and then this whole little main character moment:

        #include <cassert>
        #include <chrono>

        int main()
        {
            constexpr auto ym{std::chrono::year(2021)/std::chrono::January};
            constexpr auto wdi{std::chrono::Wednesday[1]};
            auto ymwdi{ym/wdi};
            const auto index{ymwdi.index() + 1};
            auto weekday{ymwdi.weekday() + std::chrono::days(1)};
            ymwdi = {ymwdi.year()/ymwdi.month()/weekday[index]};
            // Second Thursday in January, 2021
            assert(std::chrono::year_month_day{ymwdi} ==
       std::chrono::January/14/2021);
        }

it builds a year_month_weekday, does some index + weekday math, and asserts it’s the second Thursday in January 2021 β€” obsessed πŸŽ€βœ¨.


source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::chrono...kday::month,(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from