Search code examples
c++ntpgpsd

calculation of unix time from utc time on ubuntu


I am using ubuntu 18.04 on my machine. My ntp is configured to use gpsd as a source. Time provided by gpsd does not consider leap seconds but NTP adjusts it and provides UTC with leap seconds adjusted. So my system clock will be synced to UTC by NTP. From the documentation, std::chrono::system_clock::now provides time since 1970 and does not count leap seconds. My question is does the kernel adjusts leap seconds when we call this? Or the time queried from std::chrono::system_clock::now is actually contains same time coming from NTP which has leap seconds adjusted.


Solution

  • system_clock and NTP both "handle" leap seconds the same way. Time simply stops while a leap second is being inserted. Here I'm speaking of the time standard, and not of any particular implementation.

    An implementation of NTP might not stop for a whole second during a leap second insertion. Instead it might delay itself by small fractions of a second for hours both before and after a leap second insertion such that the sum of all delays is one second. This is known as a "leap second smear".

    So you could say that both system_clock and NTP ignore leap seconds in that if you have two time points t0 and t1 in these systems and if t0 references a time prior to a leap second insertion and t1 references a time after that leap second insertion, then the expression t1-t0 gives you a result that does not count the inserted leap second. The result is 1 less than the number of physical seconds that has actually transpired.

    A GPS satellite "ignores" leap seconds in a completely different way than system_clock and NTP. The GPS "clock" keeps ticking right through a leap second, almost completely ignoring it. However GPS weeks are always exactly 604,800 seconds (86,400 * 7), even if a leap second was inserted into UTC that week.

    So to convert GPS weeks (and GPS time of week) to UTC, one has to know the total number of leap seconds that have been inserted since the GPS epoch (First Sunday of January 1980). I believe gpsd does this transformation for you when it provides you a UTC time point.