How to get current date and time from GPS unsegment time in python(如何从python中的GPS取消分段时间获取当前日期和时间)
问题描述
我有这样的 GPS 未分段时间:
I have gps unsegmented time like this:
Tgps = 1092121243.0
我想知道那是什么日期和时间.GPS时间的开始日期是1980年1月6日.Python函数
And I'd like to understand what date and time is that. The begining of GPS time is 6 January 1980. Python function
datetime.utcfromtimestamp
可以给出从 1970 年 1 月 1 日开始的秒数.
could give seconds from 1 January 1970 year.
我发现了以下内容:
from datetime import datetime
GPSfromUTC = (datetime(1980,1,6) - datetime(1970,1,1)).total_seconds()
curDate = datetime.utcfromtimestamp(Tgps + GPSfromUTC)
Out[83]: datetime.datetime(2014, 8, 15, 7, 0, 43)
我不确定闰秒是否包含在函数 datetime 中,或者我应该计算它们并从结果中减去?是否也存在更好的解决这个问题的方法?
I'm not sure about leapseconds are they included in function datetime or I should calculate them and substract from the result? May be also exists better solution of this problem?
推荐答案
GPS 时间开始与 UTC 同步:1980-01-06 (UTC) == 1980-01-06 (GPS).两者都以 SI 秒为单位.GPS 时间和 UTC 时间之间的差异会随着每个(闰)闰秒而增加.
GPS time started in sync with UTC: 1980-01-06 (UTC) == 1980-01-06 (GPS). Both tick in SI seconds. The difference between GPS time and UTC time increases with each (intercalary) leap second.
要找到正确的 UTC 时间,您需要知道在给定 GPS 时间之前发生的闰秒数:
To find the correct UTC time, you need to know the number of leap seconds occurred before the given GPS time:
#!/usr/bin/env python
from datetime import datetime, timedelta
# utc = 1980-01-06UTC + (gps - (leap_count(2014) - leap_count(1980)))
utc = datetime(1980, 1, 6) + timedelta(seconds=1092121243.0 - (35 - 19))
print(utc)
输出
2014-08-15 07:00:27 # (UTC)
其中 leap_count(date) 是在给定日期之前引入的闰秒数.来自 TAI-UTC 表(注意: 该网站是闰秒的权威来源.它发布 Bulletin C 宣布新闰秒):
where leap_count(date) is the number of leap seconds introduced before the given date. From TAI-UTC table (note: the site is the authoritative source on leap seconds. It publishes Bulletin C announcing new leap seconds):
1980..: 19s
2012..: 35s
因此:
(leap_count(2014) - leap_count(1980)) == (35 - 19)
<小时>
如果您使用的是 Unix,那么您可以使用 "right" 时区从 TAI 时间获取 UTC 时间(并且很容易从 GPS 时间得到 TAI 时间:TAI = GPS + 19 秒(恒定偏移)):
If you are on Unix then you could use "right" time zone to get UTC time from TAI time
(and it is easy to get TAI time from GPS time: TAI = GPS + 19 seconds (constant offset)):
#!/usr/bin/env python
import os
import time
os.environ['TZ'] = 'right/UTC' # TAI scale with 1970-01-01 00:00:10 (TAI) epoch
time.tzset() # Unix
from datetime import datetime, timedelta
gps_timestamp = 1092121243.0 # input
gps_epoch_as_gps = datetime(1980, 1, 6)
# by definition
gps_time_as_gps = gps_epoch_as_gps + timedelta(seconds=gps_timestamp)
gps_time_as_tai = gps_time_as_gps + timedelta(seconds=19) # constant offset
tai_epoch_as_tai = datetime(1970, 1, 1, 0, 0, 10)
# by definition
tai_timestamp = (gps_time_as_tai - tai_epoch_as_tai).total_seconds()
print(datetime.utcfromtimestamp(tai_timestamp)) # "right" timezone is in effect!
输出
2014-08-15 07:00:27 # (UTC)
<小时>
如果您从相应的 tzfile(5).它是前两种方法的组合,其中第一种方法的跳跃计数计算是自动的,并且自动更新 tzdata(tz 数据库) 来自第二种方法:
You could avoid changing the timezone if you extract the leap seconds list from the corresponding tzfile(5). It is a combination of the first two methods where the leap count computation from the first method is automated and the autoupdating tzdata (system package for the tz database) from the second method is used:
>>> from datetime import datetime, timedelta
>>> import leapseconds
>>> leapseconds.gps_to_utc(datetime(1980,1,6) + timedelta(seconds=1092121243.0))
datetime.datetime(2014, 8, 15, 7, 0, 27)
leapseconds.py可以从/usr/share/zoneinfo/right/UTC 文件(tzdata 包的一部分).
where leapseconds.py can extract leap seconds from /usr/share/zoneinfo/right/UTC file (part of tzdata package).
所有三种方法产生相同的结果.
All three methods produce the same result.
这篇关于如何从python中的GPS取消分段时间获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从python中的GPS取消分段时间获取当前日期和时间
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
