如何将由时间,星期和leap秒组成的GPS时间转换为UTC时间戳

我从车辆中集成的GPS接收到此数据:

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

我需要的是UTC时间戳,所以我假设必须将GPS中所有不同的时间组合起来才能获得UTC时间戳,但是我不知道该怎么做。如果有人可以指导我完成这项工作,我将不胜感激。

example of the result I want to have: 1572430625230

我正在使用python 3.7,如果有一个用于此的库,那将非常有帮助,否则我也在搜索算法来做到这一点

fybbbg 回答:如何将由时间,星期和leap秒组成的GPS时间转换为UTC时间戳

我的猜测:

根据https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing,GPS时代是1980年1月6日,GPS counts weeks (a week is defined to start on Sunday) and 6 January is the first Sunday of 1980

根据http://leapsecond.com/java/gpsclock.htmGPS 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.

因此,我们必须定义一个gps_epoch并减去给定的leap秒才能获得utc日期时间

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)

以您的示例

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

但是,我离您的预期结果还很遥远(1572430625230即使以毫秒表示)

别忘了pip install pytz

本文链接:https://www.f2er.com/3166168.html

大家都在问