summaryrefslogtreecommitdiff
path: root/leapsecond.py
diff options
context:
space:
mode:
authorThomas Petazzoni <thomas.petazzoni@free-electrons.com>2012-10-23 11:32:12 -0400
committerEric S. Raymond <esr@thyrsus.com>2012-10-23 11:32:12 -0400
commit0a4db197709ca3bb39ce96a688382a581dace967 (patch)
tree36bebac06edf8ff77fd7a58fea4b1dd1ae569607 /leapsecond.py
parent692416b9b348cb7f2324e24cf69b7243b422ff6d (diff)
downloadgpsd-0a4db197709ca3bb39ce96a688382a581dace967.tar.gz
Restore Python 2.5 compatibility.
Signed-off-by: Eric S. Raymond <esr@thyrsus.com>
Diffstat (limited to 'leapsecond.py')
-rwxr-xr-xleapsecond.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/leapsecond.py b/leapsecond.py
index 2059f6c4..cdacdb46 100755
--- a/leapsecond.py
+++ b/leapsecond.py
@@ -24,7 +24,6 @@
# BSD terms apply: see the file COPYING in the distribution root for details.
#
import os, urllib, re, random, time, calendar, math, sys
-import gps.misc
__locations = [
(
@@ -48,6 +47,28 @@ __locations = [
# between times it might change, in seconds since Unix epoch GMT.
__cachepath = "/var/run/leapsecond"
+def isotime(s):
+ "Convert timestamps in ISO8661 format to and from Unix time."
+ if type(s) == type(1):
+ return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(s))
+ elif type(s) == type(1.0):
+ date = int(s)
+ msec = s - date
+ date = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(s))
+ return date + "." + repr(msec)[3:]
+ elif type(s) == type("") or type(s) == type(u""):
+ if s[-1] == "Z":
+ s = s[:-1]
+ if "." in s:
+ (date, msec) = s.split(".")
+ else:
+ date = s
+ msec = "0"
+ # Note: no leap-second correction!
+ return calendar.timegm(time.strptime(date, "%Y-%m-%dT%H:%M:%S")) + float("0." + msec)
+ else:
+ raise TypeError
+
def retrieve():
"Retrieve current leap-second from Web sources."
random.shuffle(__locations) # To spread the load
@@ -261,10 +282,10 @@ if __name__ == '__main__':
print unix_to_rfc822(float(val))
raise SystemExit, 0
elif (switch == '-I'): # Compute Unix time from ISO8601 date
- print gps.misc.isotime(val)
+ print isotime(val)
raise SystemExit, 0
elif (switch == '-O'): # Compute ISO8601 date from Unix time
- print gps.misc.isotime(float(val))
+ print isotime(float(val))
raise SystemExit, 0
print "Current leap second:", retrieve()