├── README.md ├── main.py ├── test_module.py └── time_calculator.py /README.md: -------------------------------------------------------------------------------- 1 | ### Live: https://replit.com/@hawshemi/time-calculator-freecodecamp 2 | 3 | # Project Description 4 | 5 | ### Assignment 6 | 7 | Write a function named `add_time` that takes in two required parameters and one optional parameter: 8 | * a start time in the 12-hour clock format (ending in AM or PM) 9 | * a duration time that indicates the number of hours and minutes 10 | * (optional) a starting day of the week, case insensitive 11 | 12 | The function should add the duration time to the start time and return the result. 13 | 14 | If the result will be the next day, it should show `(next day)` after the time. If the result will be more than one day later, it should show `(n days later)` after the time, where "n" is the number of days later. 15 | 16 | If the function is given the optional starting day of the week parameter, then the output should display the day of the week of the result. The day of the week in the output should appear after the time and before the number of days later. 17 | 18 | Below are some examples of different cases the function should handle. Pay close attention to the spacing and punctuation of the results. 19 | ```py 20 | add_time("3:00 PM", "3:10") 21 | # Returns: 6:10 PM 22 | 23 | add_time("11:30 AM", "2:32", "Monday") 24 | # Returns: 2:02 PM, Monday 25 | 26 | add_time("11:43 AM", "00:20") 27 | # Returns: 12:03 PM 28 | 29 | add_time("10:10 PM", "3:30") 30 | # Returns: 1:40 AM (next day) 31 | 32 | add_time("11:43 PM", "24:20", "tueSday") 33 | # Returns: 12:03 AM, Thursday (2 days later) 34 | 35 | add_time("6:30 PM", "205:12") 36 | # Returns: 7:42 AM (9 days later) 37 | ``` 38 | 39 | Do not import any Python libraries. Assume that the start times are valid times. The minutes in the duration time will be a whole number less than 60, but the hour can be any whole number. 40 | 41 |
42 | 43 | [FreeCodeCamp](https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator) 44 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # This entrypoint file to be used in development. Start by reading README.md 2 | from time_calculator import add_time 3 | from unittest import main 4 | 5 | 6 | print(add_time("11:06 PM", "2:02")) 7 | 8 | 9 | # Run unit tests automatically 10 | main(module='test_module', exit=False) -------------------------------------------------------------------------------- /test_module.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from time_calculator import add_time 3 | 4 | 5 | class UnitTests(unittest.TestCase): 6 | 7 | def test_same_period(self): 8 | actual = add_time("3:30 PM", "2:12") 9 | expected = "5:42 PM" 10 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12" to return "5:42 PM"') 11 | 12 | def test_different_period(self): 13 | actual = add_time("11:55 AM", "3:12") 14 | expected = "3:07 PM" 15 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:55 AM", "3:12" to return "3:07 PM"') 16 | 17 | def test_next_day(self): 18 | actual = add_time("9:15 PM", "5:30") 19 | expected = "2:45 AM (next day)" 20 | self.assertEqual(actual, expected, 'Expected time to end with "(next day)" when it is the next day.') 21 | 22 | def test_period_change_at_twelve(self): 23 | actual = add_time("11:40 AM", "0:25") 24 | expected = "12:05 PM" 25 | self.assertEqual(actual, expected, 'Expected period to change from AM to PM at 12:00') 26 | 27 | def test_twenty_four(self): 28 | actual = add_time("2:59 AM", "24:00") 29 | expected = "2:59 AM (next day)" 30 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00" to return "2:59 AM"') 31 | 32 | def test_two_days_later(self): 33 | actual = add_time("11:59 PM", "24:05") 34 | expected = "12:04 AM (2 days later)" 35 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05" to return "12:04 AM (2 days later)"') 36 | 37 | def test_high_duration(self): 38 | actual = add_time("8:16 PM", "466:02") 39 | expected = "6:18 AM (20 days later)" 40 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02" to return "6:18 AM (20 days later)"') 41 | 42 | def test_no_change(self): 43 | actual = add_time("5:01 AM", "0:00") 44 | expected = "5:01 AM" 45 | self.assertEqual(actual, expected, 'Expected adding 0:00 to return initial time.') 46 | 47 | def test_same_period_with_day(self): 48 | actual = add_time("3:30 PM", "2:12", "Monday") 49 | expected = "5:42 PM, Monday" 50 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12", "Monday" to return "5:42 PM, Monday"') 51 | 52 | def test_twenty_four_with_day(self): 53 | actual = add_time("2:59 AM", "24:00", "saturDay") 54 | expected = "2:59 AM, Sunday (next day)" 55 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"') 56 | 57 | def test_two_days_later_with_day(self): 58 | actual = add_time("11:59 PM", "24:05", "Wednesday") 59 | expected = "12:04 AM, Friday (2 days later)" 60 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "Friday" to return "12:04 AM, Friday (2 days later)"') 61 | 62 | def test_high_duration_with_day(self): 63 | actual = add_time("8:16 PM", "466:02", "tuesday") 64 | expected = "6:18 AM, Monday (20 days later)" 65 | self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"') 66 | 67 | if __name__ == "__main__": 68 | unittest.main() -------------------------------------------------------------------------------- /time_calculator.py: -------------------------------------------------------------------------------- 1 | def add_time(start, duration, starting_day=""): 2 | # Separate the start into hours and minutes 3 | start_time, end = start.split() 4 | start_hour, start_minute = map(int, start_time.split(':')) 5 | 6 | # Calculate 24-hour clock format 7 | if end == "PM": 8 | start_hour += 12 9 | 10 | # Separate the duration into hours and minutes 11 | dur_hour, dur_minute = map(int, duration.split(':')) 12 | 13 | # Add hours and minutes 14 | new_hour = start_hour + dur_hour 15 | new_minutes = start_minute + dur_minute 16 | 17 | if new_minutes >= 60: 18 | hours_add = new_minutes // 60 19 | new_minutes -= hours_add * 60 20 | new_hour += hours_add 21 | 22 | days_add = 0 23 | if new_hour >= 24: 24 | days_add = new_hour // 24 25 | new_hour %= 24 26 | 27 | # Find AM and PM 28 | if new_hour >= 12: 29 | end = "PM" 30 | if new_hour > 12: 31 | new_hour -= 12 32 | else: 33 | end = "AM" 34 | if new_hour == 0: 35 | new_hour = 12 36 | 37 | # Handle days later 38 | if days_add > 0: 39 | if days_add == 1: 40 | days_later = " (next day)" 41 | else: 42 | days_later = f" ({days_add} days later)" 43 | else: 44 | days_later = "" 45 | 46 | # Define the week days 47 | week_days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") 48 | 49 | if starting_day: 50 | weeks = days_add // 7 51 | i = (week_days.index(starting_day.lower().capitalize()) + days_add - 7 * weeks) % 7 52 | day = f", {week_days[i]}" 53 | else: 54 | day = "" 55 | 56 | new_time = f"{new_hour}:{new_minutes:02d} {end}{day}{days_later}" 57 | 58 | return new_time 59 | --------------------------------------------------------------------------------