'How to get the time difference between two times (24h format)

I am currently trying to create a program where the user gives two values (times, hh:mm:ss) and gets the difference between the two times. This works, if one would only use 12h formats; however, using the 24h format is a must.

My current time struct looks like the following:

typedef struct time {
    int hours;
    int minutes;
    int seconds;
} time;

And my current function to calculate the difference looks like this:

time calculateTimeDiff(time time1, time time2) {
    time timeResult;

    timeResult.hours = time1.hours - time2.hours;

    if(time1.minutes != 00 && time2.minutes != 00) {
            timeResult.minutes = time1.minutes - time2.minutes;
    }
    else {
            timeResult.minutes = 00;
    }

    if(time1.seconds != 00 && time2.seconds != 00) {
            timeResult.seconds = time1.seconds - time2.seconds;
    }
    else {
            timeResult.seconds = 00;
    }

    while(timeResult.seconds > 60) {
        timeResult.seconds -= 60;
        timeResult.minutes += 1;
    }

    while(timeResult.minutes > 60) {
        timeResult.minutes -= 60;
        timeResult.hours += 1;
    }

    return timeResult;
}

My attempts to support the 24h format have been to add 12 hours to the time if the hours "exceed" the 12 hour format, and to divide the time by two (haven't been far from complete shots in the dark, just to see what works and what wouldn't work). However, this has only resulted in getting incorrect results.

Any and all answers appreciated!

c


Solution 1:[1]

How to get the time difference between two times (24h format)

Although code could use many if-then-else's as in OP's code, it would be simple to convert the h:m:s time into seconds, subtract, and convert back to h:m:s. So I recommend a re-write:

typedef struct time {
    int hours;
    int minutes;
    int seconds;
} time;

long time_sec(time t) {
  return (t.hours * 60L + t.minutes)*60 + t.seconds;
}

time sec_time(long s) {
  time t;
  t.hours = s / 3600;
  s %= 3600;
  t.minutes = s / 60;
  t.seconds = s %= 60;
  return t;
}

time calculateTimeDiff(time time1, time time2) {
  long t1 = time_sec(time1);
  long t2 = time_sec(time2);
  long diff = t1 - t2;
  return sec_time(diff);
}

#include <stdio.h>
void test(time t1, time t2) {
  printf("t1: %3d:%3d:%3d,    ", t1.hours, t1. minutes, t1.seconds);
  printf("t2: %3d:%3d:%3d,    ", t2.hours, t2. minutes, t2.seconds);
  time t3 = calculateTimeDiff(t1, t2);
  printf("t1-t2: %3d:%3d:%3d,   ", t3.hours, t3. minutes, t3.seconds);
  t3 = calculateTimeDiff(t2, t1);
  printf("t2-t1: %3d:%3d:%3d\n", t3.hours, t3. minutes, t3.seconds);
}

int main(void) {
  test((time){14,00,00}, (time){13,00,00});
  test((time){22,00,00}, (time){04,00,00});
}

Output

t1:  14:  0:  0,    t2:  13:  0:  0,    t1-t2:   1:  0:  0,   t2-t1:  -1:  0:  0
t1:  22:  0:  0,    t2:   4:  0:  0,    t1-t2:  18:  0:  0,   t2-t1: -18:  0:  0

Note that the difference may result in negative values for the members of time returned in calculateTimeDiff().

Solution 2:[2]

You can change to time structure instead of both start and end time integer value

#include<iostream>
using namespace std;
void difftime(int startTime, int iRecordEndTime)
{
    int duration_min = 0;
    cout<<"iRecordEndTime = "<<iRecordEndTime<<endl;
    cout<<"startTime      = "<<startTime<<endl;
    if ( (iRecordEndTime/100 < startTime/100) || ((iRecordEndTime/100 == startTime/100) && (iRecordEndTime%100 <= startTime%100)) )
    {
        duration_min = ((iRecordEndTime / 100 + 24)*60 + iRecordEndTime%100) - ((startTime/100)*60 + startTime%100);
    }
    else
    {
        duration_min = (iRecordEndTime / 100 - startTime/100)*60 + (iRecordEndTime%100 - startTime%100);
    }
    cout<<"duration_min = "<<duration_min<<endl;
}

int main()
{
    cout<<"enter the start time Hour:Minutes xxxx"<<endl;
    int startTime{0};
    cin>>startTime;
    cout<<"enter the End time Hour:Minutes xxxx"<<endl;
    int iRecordEndTime{0};
    cin>>iRecordEndTime;
    difftime(startTime, iRecordEndTime );
}

Output
    enter the start time Hour:Minutes xxxx\
    2300\
    enter the End time Hour:Minutes xxxx\
    0000\
    iRecordEndTime = 0\
    startTime      = 2300\
    duration_min = 60

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2