使用 'HH:MM:SS' 格式计算从日期开始经过的时间(以秒为单位)( MATLAB)

Calculate the elapsed time in seconds from date with 'HH:MM:SS' format ( MATLAB)

我正在尝试将字符串中的日期转换为时间向量,格式为:'HH:MM:SS',在 seconds.It 中的时间向量中,在某个时间点之前和之后都可以正常工作它 returns 假值。

这是我正在做的事情:

a={'596:30:18';'596:30:28';'596:30:38';'596:30:48';'596:30:58';'596:31:08';'596:31:18';'596:31:28';'596:31:38';'596:31:48';'596:31:58';'596:32:08';'596:32:18';'596:32:28';'596:32:38';'596:32:48';'596:32:58';'596:33:08'};

         formatIn = 'HH:MM:SS';
         DateVector = datevec(a(1:end,:),formatIn);
         time = etime(DateVector,DateVector(1,:));    

它returns:

DateVector =

2017    1   25  20  30  18
2017    1   25  20  30  28
2017    1   25  20  30  38
2017    1   25  20  30  48
2017    1   25  20  30  58
2017    1   25  20  31  8
2017    1   25  20  31  18
2016    12  7   3   28  40,7040000000000
2016    12  7   3   28  50,7040000000000
2016    12  7   3   29  0,704000000000000
2016    12  7   3   29  10,7040000000000
2016    12  7   3   29  20,7040000000000
2016    12  7   3   29  30,7040000000000
2016    12  7   3   29  40,7040000000000
2016    12  7   3   29  50,7040000000000
2016    12  7   3   30  0,704000000000000
2016    12  7   3   30  10,7040000000000
2016    12  7   3   30  20,7040000000000

time =

0
10
20
30
40
50
60
-4294897,29600000
-4294887,29600000
-4294877,29600000
-4294867,29600000
-4294857,29600000
-4294847,29600000
-4294837,29600000
-4294827,29600000
-4294817,29600000
-4294807,29600000
-4294797,29600000

只有第 7 个值是正确的。有人知道为什么会这样吗?

我能想到的唯一解释是 HH 标识符不适用于 3 位数小时。实际上 datevec 的 datevec documentation

HH - Hour in two digits format

因此,'596:30:28' 可能不是有效的日期字符串,您需要将其转换为其他格式,例如'dd:HH:MM:SS'.

可以这样做(假设所有时间都是三位数):

a={'596:30:18';'596:30:28';'596:30:38';'596:30:48';'596:30:58';'596:31:08';'596:31:18';'596:31:28';'596:31:38';'596:31:48';'596:31:58';'596:32:08';'596:32:18';'596:32:28';'596:32:38';'596:32:48';'596:32:58';'596:33:08'};

a_ddhhmmss = {};
for ii=1:length(a)
    datestring = a{ii};
    colon_indices = strfind(datestring, ':');  %check how many digits the hours have
    hours_digits = colon_indices(1)-1;
    hours = str2num(datestring(1:hours_digits));
    days = floor(hours/24) + 1; % start with day=1
    hours = rem(hours,24);
    a_ddhhmmss{end+1} = [num2str(days), ':', num2str(hours), datestring(4:end)];
end

formatIn = 'dd:HH:MM:SS';
DateVector = datevec(a_ddhhmmss(1:end,:),formatIn)
time = etime(DateVector,DateVector(1,:))

当然,您观察到的行为只是在日期 '596:30:18''596:30:28' 之间设置,这仍然没有任何意义。即使 datevec 函数超出范围使用,它也应该抛出错误而不是返回无意义的输出。

这是另一种没有循环且速度更快(理论上)的方法:

B = regexp(a, ':', 'split');
C = vertcat(B{:});
D = str2double(C);  %D = cellfun(@str2num, C); also works but slower

这会将您的时间戳转换为 3 列中的 Hours:Minutes:Seconds,然后您可以很容易地减去并得到时间:

E =  bsxfun(@minus,D,D(1,:));
F = E(:,1)*3600 + E(:,2)*60 + E(:,3)

>>F =

 0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170

顺便说一句,多么奇怪的日期时间格式。

您可以通过首先使用 datenum and datetime, subtracting the first time point, and converting to seconds:

转换 a 来轻松完成此操作
D = datetime(datenum(a), 'ConvertFrom', 'datenum');  % A datetime array
secElapsed = seconds(D-D(1))

secElapsed =

     0
    10
    20
    30
    40
    50
    60
    70
    80
    90
   100
   110
   120
   130
   140
   150
   160
   170