Skip to content

Happy Leap Day!

It’s Leap Day, February 29th.  An extra day this year for all of us to get more done.  The US National Institute of Standards and Technology leap year FAQ page describes a Leap Year, Leap Second, and even the possibility of a Negative Leap Second.  There is even an Honor Society of Leap Year Babies home page. Time and date.com discusses why leap years are used.

Leap year programming algorithms are one of the classic Computer Science programs that we all had to write.  It’s amazing that some developers still make mistakes (hint: pay attention to century years divisible evenly by 400).  Here is the implementation of the Delphi/RAD Studio IsLeapYear function (from the SysUtils.pas source file):

function IsLeapYear(Year: Word): Boolean;
begin
    Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;

Here are a few additional Leap Year algorithms and functions in different languages.  Enjoy your extra day this year.  At CodeGear, in Scotts Valley, we are having a Leap Day party this afternoon.  I hope you are all enjoying your day as well.

Java

C++

Ruby

Python

VB.NET

PERL

{ 4 } Comments

  1. Thomas Mueller | February 29, 2008 at 5:55 am | Permalink

    Leap years are easy, but do you know how to calculate the week of the year? How is it defined? OK, even that is easy nowadays, there is the Internet to look it up. I guess that shows my age….

  2. Richard Foersom | February 29, 2008 at 3:38 pm | Permalink

    Leap years are difficult stuff ;-), take MS Excel as example. Excel use date numbering starting from 1900-01-01 as day 1.

    Try this:
    - Open MS Excel on a PC, any version will do
    - Format a cell as date format
    - Enter 60 as day number

    Result: A non existing day at the end of February 1900!

    BTW, according to MS there were also a day 0 in January 1900.

    OpenOffice Calc is compatible with the Excel numbering scheme, but only from 1900-03-01 onwards.

  3. Anders E. Andersen | March 2, 2008 at 4:34 pm | Permalink

    Not so fast Sirs! In Denmark the actual leap day is the 24th, not the 29th. This has no other practical significance, than where the note should be on the calender. :)

  4. Simon | March 11, 2008 at 4:07 am | Permalink

    IsLeapYear is a good example for novice programming.

    It may be greatly improved.

    see http://qc.borland.com/wc/qcmain.aspx?d=40715

Post a Comment

Your email is never published nor shared. Required fields are marked *
Close