Honor Society Cracks Invalid Date Bug

In 2007, the Honor Society of Leap Year Day Babies, engaged in two controversies related to leap day:

  1. Microsoft Corporations, makers of the popular Windows Operating system, and Office suite of applications started to pressure technology policy makers around the world to accept their version of the calender, in which the year 1900 is designated a Leap Year (if a year is divisible by 100, it is a Leap Year only if it is also divisible by 400).
  2. We found out that Toys R Us will not send birthday cards to children if they are born on Leap Day. Their computers aren't programmed properly, so they can't.

Being recognized champions of Leap Day awareness, we felt we had a duty to show computer programmers the 4 lines of code required to test for Leap Year.  Microsoft could use this code to determine that 1900 is not a Leap Year.  Toys R Us could use this code to deterimine that the current year is not a Leap Year, and send out the Feb 29 birthday cards on Feb 28th. In our opinion, there is no acceptable explanation to a child for not receiving a birthday card. Like computer programmers, children are not Leap Day conscious. If a computer programmer doesn't understand Leap Day, why would you expect a child to? That's why a child should receive a birthday card, regardless what date they are born, even if they're born on February 29th.

We circulated a press release about this, and The Register covered the story on Leap Day 2008.

The code, written in Perl, tells you how many days there are this year, in Leap Year there are 366 days, other years there are 365 days. The rule is that Leap Year occurs in years divisible by 4 unless the year is divisible by 100, in which case it is a Leap Year only if the year is divisible by 400:

sub days_in_year {

    my $year = shift;
    return 366 if 0 == $year%4 and 0 != $year%100;
    return 366 if 0 == $year%400;
    return 365;
}

The same function, written in Ruby:

def days_in_year (year)
    return 366 if 0 == year%4 and 0 != year%100;
    return 366 if 0 == year%400;
    return 365;
end

Many thanks to all the people who sent us  variations of this code in other languages. The best source for this code is at Rosetta Code where you can find program code for the Leap Year test in many languages.