Friday, February 05, 2010

I found the last Y2K bug this afternoon

And I caused it, by a design decision I made 10 years ago.

To generate unique record keys in databases I have developed, I have an algorithm that takes the current date and time and appends a 4-digit random number, so you get keys like this:

0904220824438920 (which tells me it was created on 04/22/09 at 8:24:43 am), and, for it not to be unique, two records would have to be created at the exact same hundredth of a second, and the random-number generator would have had to, twice in quick succession, decide on 8920.

All of these databases came into being since 2001, so it was reasonable to assume that the lowest possible record-key that could be generated would be 010101000001xxxx (that is, one-hundredth of a second past midnight on January 1, 2001).

I have a routine that says 'find oldest data on file' and it gets the minimum value for this (string) field it can find, in the first table that is created when initializing a new database. Got it? Should be fool-proof.

Well, they keep inventing more-clever fools.

Recently, my tester imported data from a group in Mongolia (really), and the 'find oldest data on file' routine failed.

I probed and quickly saw that one of the keys in the table in question in their data was 8801012140074820 (that is, a record created on January 1, 1988, many years before the database came into being). How is this possible?

I surmise that the clock in their computer was incorrect, and their system time really was set to January 1988, which also explains why some other data in their copy of the database was stamped with that date.

I never dreamed that ANYONE would have any (automatically-generated) key dates that referred to a 20th century date, which is why I am only using a 2-digit year in my internal keys. Had I included the '19' or '20', it would have been OK, so this is actually the last Y2K bug to be discovered (maybe)!

fun, huh? I thought I was clever saving those 2 characters in every database record, and, up until today, it has worked out great.

I wrote my first computer program in 1970. Old habits die hard.

No comments: