|
|
OverviewCheck the day of the specified month. If checkdate method of Date::Calc is used, it's easier. But in here, introduce a code.
Flow
A sample code
sub monthdaycheck {
my ($year,$month,$day) = @_;
my @monthdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
my $md = $monthdays[$month-1];
++$md if ($month == 2 and $year % 4 == 0 and ($year % 100 != 0 or $year % 400 == 0));
return ($day <= $md and $day >= 1 and $year >= 1);
}
Description of the code
sub monthdaycheck {
my ($year,$month,$day) = @_;
Get year, month and day information. my @monthdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); Put day information into an array. my $md = $monthdays[$month-1]; Get the maximum day of the given month. ++$md if ($month == 2 and $year % 4 == 0 and ($year % 100 != 0 or $year % 400 == 0)); If it's a leap year, add 1. return ($day <= $md and $day >= 1 and $year >= 1); } If the given month is within that maximum day number, return 1, otherwise, return 0. |