[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: perl leap year function?



Hi folks,

> > Is there one available in any of the perl modules? If not I could just
> > hardcode the next few into my script...

isn't that essentially why we've got a Y2K problem, because nobody
thought those old COBOL hacks would last mor that 5 years... and that
was 30 years ago.  Seriously: Write a subroutine to do this for you.
In a few years you will ge glad. 
Here's my version; the leapyear formula comes straight out of
Kerninghan and Ritchies "The C Programming language".
You may want to add some error checking, but it should do the job.

--------------->8-------------------->8-------------------------
#!/usr/bin/perl -Tw

$thisyear = 1999;

if ( &isLeapYear($thisyear) ) {
    print $thisyear, " is a leapyear.\n";
} else {
    print $thisyear, " is a not leapyear.\n";
}

# compute if argument is a leap year,
# return 1 if leapyear, 0 if not.
sub isLeapYear
{
    my $year = $_[0];
    if ( (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400) == 0)  {
	return 1;
    } 
    return 0;
}
---------------8<--------------------8<-------------------------
So long -- Stehan
-- 
Stephan Engelke                                    engelke@math.uni-hamburg.de
*** Microsoft leads to fear, fear leads to anger, anger leads to hate and
                  hate leads to suffering.  --  Master Yoda (more or less) ***


Reply to: