February 8, 2016
MarkBernstein.org
 

Dates, Done Right

Reviewing the bidding, Tinderbox’s date conversion routines sometimes were sometimes off by one day when entering dates in the 19th century and earlier. The precise date before which the error is observed varies for different customers, from a date in the 1840s to one in the 1880s. The error is completely consistent when you observe it, but doesn’t affect everyone all the time.

What on earth could explain this?

The Answer. Tinderbox dates are points in time. When you say, "July 4, 1776," Tinderbox needs to choose some specific moment on that date. Some programs choose an arbitrary time -- noon, say. Tinderbox chooses whatever the current time is.

But there was one thing they had forgotten. The current time depends, of course, on your time zone. Internally, dates are stored in Universal Time, but you don’t care about that. So, we do a simple two-step:

  1. Get the local time, 10:24 today
  2. Convert it to Universal Time. So that’s 15:24 today.
  3. Convert July 4, 1776 to Universal Time.
  4. Set the clock to the current time. Now it’s 15:24 on July 14, 1776.
  5. Convert back to your local time

But, back before they invented railroads, there were no time zones. And OS X knows this! So, before time zones were introduced, the local time doesn’t get shifted to the time zone. And, if your local time is one a different calendar date than Greenwich, you’re off by one day!

Once you see this, it’s easy to fix. When you’re looking at a page full of CFDateRefs, it’s harder to see.

Lessons:

It wasn’t that long ago that you couldn’t enter a date before 1904 on a Macintosh.

Update: And here are the rules for Time Zones, thanks to Tom Harrington.