-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild a Time Calculator Project.py
More file actions
65 lines (44 loc) · 1.73 KB
/
Copy pathBuild a Time Calculator Project.py
File metadata and controls
65 lines (44 loc) · 1.73 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
def add_time(start, duration, day=None):
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
time, period = start.split(" ")
f_time, f_minute = map(int, time.split(":"))
e_time, e_minute = map(int, duration.split(":"))
if period == "PM" and f_time != 12:
f_time += 12
if period == "AM" and f_time == 12:
f_time = 0
total_hours = f_time + e_time
total_minutes = f_minute + e_minute
if total_minutes >= 60:
total_minutes -= 60
total_hours += 1
count_days = total_hours // 24
final_hour = total_hours % 24
if final_hour >= 12:
period = "PM"
if final_hour > 12:
final_hour -= 12
else:
period = "AM"
if final_hour == 0:
final_hour = 12
format_minute = f"{total_minutes:02}"
if day is not None:
index_of_day = days_of_week.index(day.capitalize())
new_day_index = (index_of_day + count_days) % 7
new_day = days_of_week[new_day_index]
if day:
if count_days == 0:
return f"{final_hour}:{format_minute} {period}, {new_day}"
elif count_days == 1:
return f"{final_hour}:{format_minute} {period}, {new_day} (next day)"
else:
return f"{final_hour}:{format_minute} {period}, {new_day} ({count_days} days later)"
else:
if count_days == 0:
return f"{final_hour}:{format_minute} {period}"
elif count_days == 1:
return f"{final_hour}:{format_minute} {period} (next day)"
else:
return f"{final_hour}:{format_minute} {period} ({count_days} days later)"
print(add_time("3:00 PM", "3:10", "Tuesday"))