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

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

πŸ’… std::chrono::year_month_weekday_last::ok

okay so bestie, welcome to the std::chrono...ay_last::ok(3) page β€” that's the C++ Standard Library manual, and today's main character is a little member function that just wants to know if your date is valid πŸŽ€


✨ NAME ✨

std::chrono::year_month_weekday_last::ok β€” std::chrono::year_month_weekday_last::ok (yes the name is the same twice, it's giving "I said what I said" 😌)


🌸 SYNOPSIS 🌸

constexpr bool ok() const noexcept;

(since C++20) ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

Bestie, this one's literally a vibe check for your date πŸ’… It checks if this object represents a valid date.

And here's the tea πŸ“: since a year_month_weekday_last represents the last weekday of a particular month, it's valid as long as the year, month and weekday are all valid. That's it, that's the whole check β€” all three have to pass their own vibe check or the whole thing flops 😭


πŸ”₯ RETURN VALUE πŸ”₯

year().ok() && month().ok() && weekday().ok()

It literally just ANDs all three together β€” year().ok(), month().ok(), and weekday().ok() β€” and returns that πŸ’– No cap, it's that simple.


πŸ’œ EXAMPLE πŸ’œ

Run this code πŸ‘‘
#include <cassert>
#include <chrono>
using namespace std::chrono;

int main()
{
    auto ymwdl{Tuesday[last]/11/2020};
    assert(ymwdl.ok());
    ymwdl = Tuesday[last]/-2/2021;
    assert(!ymwdl.ok());
    ymwdl += months(0); // Normalize month
    assert(ymwdl.ok());
}

So the way this works πŸ‘€ β€” Tuesday[last]/11/2020 is a whole mood and passes the vibe check βœ…. Then Tuesday[last]/-2/2021 with that cursed negative month? Not ok, the assert fires, iconic behaviour honestly πŸ’€. Then ymwdl += months(0); normalizes the month and suddenly we're back to valid again. Character development 🧚✨


source: http://cppreference.com Β· version/date: 2024.06.10

ok that's the whole page bestie, mwah πŸ’‹ β€” stay valid, stay normalized, and remember: assert(ymwdl.ok()) is self care πŸ«ΆπŸ’–πŸŽ€πŸŒˆβ­

πŸ“¦ where this page comes from