Converting Timestamps in Microsconds to DateTime Format
44 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Peter Perkins
2012-4-10
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
Peter Perkins
2012-4-11
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!