Calculating Whether a Year is a Leap Year


This isn't hard... but I can't ever remember the easy ways to do it...

If Date::Calc is close at hand:

    use Date::Calc (
    if (leap_year($year)) { ... }

Otherwise, the straightforward way is:

    sub leap_year {
        my $year = shift();
        return(1) if (($year % 400) == 0);
        return(0) if (($year % 100) == 0);
        return(1) if (($year % 4)   == 0);
        return(0);
    }


source: jwe
keywords: perl,code
date: 01/04/2005