MATLAB: How to convert UTC timestamp to unix time in MATLAB with the respect of leap seconds

convertleap secondsMATLABposixposix timeposixtimeunixunix timeunixtimeutcutc-time

I need to synchronize two sensors from UTC to unix time. The first one is an GPS which yields an NMEA GPGGA file, where UTC timestamp has the format "HHmmss.SSS". As an example "042803.00" is "4:28:03" AM and zero milliseconds.
The second sensor yields an unix time without leap seconds.
Is there an MATLAB function which I can use? I'm especially interested in an solution which respects leap seconds? My first approach is to convert the unix time into utc:
sensor=datetime(unix_timestamp,'ConvertFrom','posixtime')
Unfortunately the argument posixtime does not support leap seconds. Then I take the utc timestamp from the NMEA file:
gps=datetime(utc_timestamp,'InputFormat','HHmmss.SSS','TimeZone','UTC')
Now since i don't have any day, year, month in my NMEA protocoll, I take it from the unix_timestamp
gps.Day=sensor.Day
gps.Month=sensor.Month
gps.Year=sensor.Year
Any Idea how to do it better? What is the risk if i take the year,month,day from the unix timestamp without leap seconds?
Now I want to transfer the gps time into unix time with:
unix_time_gps=posixtime(gps)
This does not work since posixtime does not respect leap seconds.
How do I achieve this?
Thank you

Best Answer

A couple things, apologies if you already know all this:
1) POSIX time does not have leap seconds. As Jan says, unix systems variously either just ignore that 86401st second, or smear it over the entire day. That's why 'ConvertFrom','posixtime' doesn't support leap seconds -- POSIX time doesn't. And strictly speaking, POSIX time is not a "timestamp" in the sense of hh:mm:ss, it's a count of seconds. "Doesn't support leap seconds "means that the count does not increment during a leap second.
2) UTC, i.e. the "real-world" UTC, does observe leap seconds. The 'UTC' timezone that datetime in MATLAB supports is more like POSIX time in that it ignores the existence of leap seconds, because most people would be unhappy to find their calculations puzzlingly off by, say, 2 seconds when going from noon to noon. 'UTCLeapSeconds' is for those who need the exact thing.
3) GPS time also does not observe leap seconds, but in a different way than POSIX time doesn't. It is also a count of elapsed time, but the count DOES increment during a leap second, and therefore GPS time drifts from UTC by 1 second each time a leap second occurs in UTC. So, you should be really careful that the thing you are calling "GPS time" and "UTC timestamp" is in fact what you think it is. I don't even know what a NMEA GPGGA file is, so you're the expert here.
It looks like your unix_timestamp is a number, and your utc_timestamp is text in the format "HHmmss.SSS". In some sense it does not matter if you use
datetime(utc_timestamp,'InputFormat','HHmmss.SSS','TimeZone','UTC')
or
datetime(utc_timestamp,'InputFormat','HHmmss.SSS','TimeZone','UTCLeapSeconds')
because that timestamp is absolute, not relative (once you get the right date for it, and assuming you can synchronize the two data streams), and datetime is happy to convert either ''UTC' or 'UTCLeapSeconds' times into the correct POSIX time. If your data are never at 23:59:60.xxx on 30-Jun or 31-Dec, everything is fine, both of those lines work, and datetime automatically compensates for leap seconds when converting from 'UTCLeapSeconds' to POSIX time. If your data do happen to land during an actual leap second, the first line above will error, the second will work, but again datetime compensates -- calling posixtime will give you the value that the POSIX time for that leap second would be -- the 0th second of the next day.
So what is it that "does not work since posixtime does not respect leap seconds"?