how to convert GPS time composed of Time, weeks and leap seconds to UTC Timestamp

9.3k Views Asked by At

I receive this data from the GPS Integrated in my Vehicle:

INS_Time::INS_Time_Millisec[ms]   # example of the value: 295584830.0
INS_Time::INS_Time_Week[Week]  # example of the value: 2077.0
INS_Time::Leap_seconds[s]  # example of the value: 18.0

what I need is a UTC timestamp so I assume that I must combine all those different times from GPS to get a UTC timestamp but I don't know how this should be done. I would be thankful if someone can guide me through this.

example of the result I want to have: 1572430625230

I'm using python 3.7, if there is a library for this it would be very helpful otherwise I'm searching also for an Algorithm to do this

1

There are 1 best solutions below

5
On BEST ANSWER

My guess :

According to https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing , GPS epoch is 6 January 1980, GPS counts weeks (a week is defined to start on Sunday) and 6 January is the first Sunday of 1980

And according to http://leapsecond.com/java/gpsclock.htm , GPS time was zero at 0h 6-Jan-1980 and since it is not perturbed by leap seconds GPS is now ahead of UTC by 18 seconds.

So we have to define a gps_epoch and substract the given leap seconds in order to have utc datetime

from datetime import datetime, timedelta
import pytz

def gps_datetime(time_week, time_ms, leap_seconds):
    gps_epoch = datetime(1980, 1, 6, tzinfo=pytz.utc)
    # gps_time - utc_time = leap_seconds
    return gps_epoch + timedelta(weeks=time_week, milliseconds=time_ms, seconds=-leap_seconds)

With your example

>>>gps_datetime(2077, 295584830.0,18.0)
datetime.datetime(2019, 10, 30, 10, 6, 6, 830000, tzinfo=<UTC>)
>>>gps_datetime(2077, 295584830.0,18.0).timestamp()
1572429966.83  

But I am quite far from your expected result (1572430625230 even if it is expressed in ms)

Do not forget to pip install pytz