Skip to content

Commit 6e43e5f

Browse files
Refactor timesheet_helper.py to calculate weekly total duration
1 parent bc87d85 commit 6e43e5f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

timesheet_helper.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,52 @@ def duration(match):
2424
END = "\033[0m"
2525

2626
day_total_duration = timedelta()
27+
weekly_total_duration = timedelta()
2728
day_tasks = []
2829
output = []
2930

3031
for line in lines:
3132
is_day_line = re.match(r'^\s*•\s*(\w+day)', line)
3233
if is_day_line:
3334
if day_tasks:
34-
day_tasks[0] += f' Total: {day_total_duration.total_seconds() / 3600:.2f}'
35-
output.append('\n'.join(day_tasks) + '\n')
35+
day_total_in_hours = day_total_duration.total_seconds() / 3600
36+
day_tasks[0] = day_tasks[0].rstrip() + f' Total: {day_total_in_hours:.2f} hours'
37+
output.append('\n'.join(day_tasks))
38+
weekly_total_duration += day_total_duration
3639
day_tasks = []
40+
day_total_duration = timedelta()
3741

3842
day = re.sub(r'^\s*•\s*(\w+day)', r'\1', line)
3943
day = re.sub(r'(\w+day)', BOLD + UNDERLINE + r'\1' + END, day)
44+
output.append('')
4045
day_tasks.append(day)
41-
day_total_duration = timedelta()
4246
else:
4347
timespans = re.finditer(pattern, line)
4448
total_duration = timedelta()
4549
for timespan in timespans:
46-
total_duration += duration(timespan)
47-
day_total_duration += duration(timespan)
50+
span_duration = duration(timespan)
51+
total_duration += span_duration
52+
day_total_duration += span_duration
4853

4954
total_duration_hours = total_duration.total_seconds() / 3600
5055
total_duration_hours = "{:.2f}".format(total_duration_hours)
5156

5257
if re.search(pattern, line):
53-
# Remove times and trailing commas
5458
task = re.sub(pattern + ',?', '', line).strip()
55-
task = re.sub(r'^\s*o\s*', '\t• ', task)
59+
task = re.sub(r'^\s*o\s*', '• ', task)
5660
task += f" {total_duration_hours}"
5761
day_tasks.append(task)
5862

5963
if day_tasks:
60-
day_tasks[0] += f' Total: {day_total_duration.total_seconds() / 3600:.2f}'
61-
output.append('\n'.join(day_tasks) + '\n')
64+
day_total_in_hours = day_total_duration.total_seconds() / 3600
65+
total_string = f'Total: {day_total_in_hours:.2f} hours'
66+
day_string = day_tasks[0].ljust(20) # Adjust the number as needed
67+
day_tasks[0] = day_string + total_string
68+
output.append('\n'.join(day_tasks))
69+
weekly_total_duration += day_total_duration
70+
71+
weekly_total_hours = weekly_total_duration.total_seconds() / 3600
72+
output.append(f'\nWeekly Total: {weekly_total_hours:.2f} hours')
6273

6374
result = '\n'.join(output)
6475
return result

0 commit comments

Comments
 (0)