added working stopwatch

This commit is contained in:
Christoph J. Scherr 2023-04-14 14:27:45 +02:00
parent 093840057e
commit a47eea6308
1 changed files with 30 additions and 0 deletions

30
stopwatch.py Executable file
View File

@ -0,0 +1,30 @@
#!/bin/env python3
from time import localtime, sleep, struct_time, time
import datetime
import sys
class stopwatch:
def __init__(self) -> None:
self.start_time = datetime.datetime.now().replace(microsecond=0)
def display(self) -> None:
print("started at:\t%s" % self.start_time)
while True:
now = datetime.datetime.now().replace(microsecond=0)
elapsed = (now - self.start_time)
text0 = ("\rcurrent:\t%s" % now)
text1 = ("elapsed:\t%s" % elapsed)
sys.stdout.write('\r' + str(text0+"\t\t"+text1) + "\t" * 3)
sys.stdout.flush()
sleep(1)
def main():
try:
timer = stopwatch()
timer.display()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()