# Datenum: how many seconds in a day?

60 views (last 30 days)
Lukas on 7 Mar 2014
Answered: Peter Perkins on 22 Mar 2015
Datenum converts a date to a number representing the number of days past since a certain initial date. The documentation of datenum doesn't seem to say anything about this, but what is meant exactly by a day?
Is the day exactly 24h, i.e. 86400 (SI) seconds? Or is it a solar day?
If it is the former, how does it match with real life dates? By adding leap seconds? If so, a day is not (necessarily) exactly 86400 seconds; and how do I know when there are leap seconds and how many?
If it is the latter, how do I know how many (SI) seconds there were/are/will be in that day?

James Tursa on 7 Mar 2014
With regards to the datenum and related functions, MATLAB days are exactly 24h (86400s) always. There is accounting for leap days in calendar date conversions, but NO accounting for leap seconds. There is also no accounting for time zones. So it is up to the user to use the functions properly as MATLAB gives you no help here. That is, you can use the datenum family of functions to work with local times or UTC based times or TT based times, but you need to know what you are doing and make any adjustments (time zone, leap seconds, etc) yourself to get the correct result.
To quote the doc, the "now" function "... returns the current date and time as a serial date number." Since this is obtained from your computer's clock, and since your computer likely updates its clock from a UTC based server with time zone correction, what you get with the "now" function is a local time zone corrected UTC based time. UTC based times have leap seconds applied to them, so it is not a continuous time scale.
If you need to convert to a continuous time scale like TT or GPS, you will need to get a 3rd party conversion function. Basically, a table look up to account for leap seconds and then a constant offset will do the job. For example, astronomical algorithms typically take a TT based time as an input (to get planet positions, etc), so if you are trying to see where a planet is at 10pm tonight you would need to convert that local UTC based time to a TT time before feeding it to the astronomical algorithm. Online programs typically do this conversion for you in the background, but if you are writing a program yourself you would need to include that conversion in your program.
I think I have a MATLAB based UTC -- TT conversion program if you need it, or you might be able to find one in the FEX (I haven't looked).
James Tursa on 20 Mar 2015
Because datenum stores the entire date information (day,hour,minute,second) as a single double precision number, so the seconds part of it is not accurate down to the precision you are showing. datevec apparently has smarts in it for conversions to recognize that those trailing digits are garbage and 0's them out for you.

Thomas on 7 Mar 2014
Edited: Thomas on 7 Mar 2014
IF my guess is right matlab considers every day as 86400 seconds.. only in leap years does datenum make sure an extra day is added to the calendar in february..
a=datenum([2012,2,1,12,00,00]); % leap year feb
b=datenum([2012,3,1,12,00,00]);
b-a % leap year feb 29 days
c=datenum([2013,2,1,12,00,00]);
d=datenum([2013,3,1,12,00,00]);
d-c % no leap year feb 28 days
So the extra seconds are added on that day..

Star Strider on 7 Mar 2014
That question interested me enough to do a search. I can’t determine if MATLAB corrects for leap seconds and such (my guess is that MATLAB uses the SI day). The Astronomy & Astrophysics package for Matlab is the only site I found that might have what you want. It’s encyclopedic, so I just scanned it.
MATLAB tracks time only to the millisecond in its date and time functions, and day length variations seem to be significantly less than that. You probably have to do your own corrections to get variations in and different definitions of day length.
Lukas on 12 Mar 2014
thanks

Sean de Wolski on 20 Mar 2015
It might be worth looking into the datetime class in R2014b. This allows you to account for leap seconds:
James Tursa on 20 Mar 2015

Peter Perkins on 22 Mar 2015
Just to summarize and expand on what several people have said:
1) The datenum, datevec, and datestr functions work strictly in terms of 86400s days. Beyond what your system may or may not return when you use the now function, there is no accounting for leap seconds (they are treated as if they did not occur), and no time zone or daylight saving time support, although they do account for leap days. What those "seconds" mean is entirely up to your interpretation, those functions make no assumptions about solar days vs. SI days, or even about things like TT vs. TAI vs. GPS time. The interpretation becomes important when you want to convert between systems. And of course UTC does include leap seconds, so it's not continuous, although I suspect that most people wish that wasn't the case. At any rate, datenum, datevec, and datestr treat every day as 86400s long.
2) Since R2014b, MATLAB includes three new data types: datetime, duration, and calendarDuration. These allow you to opt in on support for time zones and daylight saving, and even for leap seconds. They also provide much higher precision, so Phillippe's example becomes
>> d = datetime('3-Sep-2014 17:00:00') - datetime('3-Sep-2014 16:00:00')
d =
01:00:00
>> minutes(d)
ans =
60
There is not yet support for GPS time or TAI in datetime. But again, in a continuous time system that ignores leap seconds (which is what you get by default with datetime), you can interpret the times however you want. It's only when converting to other systems that you have to be careful.