-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.py
72 lines (55 loc) · 1.73 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import time
class Timer:
def __init__(self):
self.timestart = None
self.timepause = None
self.paused = None
def start_timer(self):
self.timestart = time.time()
def pause_timer(self):
if self.timestart is None:
raise ValueError("time not started")
if self.paused:
raise ValueError("time is already paused")
self.timepause = time.time()
self.paused = True
def resume_timer(self, backwards=False):
if self.timestart is None:
raise ValueError("time not started")
if not self.paused:
raise ValueError("time is not paused")
if self.reversed:
elapsed_pause = self.timepause - time.time()
else:
elapsed_pause = time.time() - self.timepause
self.timestart = self.timestart + elapsed_pause
self.paused = False
def get_timer(self):
if self.timestart is None:
raise ValueError("timer not started")
if self.paused:
return self.timepause - self.timestart
else:
if self.reversed:
return self.timestart - time.time()
else:
return time.time() - self.timestart
def get_start_time(self):
return self.timestart
def is_paused(self):
return self.paused
if __name__ == "__main__":
timer = Timer()
# print(timer.get_timer())
timer.start_timer()
print(f'{timer.timestart = }')
time.sleep(1)
timer.pause_timer()
print(timer.get_timer())
timer.reversed = True
# time.sleep(1)
timer.resume_timer()
print(f'{timer.timestart = }')
print(timer.get_timer())
time.sleep(1)
print(timer.get_timer())