<p>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.</p>

<pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
#include &lt;time.h&gt;
#include &lt;limits.h&gt;

void print(time_t rt) {
    struct tm * t = gmtime(&amp;rt);
    puts(asctime(t));
}

int main() {
    print(0);
    print(time(0));
    print(LONG_MAX);
    print(LONG_MAX+1);
}
</code></pre>

<p>Execution results in:</p>

<ul>
<li>Thu Jan  1 00:00:00 1970</li>
<li>Sat Aug 30 18:37:08 2008</li>
<li>Tue Jan 19 03:14:07 <strong>2038</strong></li>
<li>Fri Dec 13 20:45:52 <strong>1901</strong></li>
</ul>

<blockquote>
  <blockquote>
    <p>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) ).</p>
  </blockquote>
</blockquote>

<p>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?</p>

<p><strong>Update</strong> It would seem that indeed 64-bit systems are safe from this:</p>

<pre class="lang-java prettyprint-override"><code>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));
    }
}
</code></pre>

<ul>
<li>Wed Dec 31 16:00:00 PST 1969</li>
<li>Sat Aug 30 12:02:40 PDT 2008</li>
<li>Sat Aug 16 23:12:55 PST <strong>292278994</strong></li>
<li>Sun Dec 02 08:47:04 PST <strong>292269055</strong></li>
</ul>

<p>But what about the year 292278994?</p>

