I am experimenting with time_t variables, and this is the code in question:
#include <stdio.h>
#include <time.h>
#include <stdint.h>
struct tm epochtime;
time_t epochdate;
int main()
{
epochtime.tm_mday = 19;
epochtime.tm_mon = 10;
epochtime.tm_year = 2002;
epochtime.tm_hour = 0;
epochtime.tm_min = 0;
epochtime.tm_sec = 0 ;
epochdate = mktime(&epochtime);
printf("%ju\n",epochdate);
printf("%ju\n", (uint32_t)epochdate);
printf("%ju\n", (uint64_t)epochdate);
printf("%ju\n", (uintmax_t)epochdate);
printf("%Lf\n", epochdate);
printf("%Le\n", epochdate);
}
I am trying to print the epochtime of a given date. The code compiles and has no errors, but when I compare what I get printed to what I calculate on this website, the values are not the same. For this given values in the example above the code output is:
210453397503
210453397503
18446744073709551615
18446744073709551615
-1.#QNAN0e+000
-1.#QNAN0e
while the link says that value should be 1034985600. I have tried multiple printf format specifiers because I found multiple answers here on how to print time_t variables, but none of them seems to work for me. Any ideas why?
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:
Your year should be 2002 - 1900 = 102, and your month should be 9, not 10 (months start from 0 = January).
The correct code is:
Which correctly outputs
1034985600as you expect.The problem with your code is most likely that
mktimeis 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.