forked from sshreya07/HacktoberFest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.py
75 lines (67 loc) · 2.14 KB
/
time.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
def add_time(start, duration, day=None):
# Maintaining Days in a week
day_map = {
"Saturday": 0,
"Sunday": 1,
"Monday": 2,
"Tuesday": 3,
"Wednesday": 4,
"Thursday": 5,
"Friday": 6
}
# Getting useful data from start string
timely, midday = start.split()
hour, minutes = timely.split(':')
hour = int(hour)
minutes = int(minutes)
# Making the clock into 24 hour format
if midday == "PM":
hour += 12
# Getting data from duration
duration_hour, duration_minutes = duration.split(':')
duration_hour = int(duration_hour)
duration_minutes = int(duration_minutes)
# Calculating total hours, minutes
total_minutes = minutes + duration_minutes
ans_minutes = total_minutes % 60
extra_hours = total_minutes // 60
total_hour = hour + duration_hour + extra_hours
# final hours as per 12 Hour clock
ans_hour = (total_hour % 24) % 12
# Edge case
if ans_hour == 0:
ans_hour = 12
ans_hour = str(ans_hour)
# total days 24 hr 1 day
total_day = (total_hour // 24)
# deciding mid day (AM/PM)
ans_midday = ""
if (total_hour % 24) <= 11:
ans_midday = "AM"
else:
ans_midday = "PM"
# Handling single digit minutes case
if ans_minutes <= 9:
ans_minutes = '0' + str(ans_minutes)
else:
ans_minutes = str(ans_minutes)
# returning logic
time_stamp = ans_hour + ":" + ans_minutes + ' ' + ans_midday
if day == None:
if total_day == 0:
return time_stamp
if total_day == 1:
return time_stamp + ' (next day)'
return time_stamp + ' (' + str(total_day) + ' days later)'
else:
ans_day = (day_map[day.lower().capitalize()] + total_day) % 7
for i, j in day_map.items():
if j == ans_day:
ans_day = i
break
if total_day == 0:
return time_stamp + ', ' + ans_day
if total_day == 1:
return time_stamp + ', ' + ans_day + ' (next day)'
return time_stamp + ', ' + ans_day + ' (' + str(
total_day) + ' days later)'