<<< Back to the Linux Tips Index

30th December 2016

The UNIX Epoch and 2038 bug

Unix (and as a result, Linux, and many other) systems tend to store times as what is called a "signed 32-bit integer". A signed integer is a way of storing a binary number in a way which will allow it to express both positive and negative numbers. So, instead of using the simple binary method of using 00, 01, 10, 11, etc to count from 0 upwards, the mid-point is used to express "zero", so that the 32-bit binary can be used to represent a range from -2,147,483,647 to +2,147,483,647, rather than from 0 to +4,294,967,295:

Decimal32-bit BinarySigned DecimalDate
000000000 00000000 00000000 00000000-2,147,483,6471901-12-13 20:45:53
100000000 00000000 00000000 00000001-2,147,483,6461901-12-13 20:45:54
200000000 00000000 00000000 00000010-2,147,483,6451901-12-13 20:45:55
300000000 00000000 00000000 00000011-2,147,483,6441901-12-13 20:45:56
...
2,147,483,64601111111 11111111 11111111 11111110-11969-12-31 23:59:59
2,147,483,64701111111 11111111 11111111 1111111101970-01-01 00:00:00
2,147,483,64810000000 00000000 00000000 00000000+11970-01-01 00:00:01
...
3,630,626,51401011000 01100110 11110110 11010011+1,483,142,8672016-12-30 00:07:47
3,630,712,44711011000 01101000 01000110 01111111+1,483,228,8002017-01-01 00:00:00
...
4,294,967,29411111111 11111111 11111111 11111110+2,147,483,6462038-01-19 03:14:06
4,294,967,29511111111 11111111 11111111 11111111+2,147,483,6472038-01-19 03:14:07

The designers of Unix, around 1970, decided that a signed 32-bit integer would be a suitable representation for dates. You can take zero as being midnight on the 1st January 1970 (UTC), and each second bumps that number up by one. So midnight was zero, one minute past midnight was 60, two minutes past midnight was 120, and so on. And this method could be used to count backwards, too, all the way back to 2.147 billion seconds before that date.

That covers a period from December 1901 up until January 2038. So any company records, etc, could be covered quite reasonably, and, around 1970, when the Stanley Kubrick / Arthur C Clarke movie "2001" was predicting how different the world would be in the mind-bogglingly futuristic year of 2001, that would have seemed perfectly reasonable. And, indeed, this system is still in wide use today, and is causing no problems. At the time of writing this, approaching New Year's Eve 2016, the time is 1483142867 seconds since 1st January 1970, or 01011000 01100110 11110110 11010011 in signed 32-bit binary. So there's still plenty of room to work with, and few people designing a system in 1970 would expect it to still be in use in 2017.

Some systems have now moved on to 64-bit times, and will be unaffected by this "bug". Others, however, will have to come up with a workaround. But there's no need to panic, there are 21 years yet until 2038!

 

Invest in your career. Buy my Shell Scripting Tutorial today:

Steve Parker - Linux / DevOps Consultant
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon