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.

2 Comments

abs(aa1 - aa2)*3.154e+7 +...
abs(mm1 - mm2)*2.628e+6 ...
Four significant digits is nowhere near precise enough if you want to calculate something to one second accuracy.
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.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 12 Jan 2018
Edited: Star Strider on 12 Jan 2018
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 Comments

GCP
GCP on 12 Jan 2018
Edited: GCP on 12 Jan 2018
Basically, if you consider years and months, the number of days change and this will affect the final calculation. In my formula, I ignored that "just noticed this". I assume etime does consider the differences. For this reason, when I check my numbers against the etime results, they are correct for some dates but not for all.
"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.
"
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

Sign in to comment.

Categories

Asked:

GCP
on 12 Jan 2018

Commented:

on 12 Jan 2018

Community Treasure Hunt

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

Start Hunting!