I used to think that leap years only happened every four years. In fact, every centennial year that is not evenly divisible by 400 is not a leap year.
I don’t expect to live to 2100, so it shouldn’t matter. But, unfortunately, in programming world, remembering this caveat on the leap year could result in a bug, or worse, world cataclysm like we saw didn’t see with Y2K.
I recently landed this bug and had to write a way out of it. Here’s my solution in PHP, JavaScript, and Cappuccino.
1234567891011function isLeapYear($aYear) {
if (($aYear % 4) == 0) {
if (($aYear % 100) == 0 && (($aYear % 400) != 0) {
return false;
} else {
return true;
}
} else {
return false;
}
}
1234567891011function isLeapYear(aYear) {
if ((aYear % 4) == 0) {
if ((aYear % 100) == 0 && ((aYear % 400) != 0) {
return false;
} else {
return true;
}
} else {
return false;
}
}
1234567891011+ (BOOL)isLeapYear:(int)aYear {
if ((aYear % 4) == 0) {
if ((aYear % 100) == 0 && ((aYear % 400) != 0) {
return NO;
} else {
return YES;
}
} else {
return NO;
}
}