Skip to content

Instantly share code, notes, and snippets.

@MihailJP
Created October 8, 2012 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MihailJP/3852992 to your computer and use it in GitHub Desktop.
Save MihailJP/3852992 to your computer and use it in GitHub Desktop.
Check if your time_t implementation will work after 2038
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
time_t time_num[5] = {(time_t)0x00000000, (time_t)0x7fffffff, (time_t)0x80000000, (time_t)0xffffffff,};
struct tm* parsed_time; unsigned int i;
printf("sizeof(time_t) is %d.\n", sizeof(time_t));
for (i = 0; i < 4; i++) {
parsed_time = gmtime(&(time_num[i]));
if (parsed_time == NULL) {
if (sizeof(time_t) >= 8) printf("(time_t)0x%016llx is null.\n", time_num[i]);
else printf("(time_t)0x%08x is null.\n", time_num[i]);
} else {
if (sizeof(time_t) >= 8)
printf("(time_t)0x%016llx is %04lld/%02d/%02d %02d:%02d:%02d UTC.\n",
time_num[i],
(long long)parsed_time->tm_year+1900, parsed_time->tm_mon+1, parsed_time->tm_mday,
parsed_time->tm_hour , parsed_time->tm_min , parsed_time->tm_sec);
else
printf("(time_t)0x%08x is %04d/%02d/%02d %02d:%02d:%02d UTC.\n",
time_num[i],
parsed_time->tm_year+1900, parsed_time->tm_mon+1, parsed_time->tm_mday,
parsed_time->tm_hour , parsed_time->tm_min , parsed_time->tm_sec);
}
}
if (sizeof(time_t) >= 8) {
printf("This C implementation will work well after January 2038.\n");
return 0;
} else if (sizeof(time_t) == 4) {
if ((time_t)0x80000000 < (time_t)0) {
printf("time_t seems to be signed integer.\n");
printf("This C implementation might not work after January 2038.\n");
return 1;
} else {
printf("time_t seems to be unsigned integer. Huh?\n");
printf("This C implementation might work until beginning of February 2106.\n");
return 2;
}
} else {
printf("Huh?\n");
return 2;
}
}
@MihailJP
Copy link
Author

MihailJP commented Oct 8, 2012

Compilers' default (i.e. no options except source file name given) behavior tested on my PC...

Win7 32bit, standalone:

  • Cygwin GCC 4.5: Fail
  • MinGW GCC 4.5: Fail
  • MS Visual C++ 2010 Express: Pass
  • Digital Mars C 8.52: Fail
  • Borland C++ Compiler 5.5: Fail (Include and Library paths must be specified)
  • Tiny C Compiler 0.9.25: Fail
  • Open Watcom 1.9: Unsigned
  • SDCC 3.2.1: Won't compile

Xubuntu 12.04 LTS 32bit, on VMware Player:

  • GCC 4.6.3: Fail
  • Clang 3.0: Fail

Fedora 17 LXDE spin 32bit, on VMware Player:

  • GCC 4.7.2: Fail
  • Clang 3.0: Fail
  • Portable C compiler 1.1.0: Fail
  • NQC 3.1: Won't compile
  • SDCC 3.0.0: Won't compile

Arch Linux 32bit, on VMware Player:

  • GCC 4.7.1: Fail
  • Clang 3.1: Fail

OpenSUSE 12.2 64bit, standalone:

  • GCC 4.7.1: Pass
  • Clang 3.1: Pass

FreeBSD 9.0-RELEASE 32bit, on VMware Player:

  • GCC 4.2.1: Fail
  • GCC 4.7.0: Fail
  • Clang 3.0: Fail
  • Tiny C Compiler 0.9.25: Fail
  • UCC 1.6.0: Won't compile
  • Bruce's C compiler 1995.03.12: Won't compile
  • C Interpreter 5.16.19: Won't compile

Legend

  • "Pass": time_t is 64-bit integer, and will work after mid-January 2038.
  • "Fail": time_t is 32-bit signed integer, and won't work after 2038.
  • "Unsigned": time_t seems to be 32-bit unsigned integer.
  • "Won't compile": There is a compile-time issue, and cannot be tested.

@greg-kennedy
Copy link

FreeBSD 11.1-RELEASE-p5 32bit, standalone:

  • Clang 4.0.0: Fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment