Time difference

Hi there,

Here is a simple but challenging problem... How do I deal with it?

Say I have a number of dates and times in a file, for example:

2008/11/22 14:59 ... t1
2008/11/22 15:05 ... t2
2008/11/22 15:12 ... t3
2008/11/22 15:18
...

These values can then be read into an array/vector, separating year, month, day and time (somehow...). Now, what I want is the difference between successive time intervals (in seconds) for a calculation, i.e. t2 - t1, t3 - t1 and so on. This presents two problems:

1) How could time (in the form of HH:SS above) be represented in the array, so that they can be subtracted?
2) If the clock transitions from a 50-something to a "single digit" the difference is a huge negative number. (eg. 05 - 59 = -54). What now?

Thanks in advance :)
Last edited on
Hmm i haven't leanrt anything about file I/O yet so sorry if this is a bit shady but could you not read the line till you get a ':' and take the two values before and after the ':'? And then you could have two parallel arrays so that the HH and SS correspond with eachother e.g. arrayHH[0] goes with arraySS[0].

just a thought :)
Boost.Date_Time will parse date/time strings into date & time objects and calculate differences. It handles all the fun leap-year magic so you don't have to. Of course this won't help you if it's a school assignment. If you are on Unix/Linux, strptime() will parse the date and time. It's the inverse of strftime(). It is not available on Windows.

The problem is typically solved by converting everything to and from seconds. There may be limits on the number of seconds you can count.

Thanks for the advice. Although it would be possible to put the time into 2 separate arrays, how would this help? PanGalactic, I'm not familiar with parsing yet, although I have heard of it. The project is an assignment and the time difference is always in the range of 6-7s. Is there any easier way to solve this problem? I still feel pretty much totally lost on this one.
Could you not manually convert HH element values into seconds at the time you add them to the array. Then you can just minus one from the other.
Topic archived. No new replies allowed.