Monday, December 8, 2008

Y2K38

This is a follow-up to the post on "Powers of two".

Readers of that post may recall that using 32 bits, there are exactly 4,294,967,296 possible patterns or codes. That's quite a lot to expect a reader to remember. But perhaps the reader will remember the rule of thumb, and recall that 32 bits is about 4 billion patterns.

It happens that in Linux systems, such as the one I use to write and publish my own little web applications, a 32 bit number is used to store the current date and time. The possible bit patterns are used to indicate the number of seconds after midnight, January 1, 1970 GMT (Greenwich Mean Time). This is an approximation (see Unix time for the gory detail).

Here is a table showing just a very few of the possible bit patterns (grouped by four for readability). I have placed the bit patterns in a particular order, which is a lot like an automobile odometer, with each wheel working its way up from 0 to 1 and around again.

The first column shows the bit pattern, and the second column shows the decimal number that is generally assigned to that bit pattern.

0000 0000 0000 0000 0000 0000 0000 0000 0
0000 0000 0000 0000 0000 0000 0000 0001 1
0000 0000 0000 0000 0000 0000 0000 0010 2
0000 0000 0000 0000 0000 0000 0000 0011 3
0000 0000 0000 0000 0000 0000 0000 0100 4
...
0000 0001 1101 0111 1111 1001 0000 0000 30931200
...
0011 1011 1001 1101 1111 1001 1010 0000 1000208800
...
0100 1001 0011 1101 1100 0001 0010 1001 1228792105
...
0111 1111 1111 1111 1111 1111 1111 1101 2147483645
0111 1111 1111 1111 1111 1111 1111 1110 2147483646
0111 1111 1111 1111 1111 1111 1111 1111 2147483647
1000 0000 0000 0000 0000 0000 0000 0000 -2147483648
1000 0000 0000 0000 0000 0000 0000 0001 -2147483647
1000 0000 0000 0000 0000 0000 0000 0010 -2147483646
1000 0000 0000 0000 0000 0000 0000 0011 -2147483645
...
1111 1111 1111 1111 1111 1111 1111 1110 -2
1111 1111 1111 1111 1111 1111 1111 1111 -1

If you click on the link for one of the bit patterns, you will be using one of my little web applications to tell you what that bit pattern means as a date and time on my Linux server. Since the server is located in Texas, the time will be in CST (Central Standard Time).

The very first link is the moment at which the Unix epoch begins. The next particular times that I have chosen to list are, in order: 1 through 4 seconds after the start of the Unix epoch; the start of Christmas Day in London in 1970; a moment that has had a big impact on our country; a moment that came and went while I was writing this post; and the final three seconds of the Linux clock.

After that, things go wrong. The usual interpretation of the bit pattern as a number goes negative. This happens because the usual interpretation of a bit-pattern-as-number is done with Two's Complement rules.

And at that point, many programs will stop working as expected. This is known as the "Unix Millenium bug" or "Y2K38".

If you just look at the bit patterns, you'll see that the problem happens right when the odometer rolls over to put a one in the left-most position (making it negative Two's Complement rules). My program stops counting forward at that point and jumps to 1901.

Of course, my program will not likely still be available by 2038! Even if I have continued to pay the lease payments, and that I have lived well into my 80's, it is likely that the system will have been updated to fix the problem by then.

This is nothing for anyone to lose any sleep over. I had to post it to get it out of my head. Other articles about this are one by Ansari and Patil and another by Unnikrishnan, both published in May, 2005.

2 comments:

Myrna said...

I am glad that there are people smart enough to figure things like this out, but I don't mind that I am not one of them! :o)

Bruce Conrad said...

My program is still running, and I have done nothing to change it since then (2008). But something got "fixed" because all of the values work, well into the year 2106 (by which time my program will surely have been stopped).