Converting Timestamps in Microsconds to DateTime Format

44 views (last 30 days)
Hello,
I am new in matlab and I need some help please!
I have a text file that contains timestamps out of a camera that captures 50 frames per second ..The data are as follows:
1 20931160389
2 20931180407
3 20931200603
4 20931220273
5 20931240360
.
.
50 20932139319
... and so on.
It gives also the starting time of capturing like
Date: 02.03.2012 17:57:01
The timestamps are in MICROSECONDS not in milliseconds, and matlab can support only till milliseconds but its OK for me.
Now I need to know the human format of these timestamps for each row..like
1 20931160389 02.03.2012 17:57:01.045 % just an example
2 20931180407 02.03.2012 17:57:01.066
3 20931200603 02.03.2012 17:57:01.083
4 20931220273 02.03.2012 17:57:01.105
5 20931240360 02.03.2012 17:57:01.124
and so on
I tried this:
% Reference Data
clc; format longg
refTime = [2012,03,02,17,57,01];
refNum = datenum(refTime);
refStr = datestr(refNum,'yyyy-mm-dd HH:MM:SS.FFF');
% Processing data
dn = 24*60*60*1000*1000; % Microseconds! I have changed this equation to many options but nothing was helpful
for i = 1 : size(Data,1)
gzTm = double(Data{i,2}); %timestamps are uint64
gzTm2 = gzTm / dn;
gzTm2 = refNum + gzTm2;
gzNum = datenum(gzTm2);
gzStr = datestr(gzNum,'yyyy-mm-dd HH:MM:SS.FFF'); % I can't use 'SS.FFFFFF'
fprintf('i = %d\t Timestamp = %f\t TimeStr = %s\n', i, gzTm, gzStr);
end;
But I got always strange outputs like
i = 1 Timestamp = 20931160389.000000 TimeStr = 2012-03-08 13:29:28.849
i = 2 Timestamp = 20931180407.000000 TimeStr = 2012-03-08 13:29:29.330
i = 3 Timestamp = 20931200603.000000 TimeStr = 2012-03-08 13:29:29.815
i = 4 Timestamp = 20931220273.000000 TimeStr = 2012-03-08 13:29:30.287
i = 5 Timestamp = 20931240360.000000 TimeStr = 2012-03-08 13:29:30.769
Could one help me please..! Where is my mistake?
Thanks in advance
Sam

Answers (2)

Peter Perkins
Peter Perkins on 10 Apr 2012
Sam, If I understand what you're trying to do, this may be what you are looking for:
>> refTime = datenum([2012,03,02,17,57,01]);
>> datestr(refTime,'yyyy-mm-dd HH:MM:SS.FFF')
ans =
2012-03-02 17:57:01.000
>> timeStamps = [1 20931160389; 2 20931180407; 3 20931200603; 4 20931220273; 5 20931240360; 50 20932139319];
>> times = refTime + timeStamps(:,2)/(86400*1e6);
>> datestr(times,'yyyy-mm-dd HH:MM:SS.FFF')
ans =
2012-03-02 23:45:52.160
2012-03-02 23:45:52.180
2012-03-02 23:45:52.201
2012-03-02 23:45:52.220
2012-03-02 23:45:52.240
2012-03-02 23:45:53.139
  1 Comment
Sam
Sam on 10 Apr 2012
Thanks Perkins for vectorizing this to me! It's great!
But may be I should clarify my problem more...
The time gap between each entry in the array should be nearly 20 seconds..since I have 50 frames per second(1000 millisecond / 50 = 20) which is true in your code..but the hour,minute and seconds should also indicate the initial time given as reference time because it is about some seconds earlier.
refTime = datenum([2012,03,02,17,57,01])
The output time is about six hours late than the Referenced Time
2012-03-02 23:45:52.160
which is wrong!
I expect something like:
1 20931160389 02.03.2012 17:57:01.045 % just an example
2 20931180407 02.03.2012 17:57:01.066
How you could modify your code to accomplish this?
thanks again!

Sign in to comment.


Peter Perkins
Peter Perkins on 11 Apr 2012
You might expect that, but according to what you seemed to be describing, your timestamps represent microsecond offsets from your reference time. For example, 20931160389 microseconds is
>> 20931160389 / (1e6*60*60)
ans =
5.8142
a little less than 6 hours. And clearly if your data are in microseconds, then they are not about 20 seconds apart. I think you mean .02s.
I can't tell you what your data mean. A datenum in MATLAB represents days and fractional days. To add x microseconds to that, you have to scale x by (86400*1e6). That's what my code does. If your data are something other than microsecond offsets, then change the code I suggested accordingly.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!