std::chrono::year::is_leap(3)
π 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 πΈ
trueif*thisrepresents 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 thenoexceptandconstexprare doing heavy lifting here β this whole thing is a compile-time vibe check on a year value, so you can flex on it in aconstexprcontext. π
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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms