Manipulating datetime and datevector values: Adding millions of seconds

Hi,
Problem Background:
I have a large array (500k to 1M) of timestamps that I'm converting to datevectors using datevec(). These timestamps correspond to video frame timestamp metadata, so this data has a resolution down to the millisecond. However, there were glitches in certain frames, and the timestamp data from that frame and subsequent frames was shifted to a time of several months earlier than the actual date and time. I am calculating the elapsed time between the nframe (jump frame) and nframe-1 (last frame with the correct timestamp) using etime(), which gives me an answer in seconds. For an elapsed time 'et' of several months, this value can be several+ millions of seconds.
Problem Description:
What I need to do is add 'et' to nframe's datetime value. The problem is 'et' is in seconds, and I'm working with datevectors and datatime data types. How do I explicitly add millions of seconds to nframe's datetime or datevector 'seconds' value, and have the datevector or datetime datatype automatically shift the months/days/hours/minutes/seconds values to accomidate the addition of millions of seconds?
Much thanks,
Brad

 Accepted Answer

Solution:
First, convert timestamp array to datevector array DV(nframe,:) using datevec().
Then, convert datevector array to datetime array DT(nframe) using datetime().
Next, use between() to calculate the time difference between DT(nframe) and DT(nframe-1), where 'nframe' = specific jump frame number.
So, dt = between(DT(nframe),DT(nframe-1)).
Finally, DT_Fixed(nframe) = DT(nframe) + dt + 1/framerate.

More Answers (1)

Unless you specifically want date vectors, you are probably better off working entirely with datetimes. Since date vectors store whole+fractional seconds, you cannot represent a millisecond exactly, and you may run into unexpected round-off issues when converting back and forth.
The purpose of datetime is to replace the tirad of datenum/datevec/datestr, and avoid all the conversions.
Hope this helps.

Asked:

on 27 Oct 2015

Commented:

on 28 Oct 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!