필터 지우기
필터 지우기

find elapse time with date rollover

조회 수: 5 (최근 30일)
tybo1mos
tybo1mos 2021년 7월 22일
댓글: Chunru 2021년 7월 27일
I have a array with a bunch of times formatted as HH:MM:SS as follows:
11:58:00
11:59:00
00:00:02
00:02:04
I'm trying to find the elapse time in seconds from begining time. The time data I have starts a new day (11pm to 12am). All my attemps to use etime or subtract datenum result in returning a negative time when trying to subtract 00:00:02 from 11:58:00. I havn't figured out how to make matlabe know the 00:00:02 is actually a new date, since there is no actual date defined in my data just time.
I would like to get the following result, in seconds from start:
0
60
62
182

답변 (2개)

Peter Perkins
Peter Perkins 2021년 7월 27일
Get away from using datenum. Can't say this strongly enough.
>> tstr=["11:58:00"; "11:59:00"; "00:00:02"; "00:02:04"];
>> tod = duration(tstr)
tod =
4×1 duration array
11:58:00
11:59:00
00:00:02
00:02:04
>> newDay = [false;diff(tod)<0]
newDay =
4×1 logical array
0
0
1
0
Don't know if you klnow what day the time start on. If you cross a Daylight Saving Time shift, it will matter. Use datetime, not datenum.
>> day0 = datetime(2021,7,26);
>> t = day0 + caldays(cumsum(newDay)) + tod
t =
4×1 datetime array
26-Jul-2021 11:58:00
26-Jul-2021 11:59:00
27-Jul-2021 00:00:02
27-Jul-2021 00:02:04
>> elapsed = seconds(t - t(1))
elapsed =
0
60
43322
43444
My answer is different than yours. Guess what? Your time stamps have no way of indicating am or pm. That would seem to be a problem. If you can, use 24-hour time stamps. If not, then how do you know if it's 11 am or pm? about the only thing you can do is assume you will have timestamps in both the first and second half of every day, and my newDay variable will indicate both noon and midnight crossings.
  댓글 수: 1
Chunru
Chunru 2021년 7월 27일
It seems that here is no foolproof answer to the question where date time info is ambiguous. Just make the best guess.

댓글을 달려면 로그인하십시오.


Chunru
Chunru 2021년 7월 22일
tstr=[
"11:58:00"
"11:59:00"
"00:00:02"
"00:02:04"];
t = datenum(tstr, 'HH:MM:SS')
t = 4×1
1.0e+05 * 7.3816 7.3816 7.3816 7.3816
d = diff(t)*24*3600;
% check for negative difference
d(d<0) = d(d<0)+12*3600;
d = [0; d]
d = 4×1
0 60.0000 62.0000 122.0000

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by