-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecalculator.py
More file actions
60 lines (44 loc) · 1.56 KB
/
timecalculator.py
File metadata and controls
60 lines (44 loc) · 1.56 KB
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
# time_calculator.py
def add_time(start, duration, day=None):
start_time, period = start.split()
start_hour, start_minute = map(int, start_time.split(':'))
duration_hour, duration_minute = map(int, duration.split(':'))
if period == 'PM':
start_hour += 12
total_minutes = start_hour * 60 + start_minute + duration_hour * 60 + duration_minute
new_hour = total_minutes // 60 % 24
new_minute = total_minutes % 60
if new_hour < 12:
new_period = 'AM'
else:
new_period = 'PM'
new_hour %= 12
if new_hour == 0:
new_hour = 12
days_later = total_minutes // (24 * 60)
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
if day:
day = day.capitalize()
start_day_index = days_of_week.index(day)
new_day_index = (start_day_index + days_later) % 7
new_day = days_of_week[new_day_index]
result = f"{new_hour}:{new_minute:02d} {new_period}, {new_day}"
else:
result = f"{new_hour}:{new_minute:02d} {new_period}"
if days_later == 1:
result += " (next day)"
elif days_later > 1:
result += f" ({days_later} days later)"
return result
# main.py
from time_calculator import add_time
from unittest import main
print(add_time("11:06 PM", "2:02"))
main(module='test_module', exit=False)
# test_module.py
import unittest
from time_calculator import add_time
class UnitTests(unittest.TestCase):
# ... (rest of the test cases)
if __name__ == "__main__":
unittest.main()