Python 将 UTC 时间转换为 GPS 时间
UTC time to GPS time converter by Python
我有一个 UTC 时间(epoch Unix 时间),格式如下所示。
1496224620
(人类可读值:2017 年 5 月 31 日 09:57:00)
我需要将格式为 Unix 时间的时间戳转换为 GPS 时间格式,如下所示。
1180259838
(人类可读值:2017 年 5 月 31 日 09:57:00)
我需要一个 python 程序(算法对我来说很好)来将格式为 Unix 时间的时间戳转换为格式为 GPS 时间的时间戳。
有一个 PHP 程序可以做到这一点。我可以从 PHP 代码更改为 Python 代码以自己拥有一个 Python 程序。但是我觉得还有一个short way(Python的内置函数)可以更有效的实现我的期望。
这是 PHP 程序
的 link
https://www.andrews.edu/~tzs/timeconv/timealgorithm.html
怎么样:
import datetime
datetime.datetime.fromtimestamp(int("1284101485")).strftime('%B-%Y-%d %H:%M:%S')
只需从 UNIX 时间中减去 315964782。
GPS 时间从 1980 年 1 月 5 日开始。UNIX 时间从 1970 年 1 月 1 日开始。它们只是起点不同,或者 Epochs。这两个纪元之间的差异是这两个日期之间的秒数 PLUS 18 GPS 闰秒(到目前为止)。
正如其他人指出的那样,随着地球自转逐渐减慢,GPS leap seconds 会定期添加。
we must update our source code manually for each time the leap second occurs (ex. next 2018, 2019,...)? Is there any feasible way to prevent this problem?
许多设备都有一条消息,表明 "current" GPS 秒数有效。我的 C++ NeoGPS library has an example program that requests the current number of GPS seconds from a ublox device (binary message defined here)。有关 NAV-TIMEGPS 消息的更多信息,请参阅 ublox NEO-xx 规范。
其他制造商可能有自己的协议和消息来获取当前 GPS 闰秒。
但是:
大多数 GPS 设备以 UTC 报告时间,其中已经包含闰秒。除非您使用的是基于一周开始时间(星期日午夜)的 GPS 时间,否则您不需要知道 GPS 闰秒。
如果您尝试从 "GPS time since start of week" 转换,那么您还需要知道当前的 GPS week number 才能将 "GPS time of week" 转换为 UTC。
ublox 设备报告了一些带有时间戳 "GPS milliseconds since start of week." 的修复信息 This NeoGPS file 显示了在 "GPS milliseconds since start of week" 和 UTC 之间转换的几种方法。
我有一个 UTC 时间(epoch Unix 时间),格式如下所示。
1496224620
(人类可读值:2017 年 5 月 31 日 09:57:00)
我需要将格式为 Unix 时间的时间戳转换为 GPS 时间格式,如下所示。
1180259838
(人类可读值:2017 年 5 月 31 日 09:57:00)
我需要一个 python 程序(算法对我来说很好)来将格式为 Unix 时间的时间戳转换为格式为 GPS 时间的时间戳。
有一个 PHP 程序可以做到这一点。我可以从 PHP 代码更改为 Python 代码以自己拥有一个 Python 程序。但是我觉得还有一个short way(Python的内置函数)可以更有效的实现我的期望。
这是 PHP 程序
的 link
https://www.andrews.edu/~tzs/timeconv/timealgorithm.html
怎么样:
import datetime
datetime.datetime.fromtimestamp(int("1284101485")).strftime('%B-%Y-%d %H:%M:%S')
只需从 UNIX 时间中减去 315964782。
GPS 时间从 1980 年 1 月 5 日开始。UNIX 时间从 1970 年 1 月 1 日开始。它们只是起点不同,或者 Epochs。这两个纪元之间的差异是这两个日期之间的秒数 PLUS 18 GPS 闰秒(到目前为止)。
正如其他人指出的那样,随着地球自转逐渐减慢,GPS leap seconds 会定期添加。
we must update our source code manually for each time the leap second occurs (ex. next 2018, 2019,...)? Is there any feasible way to prevent this problem?
许多设备都有一条消息,表明 "current" GPS 秒数有效。我的 C++ NeoGPS library has an example program that requests the current number of GPS seconds from a ublox device (binary message defined here)。有关 NAV-TIMEGPS 消息的更多信息,请参阅 ublox NEO-xx 规范。
其他制造商可能有自己的协议和消息来获取当前 GPS 闰秒。
但是:
大多数 GPS 设备以 UTC 报告时间,其中已经包含闰秒。除非您使用的是基于一周开始时间(星期日午夜)的 GPS 时间,否则您不需要知道 GPS 闰秒。
如果您尝试从 "GPS time since start of week" 转换,那么您还需要知道当前的 GPS week number 才能将 "GPS time of week" 转换为 UTC。
ublox 设备报告了一些带有时间戳 "GPS milliseconds since start of week." 的修复信息 This NeoGPS file 显示了在 "GPS milliseconds since start of week" 和 UTC 之间转换的几种方法。