From a47eea630871543a5dea11945299c30f13597732 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 14 Apr 2023 14:27:45 +0200 Subject: [PATCH] added working stopwatch --- stopwatch.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 stopwatch.py diff --git a/stopwatch.py b/stopwatch.py new file mode 100755 index 0000000..3984a09 --- /dev/null +++ b/stopwatch.py @@ -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()