std::chrono...onth_day::ok(3)

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

🌸 std::chrono...onth_day::ok(3) 🌸

okay bestie, welcome to the C++ Standard Libary manual page for std::chrono...onth_day::ok(3) β€” yes, that manual name is literally spelled C++ Standard Libary on the page, and we are keeping it, no cap πŸ’…. This is the ok() vibe check for std::chrono::year_month_day, and it’s lowkey iconic.


✨ NAME ✨

std::chrono::year_month_day::ok - std::chrono::year_month_day::ok


πŸ¦‹ Synopsis πŸ¦‹

here’s the signature, bestie β€” it’s giving constexpr, noexcept, since-C++20 energy:

          constexpr bool ok() const noexcept;  (since C++20)

and what does it do? checks if this year_month_day object represents a valid calendar date. periodt πŸ—“οΈβœ¨


πŸ’– Return value πŸ’–

  • true if this year_month_day object represents a valid calendar date, that is, the stored year, month, and day values are all valid and the stored day value is within the number of days in the given year and month.
  • false otherwise.

πŸŽ€ Possible implementation πŸŽ€

not me showing you a possible implementation like it’s a fit check πŸ‘‘:

          constexpr bool std::chrono::year_month_day::ok() const noexcept
          {
              return year().ok() && month().ok() && day().ok() &&
                  day() <= (year()/month()/std::chrono::last).day();
          }

🌈 Example 🌈

this example is literally the whole plot: leap years, invalid dates, snap-to-last-day, overflow-to-next-month. // Run this code marker included, periodt:

       // Run this code

        #include <chrono>

        int main()
        {
            constexpr auto ymd1 {std::chrono::day(1)/std::chrono::July/2020};
            static_assert(ymd1.ok());

            constexpr auto ymd2 {std::chrono::year(2020)/7/42};
            static_assert(not ymd2.ok());

            constexpr auto ymd3 {std::chrono::February/29/2020}; // ok, leap
       year
            static_assert(ymd3.ok());

            constexpr auto ymd4 = ymd3 + std::chrono::years{1}; // bad, not a
       leap year
            static_assert(ymd4 == std::chrono::February/29/2021 and not
       ymd4.ok());

            // to fix the bad date we may want to snap to the last day of the
       month:
            if constexpr (!ymd4.ok())
            {
                constexpr auto ymd =
       ymd4.year()/ymd4.month()/std::chrono::last;
                static_assert(ymd == std::chrono::February/28/2021 and
       ymd.ok());
            }

            // or we may want to overflow to the next month:
            if constexpr (!ymd4.ok())
            {
                constexpr auto st =
       std::chrono::sys_time<std::chrono::days>{ymd4};
                constexpr auto ymd = std::chrono::year_month_day{st};
                static_assert(ymd == std::chrono::March/1/2021 and ymd.ok());
            }
        }

k bye bestie πŸ’– this cutie came from http://cppreference.com, dated 2024.06.10, with footer title std::chrono...onth_day::ok(3). Mwah πŸ’‹

πŸ“¦ where this page comes from