How to find the difference between two times using ANSI C
Here I have attached the C code, which gives some idea about taking difference between two given times. Final result is giving as Years. You can change the values which given inside the parenthesis - (60*60*24*365)
Here I have attached the C code, which gives some idea about taking difference between two given times. Final result is giving as Years. You can change the values which given inside the parenthesis - (60*60*24*365)
#include <time.h>
#include <stdio.h>
int main(void)
{
struct tm t,tt;
time_t t_of_day,tt_of_day;
double dif;
t.tm_year = 2009-1900;
t.tm_mon = 0;
t.tm_mday = 3;
t.tm_hour = 0; /* hour, min, sec don't matter */
t.tm_min = 0; /* as long as they don't cause a */
t.tm_sec = 1; /* new day to occur */
t.tm_isdst = 0;
tt.tm_year = 2007-1900;
tt.tm_mon = 0;
tt.tm_mday = 3;
tt.tm_hour = 0; /* hour, min, sec don't matter */
tt.tm_min = 0; /* as long as they don't cause a */
tt.tm_sec = 1; /* new day to occur */
tt.tm_isdst = 0;
t_of_day = mktime(&t);
tt_of_day = mktime(&tt);
dif = difftime (t_of_day,tt_of_day);
// You can change the outcome by changing following line
dif = dif /(60*60*24*365);
printf("Time Difference between %s and %s is %.2lf \n",ctime(&t_of_day),ctime(&tt_of_day),dif);
return 0;
}
Comments
Post a Comment