据我所知,目前在python中,没有从gps epoch获取当前日期闰秒的功能。正在调查/开发的适当补丁: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
如果您可以访问系统上的最新tzfile,则可以使用leapseconds
模块:
>>> 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))]
在这种情况下,当引入下一个闰秒时,您必须手动更新列表。
闰秒更新的权威来源是IERS发布的公告C,该公告也提供了自1961年以来的TAI-UTC数据。
您可以每半年(例如,1月、7月)ping一次IESR,以便使用leap_second_client.py
以编程方式确定下一闰秒的计划时间。