I have the following which I converted from C a long time ago when I was very new to Perl. It was set up for the UK so the Julian/Gregorian change is in 1752.
# ------
sub isLeap
# ------
{
my $year = shift
or croak "isleap(): no year supplied\n";
croak "isLeap(): year not numeric\n"
unless $year =~ /^\d+$/;
return 0 if $year % 4;
return 1 if $year < 1753;
return 1 if $year % 100;
return 1 unless $year % 400;
return 0;
}
# -------
sub valDate
# -------
{
my($year, $month, $day) = @_;
return 0 unless
$year =~ /^\d+$/
and $month =~ /^\d+$/
and $day =~ /^\d+$/;
my $daysinm = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]];
return 0 if $year < 1 or $year > 9999;
return 0 if $month < 1 or $month > 12;
return 0 if $day < 1 or
$day > $daysinm->[isLeap($year)]->[$month - 1];
return 0 if $year == 1752 and $month == 9 and
($day > 2 and $day < 14);
return 1;
}
It works for any year from 1 to 9999 (not y10k compliant I'm afraid ;-)
I hope this is of use.
Cheers,
JohnGG
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.