std::chrono::year::ok(3)

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

πŸ’– std::chrono::year::ok πŸ’–

okay so this page comes from the C++ Standard Libary manual (yes, that spelling, no cap), and the whole vibe is checking whether a std::chrono::year is valid. It’s giving tiny validity-checker main character energy. πŸ’…


✨ NAME ✨

std::chrono::year::ok β€” literally std::chrono::year::ok. That’s the name, bestie, we’re not doing extra.


🌸 Synopsis 🌸

constexpr bool ok() const noexcept;

✨ since C++20 ✨

This checks if the year value stored in *this is in the valid range, i.e. [-32767, 32767]. That’s the whole assignment, and it ate. πŸ”₯


πŸ’… Return value πŸ’…

It gives you true if the year value stored in *this is in the range [-32767, 32767]. Otherwise? false. That’s it. No secret lore. 🫢


🧚 Possible implementation 🧚

See the implementations in libstdc++, libc++, and Howard Hinnant's date.h.

class Year
{
    short year_;   // exposition-only

public:

    bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }

    /*...*/
};

πŸ“ Example πŸ“

πŸŽ€ // Run this code β€” go off, bestie:

#include <chrono>
#include <iomanip>
#include <iostream>

int main()
{
    std::cout << "input year β”‚ internal value β”‚ ok()\n" <<
        std::boolalpha;

    for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000})
    {
        const std::chrono::year y{i};
        std::cout << std::setw(10) << i << " β”‚ "
                  << std::setw(14) << static_cast<int>(y) << " β”‚ "
                  << y.ok() << '\n';
    }
}

🌈 Possible output 🌈

fr fr it can print like this:

input year β”‚ internal value β”‚ ok()
      2020 β”‚           2020 β”‚ true
     32768 β”‚         -32768 β”‚ false
     32769 β”‚         -32767 β”‚ true
     65535 β”‚             -1 β”‚ true
     98304 β”‚         -32768 β”‚ false

okay that’s the page πŸ’– source/version + date check: http://cppreference.com, 2024.06.10, and the page is std::chrono::year::ok(3) β€” mwah πŸ’‹

πŸ“¦ where this page comes from