Time between Military Hours

This is a discussion on Time between Military Hours within the C++ Programming forums, part of the General Programming Boards category; I've been writing a program that calculates the hours and minutes between 2 military times. For example the hours and ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Time between Military Hours

    I've been writing a program that calculates the hours and minutes between 2 military times.

    For example the hours and minutes between 0900 and 1730; 1730 and 0900 the next day. Now I just converted all the times to seconds because I find that to be easier.

    this is what I do..

    hours for time 1 = time1 / 100
    minutes for time 1 = time1 % 100
    calculate total no of seconds = (hours * 3600) + (minutes * 60)

    hours for time 2 = time2 / 100
    minutes for time 2 = time2 % 100
    calculate total no of seconds = (hours * 3600) + (minutes * 60)

    Now this is where I have trouble.. I know I can do it with an if statement but we aren't allowed to use that yet...

    remaining total seconds = seconds in 24 hours - abs((total num of secs for time 1 - total num of secs for time 2)

    number of minutes = remaing total seconds / 60
    number of hours = num of minutes / 60

    minutes left over = number of minutes % 60

    Then I output hours and minutes like 8 hours and 30 minutes

    So if I take away the number of seconds in a day in this: remaining total seconds = seconds in 24 hours - abs((total num of secs for time 1 - total num of secs for time 2)

    Then when I want to find the number of hours and minutes between 0900 -> 1730 I get 8 hours and 30 minutes which is correct.

    But if I do 1730 -> 0900 the next morning I still get 8 hours 30 minutes.

    And if I leaving the number of seconds in a day in I get the correct answer for 1730 -> 0900 but not for 0900 -> 1730.

    I would appreciate any help you can give me this is a puzzle for me..
    Attached Files Attached Files
    Last edited by StarOrbs; 02-26-2005 at 08:13 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    it is assumed that time 2 is greater than time 1
    (actually you need to check that this holds but since you must not use ifs you cant do the check here)

    .) convert time 1 to minutes
    .) convert time 2 to minutes

    tmp = time2- time1;

    if i am not mistaken the difference in hours an minutes would be
    hours = tmp / 60;
    minutes = tmp % 60; // where this is the remainder of the division
    signature under construction

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Thats the problem it's suppose to work when time1 is greater and less than time2.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Perhaps you could do this (in pseudocode):
    Code:
    time2 = (however you found time2);
    time2 += (1 day);
    timediff = (time2 - time1) % (1 day);
    I think that should work, though I can't really explain it. Something along the lines of... add a day to time2, so if it's smaller than time1 originally then you'll get the time difference from the next day same time, but if it's greater than time1 originally then the % (1 day) will have the effect of basically cutting off the extra 1 day you added to it. An if statement would be more intuitive to use though; the concept of this particular solution here is still a little hazy in my own mind Interesting problem.
    Last edited by Hunter2; 02-26-2005 at 11:02 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ...when time1 is greater and less than time2.
    time1 is greater and less than time2?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    And/or, as I understood it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Again, pencil and paper (or word processor) should be of help here. Assume I give you two times in terms of just military time and don't give you a date. The difference is just the larger minus the smaller. But now assume there is a date attached. If the dates are the same, then the difference is still just the larger minus the smaller. But, if the dates are different, then you need to account for that difference. So first determine which date comes firt. Subtract the time from 2400 for that date and store as part1 and add the time from the latter date to part1 to get total. If there are any full days between the two dates, account for that, too.


    DateTime date1 = Feb 28, 2005 1320
    DateTime date2 = Feb 26, 2005 1910

    date1.year == date2.year
    date1.month == date2.month
    but date1.day is after date2.day, therefore date1 is "larger"/"later"/whatever.
    So 2400 - 1910 = 450 + 1320 = 1810 but date1.day is separated from date2 by 1 full day, so need to add 2400 for a result that the two times are 4210 min apart in military time lingo or 42 h and 10 min apart in routine speak.

    Now if military time is entered as a single entity then will need to separate digits into separate hour and minute components, whereas if entries are separate to begin with, that won't be necessary.

    Now put this all in the appropriate loops and if/else control statements and create an addition/subtraction mechanism to account for base 60 instead of base 10 when it comes time to add/subtract minutes and I think the process can be accomplished.
    You're only born perfect.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>and if/else control statements
    Aye, there's the catch.
    Now this is where I have trouble.. I know I can do it with an if statement but we aren't allowed to use that yet...
    These 'no ifs' assignments seem to be quite the fad these days, although they do make life unnecessarily difficult. Perhaps, the code would execute slightly faster without having to deal with the branching, but code readability gets shot to hell (as you can see in my example above). Perhaps the idea is to test problem solving skills rather than to test good programming practices.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I've had similar situations where professors won't allow concepts that haven't been taught yet to be used in current assignments. Its usually because the teacher wants to reinforce the topic at hand. For example this question can be done easily in only a line or two with if statements, but I doubt that has anything do do with what this weeks topic is.

    Are you only given the two military times with no dates attached where its assumed that there is never more than 24 hours between two times? If so, then another way to solve this is with a for loop that starts at the first time and ends at the second time and counts the minutes until the loop is done. For example, say you were counting the number of days between two calendar dates and you've converted the dates to day number (Jan 1=1, Dec 31=365) then the loop would be like this:
    Code:
    int numdays=0;
    for(int x=day1; x!=day2; x++)
    {
         numdays++;
         x%=365;
    }
    // numdays should now have the total numbers of days between day1 and day2
    Note, I haven't tested this code so it might not work perfectly, but you should get the general idea.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't believe this offers any improvement. Rather, you'll get a semi-infinite loop when day2 < day1 (eventually the loop will stop, when the counter wraps around and comes back). Anyway, this introduces a test condition. If you could use for, you could fake an if statement like so:
    Code:
    for(; day2 < day1; )
    {
       day2 += (1 day);
       break;
    }
    I still see no solution other than the use of modulus.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Well, didn't say it was an improvement, just another idea

    It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Have to admit that I didn't read the OP close enough to pick up the "no ifs" qualification. Sorry about that.
    ____________________________________________
    >>It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line.

    True, it won't be an infinite loop, but when day2 is earlier than day1 then numdays will always == 365 - day2 at the end of the loop, not the difference between day1 and day2.
    ____________________________________________
    I don't see a way to do this if you can't use ifs and you can't be sure that a > b and you have to hard code
    result = a - b.
    You're only born perfect.

  13. #13
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Not to beat a dead horse because Hunter2's solution is more efficient than mine, but my code should work. For example, say we are finding the number of days between May 1st to April 1st which is a difference of 335 days. Ok, day1 would equal 121 and day2 would equal 91. The variable x would start at 121 and increment, when it hits 365 it will revert to zero since 365%365==0. At this point the loop would have executed 244 times thus numdays will equal 244. Now the loop will keep executing 91 more times incrementing numdays 91 more times at which point x will equal day2. So numdays will have a grand total of 244+91=335 which is the correct answer.
    Last edited by PJYelton; 02-28-2005 at 05:30 PM.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line.
    Oops, missed the mod line. Guess it does work after all

    >>will always == 365 - day2
    No, it will be the number of days from day2 to day1 the next year - the loop condition is !=, not <.

    I don't see a way to do this if you can't use ifs and you can't be sure that a > b and you have to hard code
    result = a - b.
    Does my solution not work? It seemed to work for the 2 test cases I worked out on paper.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    As far as I can tell your solution works and is quick and efficient. I doubt there's a better solution than that. Only problem is if he's rarely worked with modulus and he has to turn around and explain his reasoning to his teacher

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 09:08 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM

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