Hi everyone -
I have a time stamp in the following form : '0d 00:00:5.03000020980835'
I am trying to convert it into a vector in hh:mm:ss, any ideas?

2 件のコメント

Walter Roberson
Walter Roberson 2019 年 6 月 17 日
Do you need the seconds to come out as 5.03 with no 000020980835 stored?
Adam's suggestion is not bad, but it does end up storing as 00:00:05.030000208 (I might have expected 00:00:05.030000210) . If the 0000208 is not desired, then Adam's code would need to be modified.
Question: is the number of days ever non-zero ?
Sergio Mendoza
Sergio Mendoza 2019 年 6 月 17 日
編集済み: Sergio Mendoza 2019 年 6 月 17 日
The number of milliseconds is not critical, 5.03 is what I would expect. There is a caveat and that is that for some entries, there are no milliseconds recorded:
'0d 00:00:0'
'0d 00:00:1'
'0d 00:00:2'
'0d 00:00:3'
'0d 00:00:4'
'0d 00:00:5'
'0d 00:00:5.03999996185303'
'0d 00:00:6.03999996185303'
'0d 00:00:7.03999996185303'
'0d 00:00:8.03999996185303'
'0d 00:00:9.03999996185303'
Also, the number of days is not always zero

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2019 年 6 月 17 日
編集済み: Adam Danz 2019 年 6 月 17 日

1 投票

dtstr = '0d 00:00:5.03000020980835';
dtstr = regexprep(dtstr,'\d+d',''); %remove days
dt = datetime(dtstr,'InputFormat','HH:mm:ss.SSS','Format','HH:mm:ss')
[update]
To preserve the number of days within the hour-count and to account for missing decimals in the seconds,
dtstr = {'0d 23:59:58.439998626709'
'0d 23:59:59'
'1d 00:00:0'
'1d 00:00:1.44000005722046'
'1d 00:00:2.44000005722046'
'1d 00:00:3.44000005722046'};
% Add .0 to time stamps that are missing the decimal
noDecIdx = ~cellfun(@(x)contains(x,'.'),dtstr);
dtstr(noDecIdx) = cellfun(@(x)[x,'.0'],dtstr(noDecIdx),'UniformOutput',false);
% replace "d " with ":"
dtstr = strrep(dtstr,'d ',':');
% Convert to your desired format as durations
D = duration(dtstr,'InputFormat', 'dd:hh:mm:ss.S','Format','hh:mm:ss');
Result:
6×1 duration array
23:59:58
23:59:59
24:00:00
24:00:01
24:00:02
24:00:03

7 件のコメント

Sergio Mendoza
Sergio Mendoza 2019 年 6 月 17 日
This works great - How about if there are some entires with different format?
for example no milliseconds:
'0d 00:00:01'
Adam Danz
Adam Danz 2019 年 6 月 17 日
編集済み: Adam Danz 2019 年 6 月 17 日
What about apending ".0" to each datetime string that is missing a decimal and changing the number of expected decimals to 1?
dtstr = {'0d 00:00:0'
'0d 00:00:1'
'0d 00:00:2'
'0d 00:00:3'
'0d 00:00:4'
'0d 00:00:5'
'0d 00:00:5.03999996185303'
'0d 00:00:6.03999996185303'
'0d 00:00:7.03999996185303'
'0d 00:00:8.03999996185303'
'0d 00:00:9.03999996185303'};
noDecIdx = ~cellfun(@(x)contains(x,'.'),dtstr);
dtstr(noDecIdx) = cellfun(@(x)[x,'.0'],dtstr(noDecIdx),'UniformOutput',false);
% My first response
dtstr = regexprep(dtstr,'\d+d',''); %remove days
dt = datetime(dtstr,'InputFormat','HH:mm:ss.S','Format','HH:mm:ss')
% Except with only 1 S-----^
There might be a more direct format approach that I'm not aware of that takes care of these siturations.
Adam Danz
Adam Danz 2019 年 6 月 17 日
編集済み: Adam Danz 2019 年 6 月 17 日
This version is simpler than my comment above but completely disregards the decimals and you lose any rounding.
dtstr = {'0d 00:00:0'
'0d 00:00:1'
'0d 00:00:2'
'0d 00:00:3'
'0d 00:00:4'
'0d 00:00:5'
'0d 00:00:5.03999996185303'
'0d 00:00:6.03999996185303'
'0d 00:00:7.03999996185303'
'0d 00:00:8.03999996185303'
'0d 00:00:9.03999996185303'};
dtcell = regexp(dtstr, '\d{2}:\d{2}:\d+','match');
dt = datetime([dtcell{:}]','InputFormat','HH:mm:ss','Format','HH:mm:ss')
Sergio Mendoza
Sergio Mendoza 2019 年 6 月 17 日
Thanks Adam -
How about for those instances when the number of days is non zero?
Currently once it reaches 24 hr. it resets to zero and starts again, any ideas?
Adam Danz
Adam Danz 2019 年 6 月 17 日
Have you tried it out? It works for me as-is. I tested the string below.
'5d 00:00:5.03000020980835';
Sergio Mendoza
Sergio Mendoza 2019 年 6 月 17 日
Yes, it works, but what I woudl like is that once the day rolls into a new one, I can still keep track of the total amount of hours ellapsed.
'0d 23:59:58.439998626709'
'0d 23:59:59.439998626709'
'1d 00:00:0.439999997615814'
'1d 00:00:1.44000005722046'
'1d 00:00:2.44000005722046'
'1d 00:00:3.44000005722046'
So once 1d is on, I want the hours to turn to 24 and keep going.
Thanks!
Adam Danz
Adam Danz 2019 年 6 月 17 日
Hi Sergio, I updated my answer to address this. Sorry it took some dialog for me to understand what your goal was. My updated answer uses durations rather than datetime.
The updated solution uses durations rather than datetime but it's easy to convert those durations to datetime if that's what you need to do.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

質問済み:

2019 年 6 月 17 日

編集済み:

2019 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by