-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
51 lines (39 loc) · 1.58 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
import datetime
import threading
import time
class TimerThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
threading.Thread.__init__(self, group=group, target=target, name=name,
verbose=verbose)
self.session = args
def run(self):
while (self.session.remaining_time>datetime.timedelta(seconds=0)):
self.session.set_remaining_time()
time.sleep(0.01)
class SessionTimer():
def __init__(self, session_length):
self.start_time = None
self.stop_time = None
self.suspended = False
self.remaining_time = datetime.timedelta(seconds=session_length)
self.session_length = datetime.timedelta(seconds=session_length)
self.timerthread = TimerThread(args=(self))
def get_remaining_time(self):
return self.remaining_time
def set_remaining_time(self):
if not self.suspended:
self.remaining_time = self.stop_time - datetime.datetime.now()
def start_session(self):
self.start_time = datetime.datetime.now()
self.stop_time = self.start_time + self.session_length
self.timerthread.start()
def stop_session(self):
self.stop_time = datetime.datetime.now()
self.remaining_time = datetime.timedelta(seconds=0)
def suspend_session(self):
self.suspended = True
def restart_session(self):
self.suspended = False
if __name__ == "__main__":
pass