Python 将 GPS 时间 19 位转换为 UTC

Python Convert GPS Time 19 digit to UTC

我正在从一个输出通常很长的数字的设备上读取 GPS 时间 1279305882528145920。通常是 10 位数字,但我不确定为什么要长得多。我假设它有额外的 9 位数字以实现纳秒精度。

我可以用这个 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.

验证 19 位 gps 时间是否正确

以下是我的尝试:

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

然后用 get_times(1279305882528145920) 调用,这将导致以下错误:

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

编辑: 如果我将 %H/%M/%S.%f 更改为 %H/%M/%S 我的代码可以工作,但我想记录毫秒数。我将如何做到这一点?

19 位时间是以纳秒为单位的 GPS 时间。 GPS 时间使用 1980 年 6 月 6 日。这与 Epoch 使用 1970 年 1 月 1 日的方式类似。

无论如何,计算是通过使用 1980 年作为起始参考点然后加上从那时起的秒数来完成的。需要考虑时间漂移误差。 2020 年漂移为 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}')