Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

leap year

Copying eli and his hardcore test [meme]:
You are not a Hardcore LiveJournal user unless this is the second time you've been able to post on February 29th :-)

First time:
http://www.livejournal.com/users/brad/2000/02/29/
Second time:
Now. :P
Do people with birthdays on Feb. 29 celebrate them on Feb. 28 or Mar. 1 on normal years?

Do you know how to calculate whether it's a leap year or not? It's not just ever 4 years... It's ever 4 years, except every 100 years, except every 400 years. (which is why 2000 was a leap year). Here's the LJ code for it:

sub days_in_month
{   
    my ($month, $year) = @_;
    if ($month == 2)
    {               
        return 29 unless $year;  # assume largest
        if ($year % 4 == 0)
        {
          # years divisible by 400 are leap years
          return 29 if $year % 400 == 0;
    
          # if they're divisible by 100, they aren't.
          return 28 if $year % 100 == 0;

          # otherwise, if divisible by 4, they are.
          return 29;
        }
    }
    return ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$month-1]);
}
Tags: perl, tech
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 20 comments
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →
Previous
← Ctrl ← Alt
Next
Ctrl → Alt →