Date Validation —

Is this a Leap 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 (especially COBOL code) looking for vulnerabilities.Some programmers 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 #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 ); }