据我目前在python中了解,没有功能可以从gps epoch获得当前日期的leap秒。正在研究/开发中的适当补丁:leap seconds path in datetime

您能提出最佳方法来获得leap秒吗?我找到了解决方案:

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,则可以使用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软件包每年由系统自动更新几次(引入了leap秒以上)。您的代码将使用新数据,而无需对其进行更改。

否则,作为备用,您可以对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))]


在这种情况下,如果引入下一leap秒,则必须手动更新列表。

seconds秒更新的权威来源是IERS发行的Bulletin C,它也提供了TAI-UTC data since 1961

您可以每半年(例如1月,7月)一次对IESR进行ping操作,以便在计划下一个the秒时使用leap_second_client.py进行编程查找。

10-05 21:23