-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.py
59 lines (51 loc) · 1.95 KB
/
activity.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
import configuration as conf
import csv
from datetime import datetime
class Activity:
def __init__(self, user_id, category, name):
self.category = category
self.name = name
self.user_id = user_id
self.start_time = None
self.end_time = None
self.paused_total = 0
self.paused_start = None
def start_activity(self):
self.start_time = datetime.now()
def pause_activity(self):
if self.paused_start != None:
print("ERROR: Activity not paused")
return
self.paused_start = datetime.now()
def resume_activity(self):
paused = datetime.now() - self.paused_start
self.paused_start = None
self.paused_total += paused.seconds
def get_duration(self):
total_duration = (self.end_time - self.start_time)
return total_duration.seconds - self.paused_total
def record_activity(self):
with open(conf.DATA_FILE.format(user_id=self.user_id), 'a', newline='') as csvfile:
data_writer = csv.writer(csvfile)
data_writer.writerow([
self.start_time.timestamp(),
self.end_time.timestamp(),
self.start_time.year, # s_year
self.end_time.year, # e_year
self.start_time.month, # s_month
self.end_time.month, # e_month
self.start_time.day, # s_day
self.end_time.day, # e_day
datetime.strftime(self.start_time, "%H:%M:%S"), # s_hour
datetime.strftime(self.end_time, "%H:%M:%S"), # e_hour
self.category,
self.name,
self.paused_total,
self.get_duration(),
])
def end_activity(self):
if self.start_time == None:
print("ERROR: Activity not started yet")
return
self.end_time = datetime.now()
self.record_activity()