py-basic/src/stopwatch.py

32 lines
785 B
Python
Raw Normal View History

2023-04-14 14:27:45 +02:00
#!/bin/env python3
2023-04-16 17:30:32 +02:00
import time
2023-04-14 14:27:45 +02:00
import datetime
import sys
2023-04-16 17:30:32 +02:00
import time
2023-04-14 14:27:45 +02:00
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()
2023-04-16 17:30:32 +02:00
time.sleep(1)
2023-04-14 14:27:45 +02:00
def main():
try:
timer = stopwatch()
timer.display()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()