MATLAB Answers

How to convert the number of seconds from an epoch into UTC using the datetime function.

25 views (last 30 days)
I am struggling to convert the number of seconds from an epoch (1980-01-06 GPS epoch) into UTC using the datetime function. The following commands give me a time of 18-Mar-2021 20:37:31, however in UTC this should be 18-Mar-2021 20:37:13 since I would need to subtract out the number of leap seconds from the total (currently there are 18 leap seconds since the 1980-01-06 GPS epoch).
Can the datetime function automatically account for leapseconds in this conversion? I tried using the 'TimeZone','UTCLeapSeconds' name/value pair, but this adds 18 seconds since it assumes the time is already in UTC.

Accepted Answer

Peter Perkins
Peter Perkins on 2 Aug 2021
Matthew, this is easier than you think. In MATLAB, 'TimeZone','UTC' is sort of a ficticious timeline that pretends that leaps seconds don't exist (because hardly anyone wants to actually account for leap seconds). 'UTCLeapSeconds' is the timeline that knows all about leap seconds. The GPS timeline (in the real world) ticks off the same leap seconds as 'UTCLeapSeconds' in MATLAB does, it just doesn't treat them specially. So for the purposes of arithmetic, i.e. elapsed time, 'UTCLeapSeconds' is what you want. Then all you have to do is change to 'UTC' (or not, it's up to you).
>> epoch = datetime(1980,1,6,'TimeZone','UTCLeapSeconds')
epoch =
>> dtUTC = epoch + seconds(1300135051)
dtUTC =
>> dtUTC.TimeZone = 'UTC'
dtUTC =
18-Mar-2021 20:37:13
Also, this Answer might be helpful:
If I may ask, I would be interested in hearing more about how you work with GPS timestamps. Do you always get "seconds since 1980" as your input? Do you need to work with "week number, seconds within week"? Do you need to work with "human readable timestamps" (e.g. 18-Mar-2021 20:37:31) that refer to the GPS timeline (as opposed to UTC)? Do you need to convert back to one of those GPS time formats? Thanks in advance for anything you are able to share.
Matthew Casiano
Matthew Casiano on 3 Aug 2021
Great advice. Next revision I'll definitely streamline my scripts with this. Not sure how I missed this means of outputing the timestamp. Thank you for the help,

Sign in to comment.

More Answers (1)

Matthew Casiano
Matthew Casiano on 31 Jul 2021
Since there is a leapseconds table included in Matlab, there is a workaround to obtain the cumalative number of leap seconds. This can then be subtracted off of the total number of seconds from an epoch.
% Determine the number of leap seconds since the 06-Jan-1980 GPS epoch by using a Matlab-defined timetable array called 'leapseconds'.
TimeTableLeapSeconds=leapseconds; % Matlab-defined timetable array of leap seconds information
CurrentDateTime=char(datetime('now')); % converts the current time to a character array
DateTimeRange = timerange('06-Jan-1980 00:00:00',CurrentDateTime); % creates a timerange object with the desired time range
[tf, whichrows]=containsrange(TimeTableLeapSeconds,DateTimeRange); % determines which rows of the leap seconds timetable are within the time range
AllRowsRange=TimeTableLeapSeconds(whichrows,1); % selects the first column of all rows in the range which is a timetable containing the leap second sign
AllLeapSecondSignsRange=char(AllRowsRange.Type); % convert timetable array to character array containing all the leap second signs within the time range
AllLeapSecondsRange=str2double(AllLeapSecondSignsRange+"1"); % append '1' to the sign and create a numeric array with all the leap seconds within the time range
CumLeapSecondAdj=sum(AllLeapSecondsRange); % sum contents of the numeric array producting the cumulative leap second adjustment





Community Treasure Hunt

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

Start Hunting!