Date Validation —
Is this a Leap Year?

Hi There:

I say there was a February 29 in 1944.  A friend says it wasn't a leap year. 

Cheers, Tim in Toronto, Canada

Actual email message, received 1999-03-20. Maybe his friend thought that since there was a war going on they cancelled the leap day for that year.


Some people believe that the Year 2000 Crisis was exaggerated because society did not collapse on January 1st, 2000. It does not occur to them that perhaps major disasters were averted because of all the work done going through old program code looking for vulnerabilities.

Some programmers who were aware that 2000 was a leap year were not aware that 2004 was also a leap year. As a result of this, several online systems crashed on February 29th, 2004. See Leap year blamed for HICAPS stumble.


Programmers are logically-minded (otherwise their programs don't work and they stop being programmers, or more likely blame someone else and manage to keep their jobs) so it is surprising to find that a good many of them have a problem with the rule for leap years. There's sometimes confusion as to whether the year 2000 had a February 29th (yes, it did). Here's C code for date validation in the Common Era Calendar (the Gregorian Calendar with years A.D./B.C. replaced by the astronomical system of numbering years):


    //  DATEVAL.C
    //  Author: Peter Meyer
    //  Last modified: 1999-02-18
    //  Free to use.

    #define TRUE  1
    #define FALSE 0

    int is_leap_year(int year);

    /*-------------------------------------------*/
    int date_is_valid(int day, int month, int year)
    {
    int valid = TRUE;
    int month_length[13] =
        { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    if ( is_leap_year(year) )
        month_length[2] = 29;
        //  29 days in February in a leap year (including year 2000)
    
    if ( month < 1 || month > 12 )
        valid = FALSE;
    else if ( day < 1 || day > month_length[month] )
        valid = FALSE;
    
    return ( valid );
    }
    
    /*----------------------*/
    int is_leap_year(int year)
    {
    int result;
    
    if ( (year%4) != 0 )           //  or:    if ( year%4 )
        result = FALSE;            //  means: if year is not divisible by 4
    else if ( (year%400) == 0 )    //  or:    if ( !(year%400) )
        result = TRUE;             //  means: if year is divisible by 400
    else if ( (year%100) == 0 )    //  or:    if ( !(year%100) )
        result = FALSE;            //  means: if year is divisible by 100
    else                           //  (but not by 400, since that case
        result = TRUE;             //  considered already)
    
    return ( result );
    }

29th February, 2000 Index
C/C++ Programming Home Page