-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeControl.py
More file actions
executable file
·109 lines (102 loc) · 3.58 KB
/
timeControl.py
File metadata and controls
executable file
·109 lines (102 loc) · 3.58 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3
from datetime import datetime
import time, os, csv
def help(started):
print('#######################################')
if not started:
print('# s -- Start counting your worktime #')
else:
print('# p -- Pause the clock #')
print('# r -- Resume the clock #')
print('# s -- Stop the clock #')
print('# q -- Quit #')
print('#######################################')
print('>>>', end='')
def invalid():
print('Invalid input')
csv_columns = ['Day', 'Hour', 'WorkTime', 'Concept']
csv_file = 'data.csv'
def runClock():
print('Clock started')
finished = False
paused = False
startTime = datetime.now()
initDay = startTime.strftime('%d/%m/%Y')
initHour = startTime.strftime('%H:%M:%S')
pauseTime = startTime
while(not finished):
help(True)
entrada = input()
if entrada == 's':
concept = input('Enter work done: ')
finishTime = datetime.now()
if paused:
worktime = pauseTime - startTime
else:
worktime = finishTime - startTime
print(f'The {initDay} at {initHour} worked {worktime} in {concept}')
try:
with open(csv_file, 'a') as csvfile:
writer = csv.DictWriter(csvfile, delimiter=';', fieldnames=csv_columns)
writer.writerow({'Day': initDay, 'Hour': initHour, 'WorkTime': str(worktime), 'Concept': concept})
except IOError:
print('I/O error')
finished = True
elif entrada == 'p':
if not paused:
pauseTime = datetime.now()
print('Clock paused')
paused = True
else:
print('Clock is already paused')
elif entrada == 'r':
if paused:
pauseEnd = datetime.now()
pauseDuration = pauseEnd - pauseTime
startTime += pauseDuration
print(f'Clock resumed after paused for {pauseDuration}')
paused = False
else:
print('Clock is not paused')
elif entrada == 'c':
if startTime is not None:
current_time = datetime.now()
if paused:
elapsed_time = current_time - startTime - (current_time - pauseTime)
else:
elapsed_time = current_time - startTime
print(f'Elapsed time: {elapsed_time}')
else:
print('Clock has not started yet')
elif entrada == 'h':
help(True)
elif entrada == 'q':
print('Quitting ...')
exit(1)
else:
invalid()
def init():
if not os.path.exists(csv_file) or os.stat(csv_file).st_size == 0:
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, delimiter=';', fieldnames=csv_columns)
writer.writeheader()
print('Created file data.csv with info')
except IOError:
print('I/O error')
if __name__ == '__main__':
init()
print('Welcome, enter a valid command (h for more info)')
finished = False
while(not finished):
help(False)
entrada = input()
if entrada == 'h':
help(False)
elif entrada == 's':
runClock()
elif entrada == 'q':
finished = True
print('Quitting ...')
else:
invalid()