I suppose that you want to represent the date: October 19th, 2002 00:00:00, which corresponds to the epoch timestamp that you expect: 1034985600.
In such case, you are doing it wrong. Read the manual:
Broken-down time is stored in the structure
tm
, which is defined in<time.h>
as follows:struct tm { int tm_sec; /* Seconds (0-60) */ int tm_min; /* Minutes (0-59) */ int tm_hour; /* Hours (0-23) */ int tm_mday; /* Day of the month (1-31) */ int tm_mon; /* Month (0-11) */ int tm_year; /* Year - 1900 */ int tm_wday; /* Day of the week (0-6, Sunday = 0) */ int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */ int tm_isdst; /* Daylight saving time */ };
Your year should be 2002 – 1900 = 102, and your month should be 9, not 10 (months start from 0 = January).
The correct code is:
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
int main(void) {
struct tm epochtime = {
.tm_mday = 19,
.tm_mon = 9,
.tm_year = 102,
.tm_hour = 0,
.tm_min = 0,
.tm_sec = 0,
.tm_isdst = -1
};
time_t epochdate = mktime(&epochtime);
if (epochdate == (time_t)(-1)) {
perror("mktime failed");
return 1;
}
printf("%" PRIuMAX "\n", (uintmax_t)epochdate);
return 0;
}
Which correctly outputs 1034985600
as you expect.
The problem with your code is most likely that mktime
is not able to correctly represent the “wrong” date you provide and returns -1
, which you then print as unsigned and becomes a huge nonsensical number.
CLICK HERE to find out more related problems solutions.