1. can someone read the code and help me see why the code is not giving a correct answer? To test the code i set an easy value as the date of birth,i set it one day before today, on second try i set it a decade ago but still wrong result. I get a 0 value (or a wrong int number if i mess a lot with the code). The code is an example from a textbook. I know there are other ways to do it, but i do not want to use other functions only these two: mktime,difftime if there is a way...
2.I checked the mktime() example on this forum and it says tm_year= myvalue -1900. I think that the epoch is 1970 not 1900 or... it is 1900 the default value exclusively for struct tm? What am i missing/ignoring here?
i am using windows 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t date;
struct tm birth = {0};
printf("you were born in...(the format is :yy/mm/dd): ");
fflush(stdout);
scanf( "%d %d %d", &birth.tm_year, &birth.tm_mon, &birth.tm_mday );
birth.tm_year -= 1900; //why not 1970? epoch= 1970! right?
birth.tm_mon -= 1;
date = mktime(&birth);
printf("your age in seconds is:: \n %ld", difftime(time(NULL),date));
}
You appear to be using the wrong format specifier for the value returned by difftime(). The "%ld" specifier is for a long int, difftime() returns a double not a long int.
i changed it from integer to float, i get a better result 95% correct, but i think it is a bit off the correct answer by 30 minutes. To test the code, i set it one day before today and then i made the subtraction 86400 - 84500 = 1900 seconds( aka 0.523 hours which is 30 mins)
(The moment i tested the value the program is giving is 84500) .
As the time goes by the 84500 is slowly increasing to eventually become equal to 86400. I do not know what i am missing but that 84500 is not the exact answer. In my gmt+3 time here this moment i type this message the 84500 will become equal to 86400 at 01:15 after midnight, which does not make a lot of sense to me....