Tutorial :How do you create a UTC time in C for a specific day, month and year?



Question:

How do I create a UTC time in C for the following date:

1st July 2038

using standard ANSI C function calls (given that the tm_year element of the tm structure cannot be greater than 137) ?


Solution:1

You don't. The 32-bit ANSI C time_t rolls over in 2038. It's like asking how you create July 23, 2003 in your old 2-digit-year COBOL system.


Solution:2

Others have noted that the particular date you give as an example falls beyond the maximum date/time representable by a 32-bit time_t, often referred to as the Year 2038 problem. One solution is to use a 64-bit time_t, which some 64-bit POSIX systems do (linux amd64), and call mktime.

#include <time.h>  #include <stdio.h>  #include <stdlib.h>    int main(void)  {          struct tm future;       /* as in future date */          time_t t;            future.tm_sec = 0;          future.tm_min = 0;          future.tm_hour = 0;          future.tm_mday = 1;     /* 1st */          future.tm_mon = 6;      /* July */          future.tm_year = 2038 - 1900; /* 2038 in years since 1900 */          future.tm_isdst = 0;          /* Daylight Saving not in affect (UTC) */  #ifdef _BSD_SOURCE          future.tm_zone = "UTC";  





        
Previous
Next Post »