Summing Datenum resets after 24 hours

조회 수: 2 (최근 30일)
Ruger28
Ruger28 2020년 12월 15일
댓글: Steven Lord 2020년 12월 15일
Hello all,
I am trying to sum datenum values, and return them to hours:mins:secs format when all complete. When the amount of time is relatively small, it works as expected, but once it rolls past 24 hours, it resets.
Example:
a = datenum('21:10:00','hh:MM:ss')
b = datenum('1:10:00','hh:MM:ss')
c = datestr(x+y,'hh:MM:ss')
c = '22:20:00'
x = datenum('21:10:00','hh:MM:ss')
y = datenum('7:10:00','hh:MM:ss')
z = datestr(x+y,'hh:MM:ss')
z = '04:20:00'
Anyone know how to get around that to output :
z = '28:10:00'
and also 3 digit hours if need-be.
Any help is greatly appreciated!

채택된 답변

Stephen23
Stephen23 2020년 12월 15일
편집: Stephen23 2020년 12월 15일
As Steven Lord wrote, serial date numbers are not the right tool for the task: amongst other things you will struggle against defining an appropriate pivot year (which is essential for the task that you are attempting) and as you have already found DATESTR will (by design) return the hours of a day, not an arbitrarily increasing hour count.
But you can easily do it yourself without serial date numbers, here using time in seconds:
v = [60*60,60,1];
a = v * sscanf('21:10:00','%d:%d:%d'); % seconds
b = v * sscanf( '1:10:00','%d:%d:%d'); % seconds
mytime(a+b)
ans = '22:20:00'
x = v * sscanf('21:10:00','%d:%d:%d'); % seconds
y = v * sscanf( '7:10:00','%d:%d:%d'); % seconds
mytime(x+y)
ans = '28:20:00'
where:
function str = mytime(tmp)
spl = nan(1,3);
spl(3) = mod(tmp,60); % seconds
tmp = fix(tmp/60);
spl(2) = mod(tmp,60); % minutes
tmp = fix(tmp/60);
spl(1) = tmp ; % hours
str = sprintf('%02d:%02d:%02d',spl);
end
  댓글 수: 2
Ruger28
Ruger28 2020년 12월 15일
Perfect, this is exactly what I need. Thank you very much for your help!
Steven Lord
Steven Lord 2020년 12월 15일
Instead of using sscanf you could directly call duration as of release R2018a. I know this doesn't help the original poster, but I'm including it as an FYI.
d = duration('21:10:00', 'InputFormat', 'hh:mm:ss')
d = duration
21:10:00

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

추가 답변 (2개)

Matt Gaidica
Matt Gaidica 2020년 12월 15일
I don't know if the datetime functions are going to allow that triple digit hour. Here's one approach.
z = sprintf('%03i:%02i:%02i',hour(x) + hour(y), minute(x+y), second(x+y))
  댓글 수: 2
Ruger28
Ruger28 2020년 12월 15일
thanks, I'll give this a go and report back!
Ruger28
Ruger28 2020년 12월 15일
Update: the tiem commands are also not available in 2014a. Thanks again, though.

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


Steven Lord
Steven Lord 2020년 12월 15일
Serial date numbers are not the right tool for this job. You want to use a duration array.
dur1 = duration(12, 23, 45)
dur1 = duration
12:23:45
dur2 = hours(67)+minutes(89)
dur2 = duration
68.483 hr
D = dur1 + dur2
D = duration
80:52:45
If you want you can control the display Format.
D.Format = 'h'
D = duration
80.879 hr
  댓글 수: 1
Ruger28
Ruger28 2020년 12월 15일
Thank you for your reply, but I am using 2014a, and duration was introduced in 2014b, unfortunately.

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

카테고리

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

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by