summaryrefslogtreecommitdiff
path: root/leapsecond.py
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2013-11-27 04:59:17 -0500
committerEric S. Raymond <esr@thyrsus.com>2013-11-27 05:38:26 -0500
commit7995ecf746361fc53d29bf22de11a9762510d5fd (patch)
tree65500f807c9212ed6d5008e3443776fcfeedc22b /leapsecond.py
parent78c2ac35ad2cf127a60961fc1e698c59e01537d1 (diff)
downloadgpsd-7995ecf746361fc53d29bf22de11a9762510d5fd.tar.gz
Make current GPS week available in timebase.h for rollover detection.
Diffstat (limited to 'leapsecond.py')
-rwxr-xr-xleapsecond.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/leapsecond.py b/leapsecond.py
index df6bf490..b7ccec25 100755
--- a/leapsecond.py
+++ b/leapsecond.py
@@ -83,6 +83,12 @@ __locations = [
# between times it might change, in seconds since Unix epoch GMT (1970-01-01T00:00:00).
__cachepath = "/var/run/leapsecond"
+GPS_EPOCH = 315964800 # 6 Jan 1981 00:00:00
+SECS_PER_WEEK = 60 * 60 * 24 * 7 # Seconds per GPS week
+
+def gps_week(t):
+ return (t - GPS_EPOCH)/SECS_PER_WEEK
+
def isotime(s):
"Convert timestamps in ISO8661 format to and from Unix time including optional fractional seconds."
if type(s) == type(1):
@@ -228,12 +234,24 @@ def fetch_leapsecs(filename):
def make_leapsecond_include(infile):
"Get the current leap second count and century from the local cache usable as c preprocessor #define"
leapjumps = fetch_leapsecs(infile)
- year = time.strftime("%Y", time.gmtime(time.time()))
- leapsecs = 0
+ now = int(time.time())
+ year = time.strftime("%Y", time.gmtime(now))
+ gps_week_now = gps_week(now)
+ isodate = isotime(now - now % SECS_PER_WEEK)
+ leapsecs = -1
for leapjump in leapjumps:
if leapjump < time.time():
leapsecs += 1
- return ("#define CENTURY_BASE\t%s00\n" % year[:2]) + ("#define LEAPSECOND_NOW\t%d\n" % (leapsecs-1))
+ return """\
+/*
+ * Constants used for GPS time detection and rollover correction.
+ *
+ * Correct for week beginning %(isodate)s
+ */
+#define CENTURY_BASE\t%(year)s00
+#define LEAPSECOND_NOW\t%(leapsecs)d
+#define GPS_WEEK_NOW\t%(gps_week_now)d
+""" % locals()
def leastsquares(tuples):
"Generate coefficients for a least-squares fit to the specified data."