Skip to content

Instantly share code, notes, and snippets.

@lestrrat
Created August 26, 2009 07:11
Show Gist options
  • Save lestrrat/175359 to your computer and use it in GitHub Desktop.
Save lestrrat/175359 to your computer and use it in GitHub Desktop.
my @last_days = (31, undef, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
sub is_leap_year {
my $year = shift;
return 0 if $year % 4;
return 1 if $year % 100;
return 0 if $year % 400;
return 1;
}
sub last_day {
my ($year, $month) = @_;
if ($month == 2) {
if (is_leap_year($year)) {
return 29;
} else {
return 28;
}
} else {
return $last_days[ $month - 1 ];
}
}
my @days = (
[ 1900, 2 ],
[ 2000, 2 ],
[ 2009, 8 ],
[ 2009, 9 ],
);
foreach my $set (@days) {
print "@$set -> ", last_day(@$set), "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment