如何从 python 中的当前日期的 gps 纪元获取闰秒
How to get leap seconds from gps epoch for current date in python
据我所知,目前在 python 中没有从当前日期的 gps 纪元获取闰秒的功能。 investigation/development下的相应补丁:leap seconds path in datetime
您能否建议获得闰秒的最佳方法?我找到了解决方案:
def getLeapSec(Tgps):
daysFromGPSEpoch = timedelta(seconds=Tgps).days
tableLeapSec = ([
[0, 542, 907, 1272, 2003, 2917, 3648, 4013, 4560, 4925, 5290, 5839, 6386, 6935, 9492, 10588, 11865, 12960],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
])
for i in range(1, len(tableLeapSec[0])):
if daysFromGPSEpoch < tableLeapSec[0][i]:
return tableLeapSec[1][i-1]
示例:
In [25]: Tgps = 1092121243.0
In [26]: getLeapSec(Tgps)
Out[26]: 16
您可以在您的系统上使用 leapseconds
module if you have access to the up-to-date tzfile:
>>> from datetime import datetime, timedelta
>>> import leapseconds
>>> Tgps = 1092121243.0
>>> gps_time = datetime(1980, 1, 6) + timedelta(seconds=Tgps)
>>> leapseconds.dTAI_UTC_from_tai(leapseconds.gps_to_tai(gps_time))
datetime.timedelta(0, 35)
主要优点是 tzdata
包每年由系统自动更新几次(比引入闰秒更频繁)。您的代码将使用新数据而无需更改它。
否则,作为回退,您可以对 leapseconds.leapseconds()
调用的结果进行硬编码:
>>> import leapseconds
>>> leapseconds.leapseconds()
[LeapSecond(utc=datetime.datetime(1972, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 10)),
LeapSecond(utc=datetime.datetime(1972, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 11)),
LeapSecond(utc=datetime.datetime(1973, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 12)),
LeapSecond(utc=datetime.datetime(1974, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 13)),
LeapSecond(utc=datetime.datetime(1975, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 14)),
LeapSecond(utc=datetime.datetime(1976, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 15)),
LeapSecond(utc=datetime.datetime(1977, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 16)),
LeapSecond(utc=datetime.datetime(1978, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 17)),
LeapSecond(utc=datetime.datetime(1979, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 18)),
LeapSecond(utc=datetime.datetime(1980, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 19)),
LeapSecond(utc=datetime.datetime(1981, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 20)),
LeapSecond(utc=datetime.datetime(1982, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 21)),
LeapSecond(utc=datetime.datetime(1983, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 22)),
LeapSecond(utc=datetime.datetime(1985, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 23)),
LeapSecond(utc=datetime.datetime(1988, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 24)),
LeapSecond(utc=datetime.datetime(1990, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 25)),
LeapSecond(utc=datetime.datetime(1991, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 26)),
LeapSecond(utc=datetime.datetime(1992, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 27)),
LeapSecond(utc=datetime.datetime(1993, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 28)),
LeapSecond(utc=datetime.datetime(1994, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 29)),
LeapSecond(utc=datetime.datetime(1996, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 30)),
LeapSecond(utc=datetime.datetime(1997, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 31)),
LeapSecond(utc=datetime.datetime(1999, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 32)),
LeapSecond(utc=datetime.datetime(2006, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 33)),
LeapSecond(utc=datetime.datetime(2009, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 34)),
LeapSecond(utc=datetime.datetime(2012, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 35)),
LeapSecond(utc=datetime.datetime(2015, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 36))]
在这种情况下,当引入下一个闰秒时,您必须手动更新列表。
闰秒更新的权威来源是Bulletin C issued by IERS that also provides TAI-UTC data since 1961。
您可以每半年(例如,一月、七月)对 IESR 执行一次 ping 操作,以使用 leap_second_client.py
以编程方式找出下一个闰秒的计划时间。
据我所知,目前在 python 中没有从当前日期的 gps 纪元获取闰秒的功能。 investigation/development下的相应补丁:leap seconds path in datetime
您能否建议获得闰秒的最佳方法?我找到了解决方案:
def getLeapSec(Tgps):
daysFromGPSEpoch = timedelta(seconds=Tgps).days
tableLeapSec = ([
[0, 542, 907, 1272, 2003, 2917, 3648, 4013, 4560, 4925, 5290, 5839, 6386, 6935, 9492, 10588, 11865, 12960],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
])
for i in range(1, len(tableLeapSec[0])):
if daysFromGPSEpoch < tableLeapSec[0][i]:
return tableLeapSec[1][i-1]
示例:
In [25]: Tgps = 1092121243.0
In [26]: getLeapSec(Tgps)
Out[26]: 16
您可以在您的系统上使用 leapseconds
module if you have access to the up-to-date tzfile:
>>> from datetime import datetime, timedelta
>>> import leapseconds
>>> Tgps = 1092121243.0
>>> gps_time = datetime(1980, 1, 6) + timedelta(seconds=Tgps)
>>> leapseconds.dTAI_UTC_from_tai(leapseconds.gps_to_tai(gps_time))
datetime.timedelta(0, 35)
主要优点是 tzdata
包每年由系统自动更新几次(比引入闰秒更频繁)。您的代码将使用新数据而无需更改它。
否则,作为回退,您可以对 leapseconds.leapseconds()
调用的结果进行硬编码:
>>> import leapseconds
>>> leapseconds.leapseconds()
[LeapSecond(utc=datetime.datetime(1972, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 10)),
LeapSecond(utc=datetime.datetime(1972, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 11)),
LeapSecond(utc=datetime.datetime(1973, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 12)),
LeapSecond(utc=datetime.datetime(1974, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 13)),
LeapSecond(utc=datetime.datetime(1975, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 14)),
LeapSecond(utc=datetime.datetime(1976, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 15)),
LeapSecond(utc=datetime.datetime(1977, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 16)),
LeapSecond(utc=datetime.datetime(1978, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 17)),
LeapSecond(utc=datetime.datetime(1979, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 18)),
LeapSecond(utc=datetime.datetime(1980, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 19)),
LeapSecond(utc=datetime.datetime(1981, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 20)),
LeapSecond(utc=datetime.datetime(1982, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 21)),
LeapSecond(utc=datetime.datetime(1983, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 22)),
LeapSecond(utc=datetime.datetime(1985, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 23)),
LeapSecond(utc=datetime.datetime(1988, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 24)),
LeapSecond(utc=datetime.datetime(1990, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 25)),
LeapSecond(utc=datetime.datetime(1991, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 26)),
LeapSecond(utc=datetime.datetime(1992, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 27)),
LeapSecond(utc=datetime.datetime(1993, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 28)),
LeapSecond(utc=datetime.datetime(1994, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 29)),
LeapSecond(utc=datetime.datetime(1996, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 30)),
LeapSecond(utc=datetime.datetime(1997, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 31)),
LeapSecond(utc=datetime.datetime(1999, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 32)),
LeapSecond(utc=datetime.datetime(2006, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 33)),
LeapSecond(utc=datetime.datetime(2009, 1, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 34)),
LeapSecond(utc=datetime.datetime(2012, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 35)),
LeapSecond(utc=datetime.datetime(2015, 7, 1, 0, 0), dTAI_UTC=datetime.timedelta(0, 36))]
在这种情况下,当引入下一个闰秒时,您必须手动更新列表。
闰秒更新的权威来源是Bulletin C issued by IERS that also provides TAI-UTC data since 1961。
您可以每半年(例如,一月、七月)对 IESR 执行一次 ping 操作,以使用 leap_second_client.py
以编程方式找出下一个闰秒的计划时间。