# Difference between two times using C?

58 views

I want to calculate the difference between two times i.w. time1 and time2 which are in a array.

int time1[6]
int time2[6]

First index is the second, second is the minute, third is the hour, fourth is the day, fifth is the month and last is the year. Any suggestion about this

posted Nov 7, 2014

Here is my code to achieve this.
Here I am considering time1 < time2, and the number of days in feb month is 29, for exact number of day in feb month you have to do some calculation based on the year.

``````#include <stdio.h>

int main()
{
int time1[6] = {55, 59, 12, 3, 6, 1980};
int time2[6] = {56, 23, 23, 7, 2, 2014};
int no_day_in_month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int tmp[6];
int i;

for (i = 0; i < 6; i++) {
if (time1[i] > time2[i]) {
switch (i) {
case 0:
case 1:
time2[i] += 60;
time2[i+1] -= 1;
break;
case 2:
time2[i] += 24;
time2[i+1] -= 1;
break;
case 3:
time2[i] += no_day_in_month[time2[i+1]];
time2[i+1] -= 1;
break;
case 4:
time2[i] += 12;
time2[i+1] -= 1;
break;
}
}

tmp[i] = time2[i] - time1[i];

}

for (i = 0; i < 6; i++)
printf("%d\n", tmp[i]);

return 0;
}
``````
Similar Questions
+1 vote

Can someone help me with the example when to use normal function calling and when pointer function calling. What was the reason for introducing pointer function calling in C?