summaryrefslogtreecommitdiff
path: root/leapsecond.py
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2011-01-18 20:20:57 -0500
committerEric S. Raymond <esr@thyrsus.com>2011-01-18 20:20:57 -0500
commitb8b5fdd72e55c64e0b212a23baea682364e98f04 (patch)
tree037a42c4773b717ed2d721cab3a9ba7881f155ff /leapsecond.py
parentf63df07cf41e7fc69a0f51d9bf35e75499b32909 (diff)
downloadgpsd-b8b5fdd72e55c64e0b212a23baea682364e98f04.tar.gz
Refactor the leap-second fetcher.
Diffstat (limited to 'leapsecond.py')
-rwxr-xr-xleapsecond.py72
1 files changed, 32 insertions, 40 deletions
diff --git a/leapsecond.py b/leapsecond.py
index dd062e80..16226722 100755
--- a/leapsecond.py
+++ b/leapsecond.py
@@ -11,8 +11,7 @@
#
# With the -o option, take a date in Unix local time and convert to RFC822.
#
-# With -c, generate a C table that maps leap-second offset to plausible years.
-# With -p, generate a Python table.
+# With -c, generate a C initializer listing leap seconds in Unix time.
#
# With the -n option, compute Unix local time for an IERS leap-second event
# given as a three-letter English Gregorian month abbreviation followed by
@@ -107,6 +106,33 @@ def get():
pass
return current_offset
+def save_leapseconds(outfile):
+ "Fetch the USNO leap-second history data and makw a leap-second list."
+ skip = True
+ leapsecs = []
+ # This code assumes that after 1980, leap-second increments are
+ # always integrally one second and every increment is listed here
+ leapsecs = []
+ try:
+ for line in urllib.urlopen("ftp://maia.usno.navy.mil/ser7/tai-utc.dat"):
+ if line.startswith(" 1980"):
+ skip = False
+ if skip:
+ continue
+ fields = line.strip().split()
+ leapsecs.append(leapbound(fields[0], fields[1]))
+ leapsecs.append(unix_to_rfc822(time.time()))
+ def label(i):
+ if i == len(leapsecs) - 1:
+ return '?'
+ else:
+ return str(i)
+ with open(outfile, "w") as fp:
+ for (i, b) in enumerate(leapsecs):
+ fp.write(" %s, // %s -> %s\n" % (rfc822_to_unix(b), b, label(i)))
+ except IOError:
+ print >>sys.stderr, "Fetch from USNO failed, %s not updated." % outfile
+
def rfc822_to_unix(tv):
"Local Unix time to RFC822 date."
return time.mktime(time.strptime(tv, "%d %b %Y %H:%M:%S"))
@@ -132,9 +158,8 @@ if __name__ == '__main__':
next = False
from_rfc822 = False
to_rfc822 = False
- py_epochs = False
c_epochs = None
- (options, arguments) = getopt.getopt(sys.argv[1:], "c:i:n:o:p")
+ (options, arguments) = getopt.getopt(sys.argv[1:], "c:i:n:o:")
for (switch, val) in options:
if (switch == '-c'): # Generate C initializer listing leap seconds
c_epochs = val
@@ -144,10 +169,8 @@ if __name__ == '__main__':
next = True
elif (switch == '-o'): # Compute RFC822 date from Unix time
to_rfc822 = True
- elif (switch == '-p'): # Generate Python list of leap seconds
- py_epochs = True
- if not next and not from_rfc822 and not to_rfc822 and not c_epochs and not py_epochs:
+ if not next and not from_rfc822 and not to_rfc822 and not c_epochs:
print "Current leap second:", retrieve()
raise SystemExit, 0
@@ -159,39 +182,8 @@ if __name__ == '__main__':
print unix_to_rfc822(float(val))
raise SystemExit, 0
- if c_epochs or py_epochs:
- skip = True
- leapsecs = []
- # This code assumes that after 1980, leap-second increments are
- # always integrally one second and every increment is listed here
- leapsecs = []
- try:
- for line in urllib.urlopen("ftp://maia.usno.navy.mil/ser7/tai-utc.dat"):
- if line.startswith(" 1980"):
- skip = False
- if skip:
- continue
- fields = line.strip().split()
- leapsecs.append(leapbound(fields[0], fields[1]))
- leapsecs.append(unix_to_rfc822(time.time()))
- def label(i):
- if i == len(leapsecs) - 1:
- return '?'
- else:
- return str(i)
- if c_epochs:
- with open(c_epochs, "w") as fp:
- for (i, b) in enumerate(leapsecs):
- fp.write(" %s, // %s -> %s\n" % (rfc822_to_unix(b), b, label(i)))
- else:
- leapsecs = [(rfc822_to_unix(l), b, label(b)) for (i, b) in enumerate(leapsecs)]
- import pprint
- print '''# This table is generated by leapsecond.py; do not hand-hack!
-
- leapsecs ='''
- pprint.pprint(leapsecs)
- except IOError:
- print >>sys.stderr, "Fetch of USNO data failed, leap seconds not updated."
+ if c_epochs:
+ save_leapseconds(c_epochs)
raise SystemExit, 0
if val[:3].lower() not in ("jun", "dec"):