【问题标题】:how to convert GPS time composed of Time, weeks and leap seconds to UTC Timestamp如何将由时间、周和闰秒组成的 GPS 时间转换为 UTC 时间戳
【发布时间】:2019-11-04 14:00:55
【问题描述】:

我从车辆中集成的 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,如果有一个库,这将非常有帮助,否则我也在寻找一个算法来做到这一点

【问题讨论】:

  • @YuvrajJaiswal 谢谢,但在问这个之前我做了谷歌搜索。让我感到困惑的是我收到的那些周和闰秒功能。您发送给我的链接仅用于将那些毫秒转换为 UTC,但在我的情况下,我有几周和闰秒,我还必须以某种方式将它与毫秒结合,然后我可以转换为 UTC

标签: python datetime timestamp gps gps-time


【解决方案1】:

我的猜测:

根据https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing,GPS epoch 是 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 并减去给定的闰秒以获得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, 10, 6, 6, 830000, tzinfo=<UTC>)
>>>gps_datetime(2077, 295584830.0,18.0).timestamp()
1572429966.83  

但我与你的预期结果相差甚远(1572430625230即使以毫秒表示)

别忘了pip install pytz

【讨论】:

  • @jbernades 感谢您的回答,但您能解释一下代码吗?我的意思是我不明白您为什么使用 1,6,1980 以及为什么要减去闰秒而不是添加它
  • @basilisk 我已经用一些解释编辑了我的答案。
  • @jbernades 再次感谢,我会尝试看看它是否对我有用。如果我的时区不是 GMT,你认为我还需要添加偏移量吗?
  • @basilisk 实际上我忘记将 gps_epoch 表示为 UTC 时间。我刚刚更新了上面的代码。如果您只需要时间戳,那么最后编辑的代码应该可以工作。
猜你喜欢
  • 2019-03-23
  • 2017-04-27
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多