-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_calculator
53 lines (46 loc) · 2.32 KB
/
time_calculator
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
def add_time(start_time, duration, start_day=None):
# Parse the start time and convert it to 24-hour format
start_time, period = start_time.split()
start_hour, start_minute = map(int, start_time.split(':'))
if period == 'PM' and start_hour != 12:
start_hour += 12
# Parse the duration time and calculate the total number of minutes
duration_hour, duration_minute = map(int, duration.split(':'))
total_minutes = start_hour * 60 + start_minute + duration_hour * 60 + duration_minute
# Calculate the resulting hour and minute in 24-hour format
result_hour = (total_minutes // 60) % 24
result_minute = total_minutes % 60
# Convert the resulting hour and minute to 12-hour format and period
if result_hour == 0:
result_hour = 12
result_period = 'AM'
elif result_hour < 12:
result_period = 'AM'
elif result_hour == 12:
result_period = 'PM'
else:
result_hour -= 12
result_period = 'PM'
# Calculate the number of days later
num_days_later = total_minutes // (24 * 60)
# Determine the day of the week of the result if the starting day is given
days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
if start_day:
start_day = start_day.lower().capitalize()
start_day_index = days_of_week.index(start_day)
result_day_index = (start_day_index + num_days_later) % 7
result_day = days_of_week[result_day_index]
# Format the resulting time and date string
if start_day and num_days_later == 0:
result = f"{result_hour:02d}:{result_minute:02d} {result_period}, {result_day}"
elif start_day and num_days_later == 1:
result = f"{result_hour:02d}:{result_minute:02d} {result_period}, {result_day} (next day)"
elif start_day and num_days_later > 1:
result = f"{result_hour:02d}:{result_minute:02d} {result_period}, {result_day} ({num_days_later} days later)"
elif not start_day and num_days_later == 0:
result = f"{result_hour:02d}:{result_minute:02d} {result_period}"
elif not start_day and num_days_later == 1:
result = f"{result_hour:02d}:{result_minute:02d} {result_period} (next day)"
else:
result = f"{result_hour:02d}:{result_minute:02d} {result_period} ({num_days_later} days later)"
return result