#!/usr/bin/python import urllib as UL import sys import re opener = ((lambda: open(sys.argv[1])) if len(sys.argv) > 1 else (lambda: UL.urlopen('ftp://time.nist.gov/pub/leap-seconds.list'))) comment = re.compile(r'#.*\s*\Z') parse = re.compile(r'\A(\d+)\s+(\d+)\s*\Z') first = True with opener() as f: for line in f: line = comment.sub('', line) if line == '': continue if first: first = False continue stamp, offset = map(long, parse.match(line).groups()) stamp -= (70 * 365 + 17) * 86400 - offset + 1 stamp_bytes = [] while stamp != 0: stamp_bytes.append(chr(stamp % 256)) stamp //= 256 while len(stamp_bytes) < 7: stamp_bytes.append(chr(0)) stamp_bytes.append(chr(64)) stamp_bytes.reverse() sys.stdout.write(''.join(stamp_bytes))