Easy way to compare two times

This is a discussion on Easy way to compare two times within the C Programming forums, part of the General Programming Boards category; I'm writing a program to compare two times. The user inputs the start time in a hour:minute:second type of way, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    21

    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..!
    Last edited by Machiaveli; 10-04-2006 at 12:41 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    21
    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. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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 --;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    21
    Quote 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. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Exactly. All the statements I posted are equivalent; they subtract one from id.

    [edit] 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]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    21
    Quote Originally Posted by dwks
    Exactly. All the statements I posted are equivalent; they subtract one from id.

    [edit] 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 :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Times function for rational numbers
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 04:09 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  4. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  5. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21