|
| 1 | +def add_time(start, duration, starting_day=None): |
| 2 | + start_time, period = start.split() |
| 3 | + start_hour, start_minute = map(int, start_time.split(':')) |
| 4 | + duration_hour, duration_minute = map(int, duration.split(':')) |
| 5 | + if period == 'PM': |
| 6 | + start_hour += 12 |
| 7 | + |
| 8 | + # Calculate total hours and minutes |
| 9 | + total_minutes = start_hour * 60 + start_minute + duration_hour * 60 + duration_minute |
| 10 | + |
| 11 | + # Adjust total hours and minutes |
| 12 | + new_hour = (total_minutes // 60) % 24 |
| 13 | + new_minute = total_minutes % 60 |
| 14 | + |
| 15 | + new_period = 'AM' |
| 16 | + if new_hour >= 12: |
| 17 | + new_period = 'PM' |
| 18 | + if new_hour >= 24: |
| 19 | + new_hour -= 24 |
| 20 | + |
| 21 | + if new_hour > 12: |
| 22 | + new_hour -= 12 |
| 23 | + new_period = 'PM' |
| 24 | + elif new_hour == 12: |
| 25 | + new_period = 'PM' |
| 26 | + elif new_hour == 0: |
| 27 | + new_hour = 12 |
| 28 | + |
| 29 | + day_change = "" |
| 30 | + if starting_day is not None: |
| 31 | + days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] |
| 32 | + starting_day_index = days.index(starting_day.lower().capitalize()) |
| 33 | + new_day_index = (starting_day_index + (total_minutes // (24 * 60))) % 7 |
| 34 | + day_change = f", {days[new_day_index]}" |
| 35 | + |
| 36 | + days_later = total_minutes // (24 * 60) |
| 37 | + days_later_text = "" |
| 38 | + if days_later == 1: |
| 39 | + days_later_text = " (next day)" |
| 40 | + elif days_later > 1: |
| 41 | + days_later_text = f" ({days_later} days later)" |
| 42 | + new_time = f"{new_hour}:{new_minute:02} {new_period}{day_change}{days_later_text}" |
| 43 | + return new_time |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +print(add_time('3:30 PM', '2:12')) |
| 48 | +print(add_time('11:55 AM', '3:12')) |
| 49 | +print(add_time('2:59 AM', '24:00')) |
| 50 | +print(add_time('11:59 PM', '24:05')) |
| 51 | +print(add_time('8:16 PM', '466:02')) |
| 52 | +print(add_time('3:30 PM', '2:12', 'Monday')) |
| 53 | +print(add_time('2:59 AM', '24:00', 'saturDay')) |
| 54 | +print(add_time('11:59 PM', '24:05', 'Wednesday')) |
| 55 | +print(add_time('8:16 PM', '466:02', 'tuesday')) |
0 commit comments