in reply to How do I determine if a given year is a leap year?

Not my work, but a great example:

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

The IsLeapYear subroutine is called with a year number, like

IsLeapYear(2006);

The function returns a true value if the year number is a leap year, false otherwise.

Get the correct number of days in February for the given year:

my $days_in_February = IsLeapYear($year) ? 29 : 28;

Replies are listed 'Best First'.
Re: Answer: How do I determine if a given year is a leap year?
by MidLifeXis (Monsignor) on Jan 10, 2011 at 14:45 UTC

    I must not have had sufficient coffee in my bloodstream when reading this, as the first few times I read through it I stumbled on overlooked the fact that % returns a false value when the year is appropriately divisible. Thanks to Corion for smacking me with a the clue-by-four of understanding.

    As moritz stated in the CB, think of it as foo() if (($a % X) == 0)

    Posted to hasten others to the "oh yeah" moment.

    --MidLifeXis