| 1 | import sys |
| 2 | from time import sleep, time |
| 3 | from datetime import datetime |
| 4 | |
| 5 | from optparse import OptionParser |
| 6 | |
| 7 | usage = "Usage: timer.py [options]" |
| 8 | |
| 9 | parser = OptionParser(usage=usage, prog="timer", version="simple clock") |
| 10 | |
| 11 | parser.add_option("-t", "--time", dest="maxtime", metavar='maxtime', |
| 12 | default=None, help="maximum run time (seconds)") |
| 13 | parser.add_option("-u", "--update", dest="update", metavar='update', |
| 14 | default=0.1, help="update time (seconds)") |
| 15 | |
| 16 | (options, args) = parser.parse_args() |
| 17 | |
| 18 | update = float(options.update) |
| 19 | maxtime = options.maxtime |
| 20 | if maxtime is not None: |
| 21 | maxtime = float(maxtime) |
| 22 | |
| 23 | t0 = time() |
| 24 | |
| 25 | while True: |
| 26 | sleep(update) |
| 27 | print("%s : %.3f " % (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), time()-t0)) |
| 28 | sys.stdout.flush() |
| 29 | if maxtime is not None and (time()-t0 > maxtime): |
| 30 | break |
| 31 | |
| 32 | print("done.") |
| 33 | sleep(0.5) |