time-calculator/time_calculator.py
2023-04-19 16:06:10 +10:00

31 lines
1.2 KiB
Python

def add_time(start: str, duration: str, day: str=""):
days: list[str] = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
day = day.casefold()
time, am_pm = start.split(" ")
time_hour, time_minute = [int(part) for part in time.split(":")]
add_hours, add_minutes = [int(part) for part in duration.split(":")]
new_time_minute = (time_minute + add_minutes)%60
new_time_hour = (time_hour + add_hours + int((time_minute + add_minutes)/60))%12
if new_time_hour == 0:
new_time_hour = 12
am_pm_revolutions = int((time_hour + add_hours + int((time_minute + add_minutes)/60))/12)
days_past = int(am_pm_revolutions/2)
new_am_pm = am_pm
if (am_pm_revolutions%2):
days_past += 1 if am_pm == "PM" else 0
new_am_pm = "AM" if am_pm == "PM" else "PM"
if days_past == 1:
days_str = " (next day)"
elif days_past > 1:
days_str = f" ({days_past} days later)"
else:
days_str = ''
if day:
new_day = f", {days[int((days.index(day)+days_past)%len(days))].capitalize()}"
else:
new_day = ""
new_time = f'{new_time_hour}:{str(new_time_minute).zfill(2)} {new_am_pm}{new_day}{days_str}'
return new_time