py-basic/stopwatch.py

32 lines
785 B
Python
Executable File

#!/bin/env python3
import time
import datetime
import sys
import time
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()
time.sleep(1)
def main():
try:
timer = stopwatch()
timer.display()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()