Does MATLAB datetime arithmetic ignore inserted leap seconds?
0 reputation · 20 Apr 2024, 07:48 UTC
0 reputation · 20 Apr 2024, 07:48 UTC
I need to know whether MATLAB datetime subtraction across a historical leap second returns the calendar-day difference or the true elapsed SI time, and whether any release-specific flag or function changes this behavior.
MATLAB datetime objects use the proleptic Gregorian calendar and define one day as exactly 86,400 seconds. Arithmetic between datetime values or added duration values ignores inserted leap seconds and treats every minute as 60 seconds. This behavior is described as intended design and has been consistent since the datetime datatype was introduced.
It is unclear whether recent MATLAB releases provide any built-in leap-second table, conversion mode, or release note exception for timestamps spanning a leap second. Applications requiring legal or scientific leap-second accuracy may need external correction after MATLAB arithmetic.
Does MATLAB datetime arithmetic ignore inserted leap seconds across all supported releases, and is there a supported way to enable leap-second-aware subtraction?
34775 reputation · 20 Apr 2024, 11:43 UTC
MATLAB’s datetime objects always treat a day as exactly 86 400 SI seconds. Subtracting two datetime values that span a historical leap second returns the calendar‑day difference, not the true elapsed time that includes the extra second. No MATLAB release, nor any supported flag or function, changes this behavior.
datetime class was introduced in R2014b and, since then, the documentation has explicitly stated that leap seconds are not represented in the internal time scale.datetime, duration, and calendarDuration uses a uniform 86 400‑second day and ignores any inserted leap seconds.TimeZone property can be set to "UTC", but this merely selects the uniform UTC time scale; it does not enable leap‑second handling.% MATLAB example (R2023b or later)
> t1 = datetime(2016,12,31,23,59,59,'TimeZone','UTC');
> t2 = datetime(2017,1,1,0,0,0,'TimeZone','UTC');
> diff = t2 - t1
diff =
1 days 00:00:01
Although the leap second 2016‑12‑31 23:59:60 existed, the difference is reported as exactly 1 day (86 400 s). The internal representation does not distinguish the extra second.
If your workflow requires legal or scientific accuracy that must account for leap seconds, you must apply a correction after MATLAB arithmetic. Typical approaches include:
datetime, adjust the result by adding or subtracting the number of leap seconds that occurred in the interval.datetime for further processing.Since the behavior is uniform across all supported releases, there is no release‑specific setting to enable. If you are working with a particular MATLAB version, confirm that it is 2014b or newer (the datetime class did not exist before). Otherwise, you can safely assume the leap‑second‑ignorant behavior applies.
| Release | Leap‑second awareness | Supported flag / function |
|---|---|---|
| R2014b – R2026 | Ignored (uniform 86 400‑s day) | None |
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.