# Thread: Easy way to compare two times

1. ## Easy way to compare two times

I'm writing a program to compare two times. The user inputs the start time in a hour:minute:second type of way, that gets converted to seconds.
Next the user inputs a finish time, in the same manner. This also gets converted to seconds.

So,
difference = finish_time - start_time
and here's where I would like for it to format the output to reflect the hour:minute:second format. And I can't get it to work.
If the difference is >= 3600 then it need to increment the [hour], if it's less than 3600 it needs to compare it to 60, in order to get the [minutes], and whatever is left over is sent to [seconds].

Code:
```    if (run[id-1].etime >= 3600) {
++run[id-1].ffh;
run[id-1].etime = run[id-1].etime - 3600;
}

else if (run[id-1].etime >= 60) {
++run[id-1].ffm;
run[id-1].etime = run[id-1].etime - 60;
}
else
run[id-1].ffs = run[id-1].etime;```
I don't even know if I'm on the right track here.. All I know is that there is a loop missing and I've tried getting this to work with a for loop but it just subtracts once and exits the loop.. How could I get it to convert the etime value to a hour:minute:second format?

Newbie, so please be gentle, and try to explain thing as you would to a five-year-old..

I think I got it..

Code:
```    for(;;) {
if (run[id-1].etime >= 3600) {
++run[id-1].ffh;
run[id-1].etime = run[id-1].etime - 3600;
printf("hours: %d\n", run[id-1].etime);
continue;
}

else if (run[id-1].etime >= 60) {
++run[id-1].ffm;
run[id-1].etime = run[id-1].etime - 60;
printf("minutes: %d\n", run[id-1].etime);
continue;
}
else
run[id-1].ffs = run[id-1].etime;

break;
}```
Something like that.. It seems to work, FINALLY!
I used the printf's to see how the calculations took place..!

2. You could just use simple division.
Code:
`hours = seconds / 60 / 60;`
Since you want to limit the minutes to 60, you can use the modulus operator:
Code:
`minutes = (seconds / 60) % 60;`
And to get the seconds (from 0-59), use only the modulus:
Code:
`numseconds = totalseconds % 60;`
Modulus returns the remainder of division. So modulus 5 will be a number from 0-4, because that's the only remainder you could have from dividing a number by 5. See the tutorial for the modulus operator: http://www.cprogramming.com/tutorial/modulus.html

3. The way you're doing it would be like multiplying like this:
Code:
```/* multiplying a * b */

for(x = 0; x < a; x ++) {
c += b;
}```
You don't need a loop at all, just some division and modulus.

4. I tried the modulus operator but I never got that working to my favour.. Anyway, it works as it is now, I believe.

Thanks :-)

However, I have another question about scanf. I had to subract one from the id since otherwise I would have a record that starts with 0, and the program is supposed to be somewhat userfriendly so the elements of the structure needs to start a 1. Can I subract 1 from scanf immediately at the time the program scans the number, instead of using id-1 everywhere?

Hope that was clear..?

5. I still think modulus would be better . . . What did you try?

However, I have another question about scanf. I had to subract one from the id since otherwise I would have a record that starts with 0, and the program is supposed to be somewhat userfriendly so the elements of the structure needs to start a 1. Can I subract 1 from scanf immediately at the time the program scans the number, instead of using id-1 everywhere?
Yes, of course you can.
Code:
`id = id - 1;`
or
Code:
`id -= 1;`
or
Code:
`id --;`

6. Originally Posted by dwks
I still think modulus would be better . . . What did you try?

Yes, of course you can.
Code:
`id = id - 1;`
or
Code:
`id -= 1;`
or
Code:
`id --;`
You can see my solution at the bottom of the first post. I edited it after I got the loop working.

I'll give you an example with the id-1 thingie:
Code:
```    printf("Enter ID: ");
scanf("%d", &id);
printf("Details for id #%d: \n", id);
printf("Name: %s\n", run[id-1].name);```
So I would just be able to subract 1 after the line
Code:
```printf("Details for id #%d: \n", id);
id--;```
and change
Code:
```printf("Name: %s\n", run[id-1].name);
to
printf("Name: %s\n", run[id].name);```
Would that be correct?

7. Exactly. All the statements I posted are equivalent; they subtract one from id.

 As for your loop, try just this:
Code:
```total_seconds = whatever;

hours = total_seconds / 3600;
minutes = total_seconds / 60 % 60;
seconds = total_seconds % 60;```
[/edit]

8. Originally Posted by dwks
Exactly. All the statements I posted are equivalent; they subtract one from id.

 As for your loop, try just this:
Code:
```total_seconds = whatever;

hours = total_seconds / 3600;
minutes = total_seconds / 60 % 60;
seconds = total_seconds % 60;```
[/edit]
Great! I'll give both of your suggestions a go.
Thanks, again :-)