Time Calculator

                          Time Calculator

  • Start by importing the project on Replit.
  • Next, you will see a .replit window.
  • Select Use run command and click the Done button.

Write a function named add_time that takes in two required parameters and one optional parameter:

  • a start time in the 12-hour clock format (ending in AM or PM)
  • a duration time that indicates the number of hours and minutes
  • (optional) a starting day of the week, case insensitive

The function should add the duration time to the start time and return the result.

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.

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.

Below are some examples of different cases the function should handle. Pay close attention to the spacing and punctuation of the results.

add_time("3:00 PM", "3:10")
# Returns: 6:10 PM

add_time("11:30 AM", "2:32", "Monday")
# Returns: 2:02 PM, Monday

add_time("11:43 AM", "00:20")
# Returns: 12:03 PM

add_time("10:10 PM", "3:30")
# Returns: 1:40 AM (next day)

add_time("11:43 PM", "24:20", "tueSday")
# Returns: 12:03 AM, Thursday (2 days later)

add_time("6:30 PM", "205:12")
# Returns: 7:42 AM (9 days later)

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.

Development

Write your code in time_calculator.py. For development, you can use main.py to test your time_calculator() function. Click the "run" button and main.py will run.

Testing

The unit tests for this project are in test_module.py. We imported the tests from test_module.py to main.py for your convenience. The tests will run automatically whenever you hit the "run" button.

Solution:

def add_time(start, duration, day=None):

    # Extract the start time and period (AM/PM)

    start_time, period = start.split()

    start_hour, start_minute = map(int, start_time.split(':'))

    start_hour = start_hour % 12 # Convert to 12-hour format


    # Extract the duration time

    duration_hour, duration_minute = map(int, duration.split(':'))


    # Calculate the total minutes

    total_minutes = start_hour * 60 + start_minute

    total_minutes += duration_hour * 60 + duration_minute


    # Calculate the new time and period

    new_hour = (total_minutes // 60) % 12

    new_minute = total_minutes % 60

    new_period = period


    # Calculate the number of days later

    days_later = total_minutes // (24 * 60)


    # Determine if it's the next day or more days later

    if days_later == 0:

        if total_minutes // (12 * 60) % 2 == 1:

            new_period = 'PM' if period == 'AM' else 'AM'

    elif days_later == 1:

        new_period = 'PM' if period == 'AM' else 'AM'

        new_hour += 12

    else:

        new_period = 'PM' if period == 'AM' else 'AM'

        new_hour += 12 * (days_later % 2)


    # Normalize the hour to 12-hour format

    if new_hour == 0:

        new_hour = 12


    # Determine the day of the week

    if day is not None:

        day = day.capitalize()

        days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

        start_index = days_of_week.index(day)

        new_day_index = (start_index + days_later) % 7

        new_day = days_of_week[new_day_index]

        new_time = f'{new_hour:02}:{new_minute:02} {new_period}, {new_day}'

    else:

        new_time = f'{new_hour:02}:{new_minute:02} {new_period}'


    # Add "next day" or "n days later" if applicable

    if days_later == 1:

        new_time += ' (next day)'

    elif days_later > 1:

        new_time += f' ({days_later} days later)'


    return new_time



Comments

Our Popular Blogs

TECHNOLOGICAL ISSUES

VK EDUCATION

"Unlocking the Power of Data Structures: A Comprehensive Guide to Types and Techniques"

"Understanding Bubble Sort: A Simple but Inefficient Sorting Algorithm"

"The Ultimate Cooling Solutions: Unleashing the Power of Your PC"