-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (86 loc) · 6.92 KB
/
main.py
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
110
111
112
def add_time(start: str, duration: str, day: str = None) -> str:
"""
Adds a duration to a given start time and returns the resulting time, formatted in a 12-hour clock (AM/PM).
Optionally, the function can also return the day of the week if provided, and it handles multiple days later.
Parameters:
start (str): A string representing the start time in the 12-hour format (e.g., '3:00 PM').
duration (str): A string representing the duration in hours and minutes (e.g., '2:30').
day (str, optional): The starting day of the week (e.g., 'Monday'), case insensitive.
Returns:
str: The final time after adding the duration, formatted as 'HH:MM AM/PM'. If a starting day is provided, the resulting day of the week is included. The result includes '(next day)' or '(n days later)' if applicable.
"""
try:
# Split the start time into time (HH:MM) and AM/PM notation.
start_time, start_meridian = start.strip().split() # Strip any leading/trailing spaces and split into two parts: the time (HH:MM) and the AM/PM notation.
# Extract hours and minutes from the time part (HH:MM).
start_hours, start_minutes = map(int, start_time.split(":")) # Split the time string at the ":" to separate hours and minutes, and convert them to integers using map(int).
# Split the duration into hours and minutes.
duration_hours, duration_minutes = map(int, duration.split(":")) # Extract duration hours and minutes by splitting the duration string at ":" and convert both parts to integers using map(int).
# print(f'start_hours = {start_hours}, start_minutes = {start_minutes}, start_meridian = {start_meridian}, duration_hours = {duration_hours}, duration_minutes = {duration_minutes}') # Debugging
# Convert the start time from 12-hour format to 24-hour format for easier calculations.
if start_meridian == "PM" and start_hours != 12: # For PM, convert hour to 24-hour format.
start_hours += 12 # Add 12 hours to the start hours to convert to 24-hour format.
elif start_meridian == "AM" and start_hours == 12: # Special case for 12 AM (midnight).
start_hours = 0 # Convert 12 AM to 00 hours.
# Add the duration to the start time.
total_hours = start_hours + duration_hours # Add hours from the duration.
total_minutes = start_minutes + duration_minutes # Add minutes from the duration.
# Handle overflow of minutes (if minutes >= 60).
if total_minutes >= 60: # If total minutes exceed 59, convert the overflow into hours.
total_hours += 1 # Increment the total hours by 1 to account for the overflow of minutes (i.e., each full 60 minutes adds 1 hour).
total_minutes = total_minutes % 60 # Adjust minutes to be within the range of 0 to 59 by taking the remainder (the minutes after dividing by 60).
# Calculate the number of full days passed by dividing total hours by 24.
days_later = total_hours // 24 # Full days passed by dividing total hours by 24 to get the number of full days.
hours_later = total_hours % 24 # Remaining hours after full days have been accounted for using the modulo operator.
# print(f'hours_later = {hours_later}, meridian = {meridian}') # Debugging
# Convert the resulting time back into 12-hour format with AM/PM.
if hours_later == 0: # If hours_later is 0, it represents midnight (12:00 AM).
hours_later = 12 # Special case for midnight, where 00:00 should be 12:00 AM.
meridian = "AM" # Midnight falls in the AM period.
elif hours_later < 12: # If hours_later is less than 12, it means the time is in the AM period (from 1:00 AM to 11:59 AM).
meridian = "AM" # The time falls between 1:00 AM and 11:59 AM.
elif hours_later == 12: # If hours_later is 12, it represents noon (12:00 PM).
meridian = "PM" # Noon is in the PM period.
else: # If hours_later is greater than 12, it means the time is in the PM period (from 1:00 PM to 11:59 PM).
hours_later -= 12 # For hours between 13:00 and 23:59, subtract 12 to convert to 12-hour format (e.g., 13:00 becomes 1:00 PM).
meridian = "PM" # The time is in the PM period.
# print(f'days_later = {days_later}, hours_later = {hours_later}, meridian = {meridian}') # Debugging
# Handle the optional day of the week.
if day is not None:
# List of days of the week in lowercase for case-insensitive comparison.
days_of_week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] # List of days of the week in lowercase for case-insensitive comparison.
# Find the index of the start day in the list of days.
day_index = days_of_week.index(day.lower())
# Calculate the resulting day index after adding the number of days passed.
end_day_index = (day_index + days_later) % 7 # Wrap around using modulo for more than 7 days later.
end_day = days_of_week[end_day_index] # Get the resulting day of the week.
# print(f'end_day = {end_day}') # Debugging
else:
end_day = None # If no day was provided, no need to compute or return the day.
# Format the resulting time as a string.
formatted_time = f"{hours_later}:{total_minutes:02d} {meridian}" # Format the time in 'HH:MM AM/PM'. Here :02d is to add leading zeros if minutes are less than 10 to format as 'MM'. :02d is a format specifier in Python that formats an integer as a two-digit decimal number, padding with leading zeros if necessary (e.g., 5 becomes 05).
# If a day is provided, include it in the final formatted time.
if end_day is not None:
formatted_time += f", {end_day.capitalize()}" # Capitalize the day name for readability and append it to the formatted time.
# Handle the message for next day or multiple days later.
if days_later == 1:
formatted_time += " (next day)" # If 1 day later, append "(next day)" to the formatted time.
elif days_later > 1:
formatted_time += f" ({days_later} days later)" # If more than 1 day later, append the number of days later to the formatted time.
# Return the final formatted time.
return formatted_time
except Exception as e:
# If an error occurs during execution, return the error message.
return str(e)
print(add_time('3:00 PM', '3:10'))
# Returns: 6:10 PM
print(add_time('11:30 AM', '2:32', 'Monday'))
# Returns: 2:02 PM, Monday
print(add_time('11:43 AM', '00:20'))
# Returns: 12:03 PM
print(add_time('10:10 PM', '3:30'))
# Returns: 1:40 AM (next day)
print(add_time('11:43 PM', '24:20', 'tueSday'))
# Returns: 12:03 AM, Thursday (2 days later)
print(add_time('6:30 PM', '205:12'))
# Returns: 7:42 AM (9 days later)