wrong etime output in seconds
이전 댓글 표시
In my code, if I calculate Dt1 = etime(t1, t2); then
Dt2 = abs(aa1 - aa2)*3.154e+7 +...
abs(mm1 - mm2)*2.628e+6 +...
abs(dd1 - dd2)*86400 +...
abs(hh1 - hh2)*3600 +...
abs(mi1 - mi2)*60 +...
abs(se1 - se2);
abs(Dt1 - Dt2) is not zero.
Note: both t1 and t2 result from datevec calls, along with the variables for year, month, day, hour, minute, and second used in the Dt2 calculation.
What is the problem using etime? I am assuming my calculation for Dt2 is the correct one.
답변 (1개)
Star Strider
2018년 1월 12일
편집: Star Strider
2018년 1월 12일
It is difficult for me to understand what you are doing, even when I edited your post to correctly show the code.
This works for me:
t1 = clock;
t2 = t1 + [0 0 1 0 0 0]; % Add 1 Day
te = etime(t2,t1)
days = te/(60*60*24)
and also works correctly here:
t1 = clock;
t2 = t1 + [0 0 0 0 0 1]; % Add 1 Second
te = etime(t2,t1)
What precisely is the problem you are experiencing?
Note that hours, minutes, and seconds with respect to a datenum conversion are in fractions of a day, so you could be seeing the results of floating-point conversion error in your calculations, discussed in Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link).
EDIT —
‘I guess it is more about the fact I am not considering bissextile years nor the different number of days in varied months. The etime function results may reflect these variations.’
The datetime data type and its functions were introduced in R2014b to deal with exactly these problems. See the documentation on Date and Time Arithmetic (link) for details.
댓글 수: 2
Peter Perkins
2018년 1월 12일
As SS says, datetime was introduced in 14b to address the problems you are having.
>> d1 = datetime
d1 =
datetime
12-Jan-2018 12:40:21
>> d2 = d1 + seconds(1e9*rand)
d2 =
datetime
08-Nov-2034 00:26:14
>> dt = d2 - d1
dt =
duration
147443:45:53
>> dt.Format = 's'
dt =
duration
5.308e+08 sec
>> between(d1,d2)
ans =
calendarDuration
16y 9mo 26d 11h 45m 53.009s
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!