【问题标题】:Python Convert GPS Time 19 digit to UTCPython将GPS时​​间19位转换为UTC
【发布时间】:2020-07-20 19:25:32
【问题描述】:

我正在读取设备的 GPS 时间,该设备输出的数字通常很长 1279305882528145920。通常是 10 位数,但我不确定为什么会更长。我假设它有额外的 9 位数的纳秒精度。

我可以通过此链接https://www.andrews.edu/~tzs/timeconv/timedisplay.php 验证 19 位 gps 时间是否正确,但我不确定 gps->unix 转换是如何发生的。我还读到可能会有闰秒,但我不确定设备是否会这样做。我使用的设备是VectorNav VN-200

以下是我的尝试:

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 我的代码有效,但我想记录毫秒。我将如何做到这一点?

【问题讨论】:

  • 转换时是否必须非常准确?看看 python 的 datetime 模块。也许 datetime.datetime.fromtimestamp?它返回一个精确到毫秒的日期时间对象。
  • 感谢您的建议。我会发布答案。它涉及使用 1980 年 6 月 6 日的 GPS 时间并在其上加上秒数。还有一个时间漂移发生,我们从该值中减去。到 2020 年,漂移为 18,到 2023 年可能会增加到 19。我不确定如何计算这个,所以我现在已经硬编码了。希望有人能插话。

标签: python-3.x gps unix-timestamp date-conversion


【解决方案1】:

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}')

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2020-12-07
  • 2017-04-27
  • 2020-09-29
  • 1970-01-01
  • 2014-09-02
相关资源
最近更新 更多