Is Gregorian Leap Year - parsicore/parsidate GitHub Wiki
is_gregorian_leap_year
Static Method Determines whether a given year is a leap year according to the rules of the Gregorian calendar.
Description
This static method checks if the provided year
qualifies as a leap year based on the standard Gregorian calendar algorithm. This is distinct from the Persian calendar leap year rules.
The Gregorian leap year rules implemented are:
- A year is a leap year if it is evenly divisible by 4.
- However, if a year is evenly divisible by 100, it is not a leap year...
- Unless that year is also evenly divisible by 400, in which case it is a leap year.
This method is useful for contexts where interactions or conversions between Persian and Gregorian dates occur.
Arguments
year
: The Gregorian calendar year (e.g.,1900
,2000
,2024
) as an integer type (e.g.,i32
) to be checked.
Returns
true
if the specifiedyear
is a leap year according to the Gregorian calendar rules.false
otherwise.
Note: This method performs a straightforward calculation based on divisibility rules and does not typically return errors.
Examples (Rust)
use parsidate::ParsiDate; // Assuming ParsiDate provides this static method
// --- Leap Year Cases ---
// Divisible by 400 -> Is a leap year
assert_eq!(ParsiDate::is_gregorian_leap_year(2000), true);
assert!(ParsiDate::is_gregorian_leap_year(1600));
// Divisible by 4 but not by 100 -> Is a leap year
assert_eq!(ParsiDate::is_gregorian_leap_year(2024), true);
assert!(ParsiDate::is_gregorian_leap_year(1996));
assert!(ParsiDate::is_gregorian_leap_year(2020));
// --- Common Year Cases ---
// Divisible by 100 but not by 400 -> Is NOT a leap year
assert_eq!(ParsiDate::is_gregorian_leap_year(1900), false);
assert!(!ParsiDate::is_gregorian_leap_year(1700));
assert!(!ParsiDate::is_gregorian_leap_year(1800));
assert!(!ParsiDate::is_gregorian_leap_year(2100));
// Not divisible by 4 -> Is NOT a leap year
assert_eq!(ParsiDate::is_gregorian_leap_year(2021), false);
assert!(!ParsiDate::is_gregorian_leap_year(2023));
assert!(!ParsiDate::is_gregorian_leap_year(1997));
// --- Edge Cases (if applicable, though rules are clear) ---
// Check year 0 or negative years if relevant to the type,
// although calendar years are typically positive.
// Assuming standard positive year input:
// assert_eq!(ParsiDate::is_gregorian_leap_year(4), true);
// assert_eq!(ParsiDate::is_gregorian_leap_year(100), false);