Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I determine if a given year is a leap year?

by Adam (Vicar)
on Sep 15, 2000 at 02:48 UTC ( #32583=perlquestion: print w/replies, xml ) Need Help??

Adam has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

How do I determine if a given year is a leap year?

Originally posted as a Categorized Question.

  • Comment on How do I determine if a given year is a leap year?

Replies are listed 'Best First'.
Re: Leap Year
by Agyeya (Hermit) on May 17, 2004 at 10:22 UTC
    The Date::Leapyear module will tell if a year is a (Gregorian) leap year:
    use Date::Leapyear; if ( isleap(yyyy) ) { ... }
    The function isleap(yyyy) returns 1 in a leap year, 0 otherwise.

      And the source code for Date::Leapyear reads:

      sub isleap { my ($year) = @_; return 1 if (( $year % 400 ) == 0 ); # 400's are leap return 0 if (( $year % 100 ) == 0 ); # Other centuries are not return 1 if (( $year % 4 ) == 0 ); # All other 4's are leap return 0; # Everything else is not }

      which is already a provided answer. The same semi-correct formula is also used in Astro::Time and DateTime. Unless someone goes to the touble of encoding the entire FAQ, stick with the original answer. I like CPAN as much as the next Monk, but some modules are wasteful.

      Updated: I do note that the DateTime::Calendar::* modules at least make some attempt at dealing with cultural and temporal variances ...


      If anyone needs me I'll be in the Angry Dome.
        Yup. This was probably a pretty wasteful module.
        --
        Tommy Butler, a.k.a. TOMMY
        
Re: Leap Year
by mojotoad (Monsignor) on Aug 12, 2003 at 06:41 UTC
    DateTime offers an is_leap_year method for datetime objects.

    They also provide leapsecond information via the DateTime::LeapSecond class, if you're into that.

    Matt

Re: Leap Year
by TheHobbit (Pilgrim) on Apr 17, 2002 at 16:08 UTC
    Well, may be... but your answer is false:).

    Let's be precise: nowadays, your answer is right, but only since Gregorian reform, which has been take into account at different times in differents countries. Before that, and since 45BC, there was a leap year in every year divisible by 4 (NOTE: 45BC is year -44).

    Even that isn't exatly true... At the beginning people did not understand what "once in 4 years" meant, and there was a period (between 45BC and 9BC) where there was a leap year every 3 years. Followed by a period (between 8BC and 8AD) where there was no leapyear at all.

    See the Calendar FAQ .


    TheHobbit
Re: Leap Year
by Sol-Invictus (Scribe) on Feb 06, 2004 at 08:23 UTC
    The exact dates when countries (that use the Western calendar) adopted the Gregorian version varies. See this section of the Calendar FAQ
Re: How do I determine if a given year is a leap year?
by DeadPoet (Scribe) on Jan 08, 2011 at 18:16 UTC

    Not my work, but a great example:

    sub IsLeapYear { my $year = shift; return 0 if $year % 4; return 1 if $year % 100; return 0 if $year % 400; return 1; }

    The IsLeapYear subroutine is called with a year number, like

    IsLeapYear(2006);

    The function returns a true value if the year number is a leap year, false otherwise.

    Get the correct number of days in February for the given year:

    my $days_in_February = IsLeapYear($year) ? 29 : 28;

      I must not have had sufficient coffee in my bloodstream when reading this, as the first few times I read through it I stumbled on overlooked the fact that % returns a false value when the year is appropriately divisible. Thanks to Corion for smacking me with a the clue-by-four of understanding.

      As moritz stated in the CB, think of it as foo() if (($a % X) == 0)

      Posted to hasten others to the "oh yeah" moment.

      --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://32583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-01-17 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (50 votes). Check out past polls.

    Notices?