File Exchange

Calculate Time Interval/ Difference

version 1.3 (8.44 KB) by

To compute time interval, taking into account leap days that are subsumed within that interval.

3.66667
3 Ratings

Updated

main executing reference usage: usage_timeDifference.m

Sample output:
The time interval between earierTime [09-Sep-1900 09:09:18] and laterTime [31-Oct-2010 23:33:08] is
110 years, 79 days, 14 hours, 23 mins, 50.17 secs.

The objective is to compute the time interval taking into account the leap days that are subsumed within the time interval.

Note:
The logic subsumes the time interval by
yearEnd, monthEnd, dayEnd - timeEarlier (within that year)
+ time interval (excluding that 2 years)
+ timeLater - yearEnd, monthEnd, dayEnd (within that year)

It differentiates the earlier time and later time, ie. timeDifference (earlierTime, laterTime) gives the same result as timeDifference (laterTime, earlierTime).

leapDayHasPassed.m illustrates a possible approach to determine if a certain day of concern is already over at a given time.

Caveat: does not include leap secs

If the reference demo has a more elegant presentation, please do not hesitate to suggest and send feedback to author.
Email: promethevx@yahoo.com.

Thank you.

Regards,
Michael Chan JT

kevin

Sean

Sean (view profile)

Useful reference. I dropped you a mail. Can you help me to translate this to C codes?
Thank you.

Michael Chan

Michael Chan (view profile)

Hi Jan, I will archive it accordingly. I promptly placed your rendition as I believe you are right that this module is much needed in many features and applications. Indeed, I should have stated in the read me, or store in old archives for formatting sake. My apologies.

I am really happy to think that it's going to make life easier for many people. Thank you, Jan. You make a great difference. ;)

Jan Simon

Jan Simon (view profile)

Dear Michael, your function timeDifference still replies 40228 days for the date given in the example. As I've shown above, the correct number of days would be 40229.
Thanks for including the function DateDiff in your submission, which replies the correct number of days. Perhaps the downloaders are confused by having two functions replying different results... I'm satisfied with DateDiff, but I cannot rate a function, which I have written.

Jan Simon

Jan Simon (view profile)

The actual calculation can be done by:
D = abs(datenum(T1) - datenum(T2));
days = fix(D); secs = round(mod(D, 1) * 86400);
This accepts even arrays of input dates, e.g. cell strings.
Together with a check of inputs this could be efficient and useful for FEX downloaders. Matlab users and the HELP function expect the description of inputs, calculations and outputs always in the same location. Scrolling through the source code happens most likely once only, while HELP is called frequently during programming to remember the interface.
It would be kind if you could publish a polished version, because the number of elapsed days is needed for a lot of problems. Thanks!

Michael Chan

Michael Chan (view profile)

Pardon. To deter misunderstanding of what was meant by maintenance. The modelling is designed for translation to other builds, as Matlab is only used for simulation and tracing purposes.

I apologize if this utility is not helpful at all. However, please send refinement and suggestions to the given email so that we may make this utility better, so that we can help people which is the key motivation for this posting.

Thank you.

Michael Chan

Michael Chan (view profile)

Thank you, Jan.
The 'clear all' is to deter collision of variables formally declared. It may messed up your previous keeps. As stated, it is merely a usage for example. My opinion is that there is no point optimizing Matlab, as it is merely for simulation, premature optimization can cause maintenance issue later on. Due to schedule constraints, comments and meaning variable names are designated as refined as possible. Like you, I would like to have the help sections as well. I also like to note that it is more useful putting the help comments at the end, not top. As experienced that, there is alot of unnecessary scrolling when the file is opened.

Your inputs are valuable. The motivation is to help as many people as possible. It is true that the utilities is hardly perfect, they are only for modelling, that is why caveats are always placed. Regression testing is a different domain unaddressed. Could you send me the debugged version? I will update it and ascribed to your help.

Thank you for your help, Jan.

Jan Simon

Jan Simon (view profile)

t1 = datenum('09-Sep-1900 09:09:18');
t2 = datenum('31-Oct-2010 23:33:08');
d = t2 - t1, >> d = 40229.59988426928
% Integer part=number of days
% Fractional part * 86400=number of seconds
40229 days are 110 years (a 365 days) and 80 days.
But you get: 110 years, 79 days, 14 hours, 23 mins, 50.17 secs? Where do the fractional parts of the seconds come from?!Why do you count one day less? Recalculating the number of days manually leads to:
09-Sep-1900 -> 01-Jan-1901: 114 days
01-Jan-1901 -> 01-Jan-2010: 109*365 + 27 days
01-Jan-2010 -> 31-Oct-2010: 303 days
==> 40229 days, which are 110 years and *80* days.
But this is just the documentation, lets look in the real calculations: [days, secs] = timeDifference(t1, t2)
>> days = 40228 % (one day to less)
>> secs = 138230 % ??? Here is the lost day! A day has 86400 sec only!

My rating: No help in the function, but a readme file, a lean H1 line, no check of inputs, output almost correct, but with a strange inconsistency causes e.g. a wrong example. The computations could be performed in 4 lines and much faster (100 times with using DATENUMMX), so I do not see a reason to spread this over 9(!) M-files. The check for the number of leap years is performed in a loop over all years and without pre-allocation.
The usage_TimeDifference script calls "clear all" - why do you assume that I don't need my variables anymore?! Not useful for calculations or education and a very limited usability. I see that you've spent a lot of work and time to create this. I'd appreciate this, if at least the results are correct.