std::chrono::year::is_leap(3)

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

πŸ’– NAME πŸ’–

std::chrono::year::is_leap β€” okay so it's literally the function that tells you if a year is a leap year, bestie, and it lives in the C++ Standard Libary (that's the manual name, section 3, we stan a slightly misspelled icon πŸ’…)


✨ SYNOPSIS ✨

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

Okay so here's the vibe: this thing tells you whether *this represents a leap year in the proleptic Gregorian calendar. That's the calendar but going backwards forever, no cap. πŸ—“οΈ

And here's the tea on when *this counts as a leap year β€” it's a leap year if the stored year value:

  • is divisible by 4 and not divisible by 100; or
  • is divisible by 400.

That's it, that's the whole rule. Periodt. πŸ‘‘


🌸 RETURN VALUE 🌸

  • true if *this represents a leap year
  • otherwise false

No in-between, no maybe, no "it's complicated" β€” just a bool. 🫢


πŸŽ€ EXAMPLE πŸŽ€

// Run this code

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono_literals;
    for (const std::chrono::year y : {2020y, 2021y, 2000y, 3000y})
    {
        if (const int iy{static_cast<int>(y)}; y.is_leap())
            std::cout << iy << " is a leap year because it is divisible by "
                      << (iy % 400 == 0 ? "400\n" : "4 and not divisible by 100\n");
        else
            std::cout << iy << " is not a leap year\n";
    }
}

⭐ OUTPUT ⭐

2020 is a leap year because it is divisible by 4 and not divisible by 100
2021 is not a leap year
2000 is a leap year because it is divisible by 400
3000 is not a leap year

Like look at that 😭 2000 said "divisible by 400, main character energy" and 3000 said "divisible by 100 but not 400, not today." Iconic behaviour honestly. πŸ”₯


ngl the noexcept and constexpr are doing heavy lifting here β€” this whole thing is a compile-time vibe check on a year value, so you can flex on it in a constexpr context. πŸ’€

okay that's the whole page bestie, go check your leap years πŸ’–

this has been http://cppreference.com, version 2024.06.10, page std::chrono::year::is_leap(3) β€” mwah, love u, bye πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from