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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.