[{'answer_id': 36242, 'author': 'rmmh', 'author_id': 3694, 'author_profile': 'https://Stackoverflow.com/users/3694', 'pm_score': -1, 'selected': False, 'text': "<p>By 2038, time libraries should all be using 64-bit integers, so this won't actually be that big of a deal (on software that isn't completely unmaintained).</p>\n\n<p>COBOL programs might be fun though.</p>\n"}, {'answer_id': 36243, 'author': 'Teifion', 'author_id': 1384652, 'author_profile': 'https://Stackoverflow.com/users/1384652', 'pm_score': -1, 'selected': False, 'text': '<p>Operative word being "should".</p>\n\n<p>If you need to ensure futureproofing then you can construct your own date/time class and use that but I\'d only do that if you think that what you write will be used on legacy OS\'</p>\n'}, {'answer_id': 36249, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 2, 'selected': False, 'text': "<p>Visual Studio moved to a 64 bit representation of time_t in Visual Studio 2005 (whilst still leaving _time32_t for backwards compatibility).</p>\n\n<p>As long as you are careful to always write code in terms of time_t and don't assume anything about the size then as sysrqb points out the problem will be solved by your compiler.</p>\n"}, {'answer_id': 217348, 'author': 'Schwern', 'author_id': 14660, 'author_profile': 'https://Stackoverflow.com/users/14660', 'pm_score': 7, 'selected': True, 'text': '<p>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.</p>\n\n<p>You can find the code <a href="https://github.com/schwern/y2038" rel="noreferrer">at the y2038 project</a>.  Please feel free to post any questions to the <a href="https://github.com/schwern/y2038/issues" rel="noreferrer">issue tracker</a>.</p>\n\n<p>As to the "this isn\'t going to be a problem for another 29 years", peruse this <a href="https://github.com/schwern/y2038/wiki/Why-Bother%3F" rel="noreferrer">list of standard answers</a> to that.  In short, stuff happens in the future and sometimes you need to know when.  I also have <a href="http://www.slideshare.net/schwern/whos-afraid-of-2038-presentation" rel="noreferrer">a presentation on the problem, what is not a solution, and what is</a>.</p>\n\n<p>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.</p>\n'}, {'answer_id': 237016, 'author': 'benc', 'author_id': 2910, 'author_profile': 'https://Stackoverflow.com/users/2910', 'pm_score': 0, 'selected': False, 'text': "<p>Keep good documentation, and include a description of your time dependencies. I don't think many people have thought about how hard this transition might be, for example HTTP cookies are going to break on that date.</p>\n"}, {'answer_id': 237057, 'author': 'Kasprzol', 'author_id': 5957, 'author_profile': 'https://Stackoverflow.com/users/5957', 'pm_score': 4, 'selected': False, 'text': '<p>You can always implement <a href="http://www.ietf.org/rfc/rfc2550.txt" rel="noreferrer">RFC 2550</a> and be safe forever ;-)</p>\n\n<blockquote>\n  <p>The known universe has a finite past and future.  The current age of\n     the universe is estimated in [Zebu] as between 10 ** 10 and 2 * 10 **\n     10 years.  The death of the universe is estimated in [Nigel] to occur\n     in 10 ** 11 - years and in [Drake] as occurring either in 10 ** 12\n     years for a closed universe (the big crunch) or 10 ** 14 years for an\n     open universe (the heat death of the universe).</p>\n</blockquote>\n\n<p>&nbsp;</p>\n\n<blockquote>\n  <p>Y10K compliant programs MAY choose to limit the range of dates they\n     support to those consistent with the expected life of the universe.\n     Y10K compliant systems MUST accept Y10K dates from 10 ** 12 years in\n     the past to 10 ** 20 years into the future.  Y10K compliant systems\n     SHOULD accept dates for at least 10 ** 29 years in the past and\n     future.</p>\n</blockquote>\n'}, {'answer_id': 682674, 'author': 'Martin Brown', 'author_id': 20553, 'author_profile': 'https://Stackoverflow.com/users/20553', 'pm_score': 1, 'selected': False, 'text': "<p>I think that we should leave the bug in. Then about 2036 we can start selling consultancy for large sums of money to test everything. After all isn't that how we successfully managed the 1999-2000 rollover.</p>\n\n<p>I'm only joking!</p>\n\n<p>I was sat in a bank in London in 1999 and was quite amazed when I saw a consultant start Y2K testing the coffee machine. I think if we learnt anything from that fiasco, it was that the vast majority of software will just work and most of the rest won't cause a melt down if it fails and can be fixed after the event if needed. As such, I wouldn't take any special precautions until much nearer the time, unless you are dealing with a very critical piece of software.</p>\n"}, {'answer_id': 692403, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>What should we do to prepare for 2038?</p>\n</blockquote>\n\n<p>Hide, because the apocalypse is coming.</p>\n\n<p>But seriously, I hope that compilers (or the people who write them, to be precise) can handle this. They've got almost 30 years. I hope that's enough time.</p>\n\n<p>At what point do we start preparing for Y10K? Have any hardware manufacturers / research labs looked into the easiest way to move to whatever new technology we'll have to have because of it? </p>\n"}, {'answer_id': 3850599, 'author': 'Ian Ringrose', 'author_id': 57159, 'author_profile': 'https://Stackoverflow.com/users/57159', 'pm_score': 1, 'selected': False, 'text': '<p>Given my age, I think I should pay a lot into my pension and pay of all my depts, so someone else will have to fit the software!</p>\n\n<p>Sorry, if you think about the “net present value” of any software you write today, it has no effect what the software does in 2038.  A “return on investment” of more than a few years is uncommon for any software project, so you make a lot more money for your employer by getting the software shipped quicker, rather than thinking that far ahead. </p>\n\n<p>The only common exception is software that has to predict future, 2038 is already a problem for mortgage quotation systems.</p>\n'}, {'answer_id': 42657245, 'author': 'Machinegon', 'author_id': 1342231, 'author_profile': 'https://Stackoverflow.com/users/1342231', 'pm_score': 1, 'selected': False, 'text': '<p>I work in embedded and I thought I would post our solution here. Our systems are on 32 bits, and what we sell right now has a warantee of 30 years which means that they will encounter the year 2038 bug. Upgrading in the future was not a solution.</p>\n\n<p>To fix this, we set the kernel date 28 years earlier that the current date. It\'s not a random offset, 28 years is excatly the time it will take for the days of the week to match again. For instance I\'m writing this on a thursday and the next time march 7 will be a thursday is in 28 years. </p>\n\n<p>Furthermore, all the applications that interact with dates on our systems will take the system date (time_t) convert it to a custom time64_t and apply the 28 years offset to the right date.</p>\n\n<p>We made a custom library to handle this. The code we\'re using is based off this: <a href="https://github.com/android/platform_bionic" rel="nofollow noreferrer">https://github.com/android/platform_bionic</a></p>\n\n<p>Thus, with this solution you can buy yourself an extra 28 years easily.</p>\n'}]
['https://Stackoverflow.com/questions/36239', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/338/']