#!/usr/bin/python -u
""" AtomicClock.py - Access the NIST standard time service.

    The script will output the current time together with a marker
    indicating the source of information (NIST = genuine NIST signal,
    [NIST] = NIST calibrated CPU clock, CPU = uncalibrated CPU clock).

    You can call this script with a calibration float as parameter.
    It will then be set as calibration for the NIST module to use.

"""
import sys
import DateTime.NIST
from time import sleep

if len(sys.argv) == 2:
    try:
	DateTime.NIST.set_calibration(float(sys.argv[1]))
    except ValueError:
	pass

def run():
    while 1:

	if DateTime.NIST.calibrating and DateTime.NIST.online():
	    # Using the real thing
	    marker = 'NIST'
	elif DateTime.NIST.calibrated:
	    # Using calibrated CPU clock
	    marker = '[NIST]'
	else:
	    # Using the uncalibrated CPU clock
	    marker = 'CPU'

	# Get and show time
	utc = DateTime.NIST.utctime()
	print '  %-6s  Local Time: %s   UTC: %s\r' % \
	      (marker,DateTime.utc2local(utc),utc),
	sys.stdout.flush()
	sleep(1)

	# Recalibrate every 10 minutes
	if utc.minute % 10 == 0:
	    DateTime.NIST.reset_auto_calibration()

if __name__ == '__main__':
    try:
	run()
    except KeyboardInterrupt:
	print
	print 'Current CPU clock calibration:',DateTime.NIST.calibration
