| Date Validation |
|---|
Hi There: I say there was a February 29 in 1944. A friend says it wasn't a leap year. Cheers, Tim in Toronto, CanadaActual 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.
Programmers are logically-minded (otherwise their programs don't work and they stop being programmers) 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