std::chrono::year::is_leap
De cppreference.com
constexpr bool is_leap() const noexcept;
|
(desde C++20) | |
Determina si *this representa un año bisiesto en el Calendario gregoriano.
*this representa un año bisiesto si el valor del año almacenado
- es divisible por 4 y no es divisible por 100; o
- es divisible por 400.
Valor de retorno
true si *this representa un año bisiesto, false de lo contrario.
Ejemplo
Ejecuta este código
#include <iostream>
#include <chrono>
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 << " es un año bisiesto porque es divisible por "
<< (iy % 400 == 0 ? "400\n" : "4 y no es divisible por 100\n");
} else {
std::cout << iy << " no es un año bisiesto\n";
}
}
}
Salida:
2020 es un año bisiesto porque es divisible por 4 y no es divisible por 100
2021 no es un año bisiesto
2000 es un año bisiesto porque es divisible por 400
3000 no es un año bisiesto