top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference between two times using C?

+3 votes
223 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 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

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;
}
answer Nov 7, 2014 by Arshad Khan
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?

+2 votes

Does anyone know how to rotate a 2d matrix circularly for n times in suppose C language...? It would be a lot of help if you could explain with code.

Hint : Each time each row vector needs to be rotated one element to the right relative to the preceding row vector.

+4 votes

Suppose a programmer wants that generated binary of C program should not work after a fixed number of times(like 5 times).

...