-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (72 loc) · 2.62 KB
/
main.py
File metadata and controls
97 lines (72 loc) · 2.62 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
import sys
import os
from datetime import datetime, timedelta
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Font
TEMP_FILE = "temp_start_time.txt"
def write_to_excel(start_time, end_time):
file_name = "work_hours.xlsx"
try:
workbook = load_workbook(file_name)
except FileNotFoundError:
workbook = Workbook()
worksheet = workbook.active
worksheet.append(['Date', 'Start Time', 'End Time', 'Time Difference'])
worksheet = workbook.active
start_datetime = datetime.strptime(start_time, "%H:%M")
end_datetime = datetime.strptime(end_time, "%H:%M")
total_hours = (end_datetime - start_datetime).seconds / 3600
expected_hours = 8
time_difference = total_hours - expected_hours
time_difference_formatted = f"{int(time_difference):02d}:{int((time_difference * 60) % 60):02d}"
time_difference_formatted = f"{'+' if time_difference >= 0 else '-'}{time_difference_formatted}"
row_data = [
datetime.now().strftime("%Y-%m-%d"),
start_time,
end_time,
time_difference_formatted
]
row = worksheet.append(row_data)
last_row = worksheet.max_row
cell_color = "00FF00" if time_difference >= 0 else "FF0000"
worksheet.cell(row=last_row, column=4).font = Font(color=cell_color)
workbook.save(file_name)
def start_session():
start_time_input = input("When did you start work today? (e.g., 8:30): ")
with open(TEMP_FILE, 'w') as temp_file:
temp_file.write(start_time_input)
print("Work session started.")
def stop_session():
if not os.path.exists(TEMP_FILE):
print("No work session found. Start a session first.")
return
with open(TEMP_FILE, 'r') as temp_file:
start_time_input = temp_file.read()
os.remove(TEMP_FILE)
end_time_input = datetime.now().strftime("%H:%M")
print(f"You clocked out at: {end_time_input}")
write_to_excel(start_time_input, end_time_input)
def show_help():
help_text = """
Usage: python work_log.py [start|stop|help]
Commands:
start Start a work session and record the start time.
stop Stop the current work session, calculate the time difference, and update the Excel file.
help Display this help message.
"""
print(help_text)
def main():
if len(sys.argv) < 2:
show_help()
return
command = sys.argv[1].lower()
if command == "start":
start_session()
elif command == "stop":
stop_session()
elif command == "help":
show_help()
else:
print("Invalid command. Use 'start', 'stop', or 'help'.")
if __name__ == "__main__":
main()