]>
git.sthu.org Git - shutils.git/blob - timer.py
7 units
= {"s" : 1, "m" : 60, "h" : 60*60, "d" : 24*60*69 }
9 def reprint(value
, file=sys
.stdout
, end
=""):
10 print("\r" + value
, file=file, end
=end
)
14 print("""Usage: {0} [--reverse] [--offset <time>] [--update <time>] [--round <int>]
19 --reverse count backwards
20 --offset <time> start counting from specified offset
21 --update <time> interval between two screen updates
22 --round <int> round seconds by specified number of digits
24 <time> is an integer specifing the number of seconds or a string
25 of the format "<int>(Undefined, Undefined, Undefined, Undefined)" with the suffixes d(ay), h(our),
27 """.format(sys
.argv
[0]))
33 if value
[-1] in units
:
34 factor
= units
[value
[-1]]
37 return float(value
)*factor
40 def formatTime(secs
, digits
=0):
47 days
= int(secs
/units
["d"])
48 secs
-= days
* units
["d"]
50 hours
= int(secs
/units
["h"])
51 secs
-= hours
* units
["h"]
53 mins
= int(secs
/units
["m"])
54 secs
-= mins
* units
["m"]
56 s
= "%.{0}f".format(digits
) % (round(secs
, digits
) ) + "s"
58 s
= " %dd %dh %dm %s" %(days
, hours
, mins
, s
)
60 s
= " %dh %dm %s" %(hours
, mins
, s
)
62 s
= " %dm %s" %(mins
, s
)
69 opts
= {"help" : False, "reverse" : False, "offset" : 0.0, "update" : 1,
72 while i
<len(sys
.argv
):
74 if sys
.argv
[i
] in ["-h", "--help"]:
76 elif sys
.argv
[i
] in ["--reverse"]:
77 opts
["reverse"] = True
78 elif sys
.argv
[i
] in ["--offset"]:
80 opts
["offset"] = parseTime(sys
.argv
[i
])
81 elif sys
.argv
[i
] in ["--update"]:
83 opts
["update"] = parseTime(sys
.argv
[i
])
84 elif sys
.argv
[i
] in ["--round"]:
86 opts
["round"] = int(sys
.argv
[i
])
88 raise "Unknown option '{0}'".format(sys
.argv
[i
])
94 if __name__
== "__main__":
104 print("started at " + str(datetime
.datetime
.now()))
108 diff
=time
.time()-start
111 diff
+= opts
["offset"]
113 reprint(formatTime(diff
, opts
["round"]) + " ")
114 time
.sleep(opts
["update"])