Time out of mind

At a former company, a cow-orker of mine had cobbled up a set of date and time classes that he declared “general purpose” and “bug free.” About three months later, and after he had left the company, I had to do an 11th hour re-write of the code, just prior to releasing our company’s first product.

Some of the bugs I found:

  • The classes had a pretty interface with lots and lots of range, but internally the code used the Unix epoch of 1970 (so no birthdays before then, for instance). It was round-tripping the 1970 epoch through the standard Unix 1970-based routines as well
  • Didn’t handle 400-year cycle of leap years
  • Fell apart in 2038, like everything else will (Unix epoch, natch)
  • Fell apart in 2008, 2010, 2012, and some other years, as I recall. One failure was like some random day in March 2007, I forget why
  • Obviously didn’t handle screw cases like the calendar skipping that happened in 1582
  • Forget about supporting astronomy or geology applications…

… and other things.

I recall writing unit tests that stepped through the ranges that we definitely cared about (several tens of thousands of years around the current epoch), and also stepped plus-and-minus in the billions of years ranges (by millions or thousands, I think). Random tests, too.

I also recall spending a lot of time reconciling OLE’s idea of date-and-time. (Don’t ever let me catch you using floats for something like this. Really. I’ll find you).

This stuff is not easy to get right, but everyone thinks that it is. Maybe that’s why people get it wrong so often (“Oh, it’s just same date/time code, I’ll just whip this out and go to lunch.  Umm, ’30 days hath November, except September, May, and, uh…'”)

Required reading: Calendrical Calculations. Unless you write stuff used by archeologists can probably ignore the Mayan (etc.) calendars, but they’re fun to read about.

This entry was posted in Rantage. Bookmark the permalink.

5 Responses to Time out of mind

  1. Doug Graham says:

    Hi Landon –

    You might have heard about the Microsoft Zune-30 leap year date glitch last December 30th — that one stung me.

    I really wanted just to say hi. I love reading your blog being an old timer myself who dabbled in Atari programming, etc.

    I used to work with Judy Bogart and Donna Auguste at IntelliCorp in Mountain View back in the 80’s and early 90’s (I worked there until 2005).

    Judy mentioned you several times and told me about doing Robotron for the 800 (and other fun stories) — good times, good times.

    All the best to you & your family!

    Regards,

    — Doug

  2. Firstly, I’d like to say that I haven’t done any research into what is available out there or not.

    However by what you describe above, it sounds like there has got to be some proven open source libraries out there that do what you want already.
    Someone else has got to have already written the code needed to support what you mentioned above and released it as an free component.

  3. Thomas says:

    I fully agree – and I’m probably guilty of introducing most of the problems listed here at one point or another myself… or I alternatively had to clean up behind somebody else on various projects where the calendar code suddenly went suborbital.

    And you are correct, it’s the fact that all programmers think it’s easy, it has been done before, and “we’ll just reuse some old code” that makes calendar calculations such a nightmare.

    My most recent run-in with a supposedly working plug-and-play calendar library ended with several wasted weekends trying to figure out why it chopped off days at the end of random months. Fun!

  4. William Mayo says:

    Two questions:
    A) Was the hyphenation of “cow-orker” intentional?
    B) Are there any date/calendar libraries that you would recommend as high quality? I don’t particularly have a use, but I’m curious as to whether they’re all that bad 😉

  5. Kaishaku says:

    The BlackBerry’s Calendar class is broken. I wanted to use it so I wouldn’t have to do my own number-of-milliseconds-since-the-epoch calculations and worry about leap years and daylight savings and such, but nope.. I was relegated to doing so anyway.

Leave a Reply

Your email address will not be published. Required fields are marked *