Do you know what today is? Yesterday was February 28, so some badly-written programs might think it’s the 1st of March, but it’s not. Yep, it’s every programmers worst nightmare: Leap Day. You’re not getting a “code” post on Monday because I can’t read a calendar. Today’s special, so this is a special post.
Dates are hard. There’s no getting around that. Every part of our calendar seems like it was made specifically to drive programmers insane. Most years have 365 days, but every fourth one has 366. Well, unless it’s a century year, then it’s back to 365. Except every 400 years, like in the year 2000—those are leap years again. Y2K wasn’t as bad as it could’ve been, true, but there were quite a few hiccups. (Thanks to JavaScript weirdness, those never really stopped. Long after millennial fever died down, I saw a website reporting the current year as “19108”!) But 2000 was a leap year, and that surprised older software almost as much as actually being in the year 2000.
It gets worse. How long is a month? The answer: it depends. Weeks are always 7 days, thankfully, but you can’t divide 365 or 366 into 7 without a remainder. You’ll always have one or two days left over. And a day isn’t necessarily 24 hours, thanks to DST. Topping it all off, you can’t even assume that there are 60 seconds in a minute, because leap seconds. (That one is subject to change, supposedly. I’ll believe it when I see it.)
That’s just for the calendar we use today in the Western world. Add in everything else involving dates, and you have a recipe for disaster. The days in a month are numbered consecutively, right? Wrong! If you’re using, say, the Jewish calendar, you can’t even guarantee that two years have the same number of months. The Islamic calendar once depended on the sighting of the moon. The Maya had a calendar with 5 or 6 days that weren’t part of any month. Even our own Gregorian calendar doesn’t have a year zero. At some point, you’re left wondering how society itself has made it this far.
I’ll leave it to sites like The Daily WTF for illustrated examples of date handling gone wrong. God knows there are enough to choose from. (Dates in Java are especially horrendous, I can say from experience.) Honestly, I’d have to say that it’s more amazing to see date handling done right. By “right”, I mean handling all the corner cases: leap seconds, leap years, week numbers, time zones, DST adjustments, etc. ISO has a standard for dates, but…good luck with that.
So don’t be surprised to see a few things break today. And if you’re not a programmer, don’t feel bad. Sometimes, it feels like we can’t figure this out any better than you. That’s never more true than on this day.