-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
62 lines (44 loc) · 1.72 KB
/
Copy pathtimer.py
File metadata and controls
62 lines (44 loc) · 1.72 KB
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
import time
class Timer:
def __init__(self, time_left=0, paused=False):
self.end_time_reference = time.time() + time_left
self.paused = paused
self.pause_timer_duration = 0
def set_time(self, time_left):
self.end_time_reference = time.time() + time_left
self.paused = False
self.pause_timer_duration = 0
def toggle_pause(self):
if not self.paused:
self.pause_timer_duration = self.get_time_left(round_time=False)
else:
self.end_time_reference = time.time() + self.pause_timer_duration
self.pause_timer_duration = 0
self.paused = not self.paused
# Returns the number of seconds left on the timer (0 if the timer is at 0)
def get_time_left(self, round_time=True):
if self.paused:
return round(self.pause_timer_duration)
seconds = round(self.end_time_reference - time.time())
if seconds <= 0:
self.end_time_reference = time.time()
return 0
if not round_time:
return self.end_time_reference - time.time()
return seconds
# Adds seconds to the timer, if the timer is at 0, it will start counting from now
def add_seconds(self, seconds):
if self.get_time_left() == 0:
self.end_time_reference = time.time()
self.end_time_reference += seconds
# Formats seconds into HH:MM:SS
@staticmethod
def format_time(seconds: int) -> str:
seconds = round(seconds)
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
return f"{hours:02}:{minutes:02}:{secs:02}"
if __name__ == "__main__":
t = Timer(10)
print(Timer.format_time(4123))