]> git.sthu.org Git - shutils.git/commitdiff
Add timer.py
authorStefan Huber <shuber@sthu.org>
Sat, 9 Nov 2013 15:35:09 +0000 (16:35 +0100)
committerStefan Huber <shuber@sthu.org>
Sat, 9 Nov 2013 15:35:09 +0000 (16:35 +0100)
timer.py [new file with mode: 0644]

diff --git a/timer.py b/timer.py
new file mode 100644 (file)
index 0000000..4f842e7
--- /dev/null
+++ b/timer.py
@@ -0,0 +1,118 @@
+#!/usr/bin/python3
+
+import sys
+import time
+import datetime
+
+units = {"s" : 1, "m" : 60, "h" : 60*60, "d" : 24*60*69 }
+
+def reprint(value, file=sys.stdout, end=""):
+    print("\r" + value, file=file, end=end)
+
+
+def usage():
+    print("""Usage: {0} [--reverse] [--offset <time>] [--update <time>] [--round <int>]
+       {0} -h
+       {0} --help
+
+Options:
+  --reverse           count backwards
+  --offset <time>     start counting from specified offset
+  --update <time>     interval between two screen updates
+  --round <int>       round seconds by specified number of digits
+
+<time> is an integer specifing the number of seconds or a string
+of the format "<int>(Undefined, Undefined, Undefined, Undefined)" with the suffixes d(ay), h(our),
+m(inute), s(econd).
+""".format(sys.argv[0]))
+
+def parseTime(value):
+
+    factor = 1
+
+    if value[-1] in units:
+        factor = units[value[-1]]
+        value = value[:-1]
+
+    return float(value)*factor
+
+
+def formatTime(secs, digits=0):
+
+    sign = ""
+    if secs < 0:
+        sign = "-"
+        secs = -secs
+
+    days = int(secs/units["d"])
+    secs -= days * units["d"]
+
+    hours = int(secs/units["h"])
+    secs -= hours * units["h"]
+
+    mins = int(secs/units["m"])
+    secs -= mins * units["m"]
+
+    s = "%.{0}f".format(digits) % (round(secs, digits) ) + "s"
+    if days>0:
+        s = " %dd %dh %dm %s" %(days, hours, mins, s)
+    elif hours>0:
+        s = " %dh %dm %s" %(hours, mins, s)
+    elif mins>0:
+        s = " %dm %s" %(mins, s)
+
+    s = sign + s
+    return s
+
+
+def parseArgs():
+    opts = {"help" : False, "reverse" : False, "offset" : 0.0, "update" : 1,
+            "round" : 0}
+    i=1
+    while i<len(sys.argv):
+
+        if sys.argv[i] in ["-h", "--help"]:
+            opts["help"] = True
+        elif sys.argv[i] in ["--reverse"]:
+            opts["reverse"] = True
+        elif sys.argv[i] in ["--offset"]:
+            i = i+1
+            opts["offset"] = parseTime(sys.argv[i])
+        elif sys.argv[i] in ["--update"]:
+            i = i+1
+            opts["update"] = parseTime(sys.argv[i])
+        elif sys.argv[i] in ["--round"]:
+            i = i+1
+            opts["round"] = int(sys.argv[i])
+        else:
+            raise "Unknown option '{0}'".format(sys.argv[i])
+
+        i = i+1
+    return opts
+
+
+if __name__ == "__main__":
+
+    opts = parseArgs()
+
+    if opts["help"]:
+        usage()
+        sys.exit(0)
+
+    start=time.time()
+
+    print("started at " + str(datetime.datetime.now()))
+
+    while True:
+
+        diff=time.time()-start
+        if opts["reverse"]:
+            diff = -diff
+        diff += opts["offset"]
+
+        reprint(formatTime(diff, opts["round"]) + "                    ")
+        time.sleep(opts["update"])
+
+
+
+