At the beginning I want to highlight that I've already read all similar posts in stack overflow. And nothing helped me.
#include <stdio.h>
#include <stdlib.h>
#define _USE_32BIT_TIME_T 1
#include <time.h>
int main(void)
{
struct tm beg;
struct tm aft;
beg.tm_hour=0; beg.tm_min=0; beg.tm_sec=0;
aft.tm_hour=11; aft.tm_min=19; aft.tm_sec=19;
long long c;
c=difftime(mktime(&aft),mktime(&beg));
printf("%lld",c);
return 0;
}
It all the timr print out 0 an nothing else, but when I tried to change mktime(&aft) to time now time(&now) I got non-zero result. What should I correct in this code?
If the argument passed to mktime references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, the function returns –1 cast to type time_t. (http://msdn.microsoft.com/en-us/library/aa246472(v=vs.60).aspx)
This could be causing the mktime to fail because the tm_year variable in beg and aft are invalid.
Setting this variable to a value that represents a year after 1970 produced the intended result.
#include <stdio.h>
#include <stdlib.h>
#define _USE_32BIT_TIME_T 1
#include <time.h>
int main(void)
{
struct tm beg = {0};
struct tm aft = {0};
beg.tm_hour=0; beg.tm_min=0; beg.tm_sec=0;
beg.tm_year = 71; //Set to 1971
aft.tm_hour=11; aft.tm_min=19; aft.tm_sec=19;
aft.tm_year = 71; //Set to 1971
long long c;
c=difftime(mktime(&aft),mktime(&beg));
printf("%lld",c);
return 0;
}