Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should we do to prepare for 2038?

I would like to think that some of the software I'm writing today will be used in 30 years. But I am also aware that a lot of it is based upon the UNIX tradition of exposing time as the number of seconds since 1970.

#include <stdio.h> #include <time.h> #include <limits.h>  void print(time_t rt) {     struct tm * t = gmtime(&rt);     puts(asctime(t)); }  int main() {     print(0);     print(time(0));     print(LONG_MAX);     print(LONG_MAX+1); } 

Execution results in:

  • Thu Jan 1 00:00:00 1970
  • Sat Aug 30 18:37:08 2008
  • Tue Jan 19 03:14:07 2038
  • Fri Dec 13 20:45:52 1901

The functions ctime(), gmtime(), and localtime() all take as an argument a time value representing the time in seconds since the Epoch (00:00:00 UTC, January 1, 1970; see time(3) ).

I wonder if there is anything proactive to do in this area as a programmer, or are we to trust that all software systems (aka Operating Systems) will some how be magically upgraded in the future?

Update It would seem that indeed 64-bit systems are safe from this:

import java.util.*;  class TimeTest {     public static void main(String[] args) {         print(0);         print(System.currentTimeMillis());         print(Long.MAX_VALUE);         print(Long.MAX_VALUE + 1);     }      static void print(long l) {         System.out.println(new Date(l));     } } 
  • Wed Dec 31 16:00:00 PST 1969
  • Sat Aug 30 12:02:40 PDT 2008
  • Sat Aug 16 23:12:55 PST 292278994
  • Sun Dec 02 08:47:04 PST 292269055

But what about the year 292278994?

like image 466
Frank Krueger Avatar asked Aug 30 '08 18:08

Frank Krueger


People also ask

How do you solve 2038 problems?

Solutions. There is no universal solution for the Year 2038 problem. For example, in the C language, any change to the definition of the time_t data type would result in code-compatibility problems in any application in which date and time representations are dependent on the nature of the signed 32-bit time_t integer.

What will happen in the year 2038?

The counter will then begin to count from the negative scale, once this date comes to pass, causing the computers to reset to December 13th 1901, leading to all kinds of errors in every modern 32-bit computer. This is the 2038 problem, which will mark the end of UNIX time and subsequently the UNIX epoch.

Why is there no October in 2038?

If you have read How Bits and Bytes Work, you know that a signed 4-byte integer has a maximum value of 2,147,483,647, and this is where the Year 2038 problem comes from. The maximum value of time before it rolls over to a negative (and invalid) value is 2,147,483,647, which translates into January 19, 2038.

What is Unix 32-bit time?

Representing timeMost 32-bit Unix-like systems store time in a 32-bit (4 bytes) signed integer and are thus able to represent a range of 136 years, from 1901 to 2038. Other 32-bit implementations use unsigned integers and can represent years up until 2106, but cannot handle dates before the epoch.


2 Answers

I have written portable replacement for time.h (currently just localtime(), gmtime(), mktime() and timegm()) which uses 64 bit time even on 32 bit machines. It is intended to be dropped into C projects as a replacement for time.h. It is being used in Perl and I intend to fix Ruby and Python's 2038 problems with it as well. This gives you a safe range of +/- 292 million years.

You can find the code at the y2038 project. Please feel free to post any questions to the issue tracker.

As to the "this isn't going to be a problem for another 29 years", peruse this list of standard answers to that. In short, stuff happens in the future and sometimes you need to know when. I also have a presentation on the problem, what is not a solution, and what is.

Oh, and don't forget that many time systems don't handle dates before 1970. Stuff happened before 1970, sometimes you need to know when.

like image 117
Schwern Avatar answered Oct 06 '22 00:10

Schwern


You can always implement RFC 2550 and be safe forever ;-)

The known universe has a finite past and future. The current age of the universe is estimated in [Zebu] as between 10 ** 10 and 2 * 10 ** 10 years. The death of the universe is estimated in [Nigel] to occur in 10 ** 11 - years and in [Drake] as occurring either in 10 ** 12 years for a closed universe (the big crunch) or 10 ** 14 years for an open universe (the heat death of the universe).

 

Y10K compliant programs MAY choose to limit the range of dates they support to those consistent with the expected life of the universe. Y10K compliant systems MUST accept Y10K dates from 10 ** 12 years in the past to 10 ** 20 years into the future. Y10K compliant systems SHOULD accept dates for at least 10 ** 29 years in the past and future.

like image 23
Kasprzol Avatar answered Oct 06 '22 00:10

Kasprzol