-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeCalculator.py
80 lines (77 loc) · 2.67 KB
/
TimeCalculator.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
def add_time(start, duration, weekday=None):
week_dict = {
1: 'monday',
2: 'tuesday',
3: 'wednesday',
4: 'thursday',
5: 'friday',
6: 'saturday',
7: 'sunday'
}
day_counter = 0
start_time_elements = start.split()
period = start_time_elements[1]
start_time = start_time_elements[0].split(':')
start_time_hour = int(start_time[0])
start_time_minutes = int(start_time[1])
duration_elements = duration.split(':')
duration_hour = int(duration_elements[0])
duration_minutes = int(duration_elements[1])
if period == 'PM':
start_time_hour = start_time_hour + 12
if duration_hour > 24:
day_counter = duration_hour // 24
duration_hour = duration_hour % 24
new_hour = start_time_hour + duration_hour
new_minutes = start_time_minutes + duration_minutes
if new_minutes > 60:
temp_count = new_minutes // 60
new_minutes = new_minutes % 60
new_hour = new_hour + temp_count
if new_hour > 24:
temp_count = new_hour // 24
new_hour = new_hour % 24
day_counter = day_counter + temp_count
if new_hour > 11:
new_hour = new_hour - 12
period = 'PM'
else:
period = 'AM'
if weekday is not None:
weekday = weekday.lower()
for number, day in week_dict.items():
if weekday == day:
current_day_number = number
print(current_day_number)
if day_counter > 7:
temp_count = day_counter % 7
desired_day_number = current_day_number + temp_count
if desired_day_number > 7:
desired_day_number = desired_day_number % 7
else:
desired_day_number = current_day_number + day_counter
weekday = week_dict[desired_day_number]
if new_hour == 0:
new_hour = 12
new_hour = str(new_hour)
new_minutes = str(new_minutes)
if int(new_minutes) < 10:
new_minutes = '0' + new_minutes
if day_counter == 0:
day_counter = ""
elif day_counter == 1:
day_counter = " (next day)"
else:
day_counter = " (" + str(day_counter) + " days later)"
if weekday is None:
new_time = new_hour + ':' + new_minutes + ' ' + period + day_counter
else:
weekday = weekday.capitalize()
weekday = ", " + weekday
new_time = new_hour + ':' + new_minutes + ' ' + period + weekday + day_counter
return new_time
print(add_time("11:06 PM", "2:02"))
print(add_time("8:16 PM", "466:02", "tuesday"))
print(add_time("11:59 PM", "24:05", "Wednesday"))
print(add_time("11:40 AM", "0:25"))
print(add_time("8:16 PM", "466:02", "tuesday"))