datetime.isregular
datetime: tf = isregular (T)
datetime: tf = isregular (T, unit)
datetime: [tf, dt] = isregular (…)
Determine whether a datetime vector is regularly spaced.
tf = isregular (T) returns true if the elements
of the datetime vector T are equally spaced in time, and
false otherwise. A scalar or empty T, or one containing a
Not-A-Time (NaT) value, is not regular. Neither is a T
that does not move: a step of zero describes no spacing, so a vector of
repeated instants is not regularly spaced.
tf = isregular (T, unit) tests for regular
spacing with respect to unit, which may be 'time' (the
default), 'years', 'quarters', 'months',
'weeks', or 'days'. With a calendar unit, T is
regular when successive elements differ by the same whole number of that
unit, which – unlike 'time' – accounts for varying month lengths
and daylight saving time.
[tf, dt] = isregular (…) also returns the common
time step dt. For 'time' it is a duration; for a
calendar unit it is a calendarDuration. When T is not
regular, dt is NaN.
Steps are compared as stored, to the microsecond this class keeps. A spacing that differs by less than that cannot be seen and reads as regular, while one that rounds differently at the microsecond – thirds of a second, say – reads as irregular. MATLAB stores instants more finely and so draws that line elsewhere.
Source Code: datetime
isregular tests whether a datetime vector is equally spaced. A daily series is regular; drop a day and it is not.
isregular (datetime (2024, 1, 1) : caldays (1) : datetime (2024, 1, 5))
ans = 1
isregular (datetime (2024, 1, [1, 2, 5]))
ans = 0
A monthly series is not equally spaced in absolute time — months differ in length — but it IS regular with respect to the 'months' unit, and the second output returns the common step.
m = datetime (2024, 1, 1) : calmonths (1) : datetime (2024, 5, 1)
m =
1x5 datetime array
01-Jan-2024 01-Feb-2024 01-Mar-2024 01-Apr-2024 01-May-2024
isregular (m)
ans = 0
[tf, step] = isregular (m, 'months')
tf = 1 step = calendarDuration 1mo