Search code examples
python-3.xgpsunix-timestampdate-conversion

Python Convert GPS Time 19 digit to UTC


I am reading GPS time off a device which output in a typically long number 1279305882528145920. Usually it would be 10 digits but I am unsure why this is much longer. I assume it has additional 9 digits for nanosecond accuracy.

I am able to verify the 19 digit gps time is correct with this link https://www.andrews.edu/~tzs/timeconv/timedisplay.php but I am unsure how the gps->unix conversion occurs. I have also read that there could be leap seconds but I am unsure if the device does this. The device I am using is a VectorNav VN-200.

below is my attempt:

def get_times(gps_time):
    epoch = gps_time/1000000000
    date = time.strftime("%m/%d/%Y", time.localtime(epoch))
    time = time.strftime("%H/%M/%S.%f", time.localtime(epoch))
    
    return epoch, date, time

then called with get_times(1279305882528145920) which will result in the following error:

ValueError: ('Unknown string format:', '12:05:24.%f')

Edit: If I change %H/%M/%S.%f to %H/%M/%S my code works but I want to log the milliseconds. How would I accomplish this?


Solution

  • The 19 digit time is GPS time in nanoseconds. GPS Time uses June 6, 1980. This is similar to how Epoch uses January 1, 1970.

    Anyways the calculation is done by using 1980 as a starting reference point then adding the number of seconds since then. There is a time drift error which needs to be accounted for. In 2020 that drift is 18.

    time_gps = time_gps/1000000000  # Converts ns -> s
    gps_start = pd.datetime(year=1980, month=1, day=6)
    time_since= pd.to_timedelta(time_gps, unit='s')
    error = pd.to_timedelta(19, unit='s')
    time_utc = gps_start + time_since - error
    print(f'time_utc: {time_gps}')