Created
October 8, 2012 15:04
-
-
Save MihailJP/3852992 to your computer and use it in GitHub Desktop.
Check if your time_t implementation will work after 2038
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FreeBSD 11.1-RELEASE-p5 32bit, standalone: