Thread: Time between Military Hours

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hehehe, after some thinking I came up with an even more twisted solution:
    Code:
    int diff = ((abs(day2 - day1)/(day2 - day1) + 1) / 2 * 365 + day2) - day1
    In more bite-sized pieces (or 4-bite pieces, depending on platform):
    Code:
    int diff = day2 - day1;
    int sign = abs(diff) / diff;
    int shouldWeAdd365OrNot = (sign + 1) / 2;  //1 for yes, 0 for no
    int correctedDay2 = day2 + (shouldWeAdd365OrNot * 365);
    
    int finalDiff = correctedDay2 - day1;
    Of course, abs() probably uses if internally, but since it's apparently allowed for the assignment.. heck, why not? And, as far as I can see, it strictly uses features that have already been demonstrated to be allowed (i.e. no mod involved, in case that's banned).
    Last edited by Hunter2; 02-28-2005 at 05:47 PM. Reason: Took out the stupid Simpsons reference..
    Just Google It. √

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

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I admit my statement "always == 365 - day2" was wrong, and I admit if day1 is May 1st and day2 is April 1st of the preceding year, then the difference is 335 days. However, if day1 and day2 are in the same year, then the difference isn't 335, it's 30. That's the labeling problem identified earier, and if an if statement isn't possible then this approach won't work when in cases when dates are in same year and when dates are in different years.


    Likewise, let's say that time1 is 6AM and time2 is 9AM on the same day. Ideally, you'd say 9 - 6 = 3 and you'd be correct. But, what if time1 is 9AM and time2 is 6AM on the same day and your code is hardcoded to use time2 - time1. Now 6 - 9 = -3, which isn't correct, but you could use absolute value to get the correct answer, though there are problems with this approach for times that aren't on the same day. How about using the following with time1 = 9AM and time2 = 6AM of the same day (so we know without a doubt the difference in the two times is 3 hours)?

    time1 = 9
    time2 = 6
    time2 += 24 means time2 = 6 + 24 = 30;
    timediff = (time2 - time1) % 24 = (30 - 9) % 24 = 21 % 24 = 21??????????

    Which is why I said I can't see a solution that works for all cases of time1 and time2 without using an if statement somewhere.

    Edit : I haven't looked at the latest post closely yet, and it's getting busy here at work, so I may not be able to post on that til later.

    Edit : Just a brief run I guess. So, my analysis of Hunter2s latest post.

    Given:
    day1 = day 200 of yr a;
    day2 = day 100 of yr a;

    The actual difference is 100 days because number of days can't be negative.

    Calculated distance per Hunter2 last post:
    int diff = day2 - day1 = 100 - 200 = -100;
    int sign = abs(diff) / diff = 100/-100 = -1;
    int shouldWeAdd365OrNot = (sign + 1) / 2 = (-1 + 1)/2 = 0/2 = 0; //1 for yes, 0 for no
    int correctedDay2 = day2 + (shouldWeAdd365OrNot * 365) = 100 + (0 * 365) = 100;

    int finalDiff = correctedDay2 - day1 = 100 - 200 = -100????????
    Last edited by elad; 03-01-2005 at 11:57 AM.
    You're only born perfect.

  3. #18
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I think the confusion lies in what it means for time2<time1. As far as I can tell, if the numerical value of time2 is less than time1 it is assumed that it is the next day. So at no point would 9am to 6am be three hours but will always be 21 hours in which case both hunter's and my code will always work. If this is not the case than I'm not sure how to solve the problem because the OP didn't tell us how the program is to know if its the same day or not.

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>int finalDiff = correctedDay2 - day1 = 100 - 200 = -100????????
    Whoops. The problem lies in:
    Code:
    int shouldWeAdd365OrNot = (sign + 1) / 2 = (-1 + 1)/2 = 0/2 = 0;
    Since this should be 1 when time2 < time1, i.e. sign == -1, this should be:
    Code:
    int shouldWeAdd365OrNot = ((-sign + 1) / 2);  //1 for yes, 0 for no
    but you could use absolute value to get the correct answer, though there are problems with this approach for times that aren't on the same day.
    Indeed, I made the assumption that negative time differences are always interpreted as time2 being the next day, since given the input format there would be no way to determine the intention if the other interpretation were possible.
    Just Google It. √

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

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, 08:08 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04: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