-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_calculator.py
47 lines (40 loc) · 1.55 KB
/
time_calculator.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
def add_time(start, duration, day=None):
# days in a week
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
# initial time and the duration to be added in hours and minutes
init_time = list(map(int, start[:-3].split(':')))
duration = list(map(int, duration.split(':')))
# adding the minutes to the initial time
init_time[1] += duration[1]
count = 0 # to store the extra number of hours (60 minutes)
while init_time[1] > 60:
count += 1
init_time[1] -= 60
# adding the hours to the initial time
init_time[0] += count + duration[0]
count = 0 # to store the number of days
while init_time[0] >= 12:
init_time[0] -= 12
if 'PM' in start:
# increment the count for days if time crosses the PM mark
count += 1
# and replace PM with AM
start = start.replace('PM', 'AM')
elif 'AM' in start:
# else replace AM with PM
start = start.replace('AM', 'PM')
init_time[0] = '12' if init_time[0] == 0 else str(init_time[0])
init_time[1] = str(init_time[1]).rjust(2, '0')
new_time = ':'.join(init_time) + start[-3:]
if day is not None:
day = day.lower().capitalize()
days = days[days.index(day):] + days[:days.index(day)]
n = count
while n > 7:
n -= 7
new_time += ', ' + days[n]
if count == 1:
new_time += ' (next day)'
elif count > 1:
new_time += ' (' + str(count) + ' days later)'
return new_time