Thread: Easy(?) Problem.

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    Easy(?) Problem.

    I have been writing a program and I came across a problem and have scrapped my code so many times, here is an example of what im trying to do.

    I have the date and time setup, say for example the 6th of October at 21:57.

    I wrote the code to subtract that date from the current date to give me how long its been since, it worked fine till it ticked over midnite, then it said 1 day! hehe i can see why it said one day, as "7 - 6 = 1", but it really has only been 2 hours and 3 minutes. Any suggestions on how to do this to actually show the correct amount of time since, i wrote some more code but that seemed to get screwed up somewhere, I'm trying to make the code short and sweet, because the idea i have in my mind is a long idea, where i get the total amount of hours and work from there, as you can see.. ouch! hehe So any of you got a good solution or if you find this really easy which I bet most of you would post me some code examples?

    Thankyou for you time.
    Scorpion-ice
    Imperium et Respectus

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Whu don't you post some (commented code)code, so I can see what's the problem.

    I think you can make use of the % operator, but I'm not sure.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    hmz, surely that would give the same effect as when the time goes over 3hrs (past midnight) that the day would = 1, or did u mean convert it all into seconds and then work out how many hours are within that, and from the hours work out how many days have past etc?

    If possible could you supply an example.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Understandably, the logic can get murky here until you break it down into manageable parts.

    Given that you've taken Salem's suggestion of finding the difference in times, you'll want to determine if that difference is greater than 24 hours (86,400 seconds) - the whole number of days that may have passed.

    Assuming the difference is less than 24 hours, you'll need to determine if the starting time plus the difference exceeds 2400 hours (midnight). If not, you're in good shape, day-wise.

    If it does run past 2400 hours, you'll have to increment the day while limiting your output to hours/minutes/seconds. (This is why I would recommend determining the whole number of days that may have passed first. That is, if exactly 86,400 seconds have elapsed, the "clock" will show the same as your original start time, though one day will have gone by.)

    Offhand, I would suggest that you write two separate functions. One to handle time differences of greater than 24 hours and one for less than 24 hours. The handy part of this is that you can call the "less than" function from the "greater than" function to handle portions of a day without duplicating your code.

    Now, without trying to turn this into a bigger problem for you than it is, have you determined how you'll handle potential changes in the month? Let's say it's the 31st of October rather than the 6th. Food for thought?

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    Why don't you do

    (7-1) - 6

    Then it will have to cycle thru a whole day before your output = 1
    --Cid666

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    I'm not certain I understand where you're going, cid666.

    For example:

    1st Start date/time:
    October 6 / 21:00:00 hours

    Elapsed time:
    23 hours

    1st End date/time:
    October 7 / 20:00:00 hours

    2nd Start date/time:
    October 6 / 21:00:00 hours

    Elapsed time:
    25 hours

    2nd End date/time:
    October 7 / 22:00:00 hours

    I inferred from Scorpion-ice's original post that the number of days was being determined by subtracting the start day from the end day, which isn't appropriate.

    Your solution gets by for my first example, but not the second.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. Relatively easy math problem...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-18-2005, 08:48 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM