ABSTRACT
This project introduces a Python program designed to calculate the future time after adding a specified duration to a starting time. The program accepts inputs such as a starting time in 12-hour format, a duration in hours and minutes, and optionally a starting day of the week. It then calculates the resulting time, correctly handling AM/PM conversions, day changes, and optionally returning the day of the week. The project aims to improve understanding of string parsing, arithmetic operations, conditional logic, and modular programming in Python. This task is suitable for beginner to intermediate programmers to strengthen practical coding skills.
CONTENTS
-
ABSTRACT
-
INTRODUCTION
-
PYTHON CODE AND PERSONAL SOLUTION
-
OUTPUT
-
CONCLUSION AND DISCUSSION
1 INTRODUCTION
Calculating time after adding a duration to a starting time is a common problem encountered in daily life, such as scheduling or time management. This project focuses on creating a Python function that performs this calculation accurately, considering the 12-hour clock format and days of the week.
The function processes inputs including a start time with AM/PM, a duration in hours and minutes, and optionally a day of the week. It then returns the new time after adding the duration, along with how many days later it is, and the updated day if provided. Key programming concepts like string manipulation, arithmetic with carryover, conditional statements, and modular arithmetic are exercised in this project.
2 PYTHON CODE AND PERSONAL SOLUTION
The main function is called add_time(), designed to handle the inputs and return a formatted string representing the new time.
Key steps in the solution include:
-
Parsing the start time into hours, minutes, and AM/PM.
-
Converting the 12-hour time into 24-hour format for easier calculations.
-
Parsing the duration into hours and minutes.
-
Adding duration minutes and handling carryover to hours.
-
Adding duration hours to the starting hour, counting how many days pass.
-
Converting the 24-hour result back into 12-hour format.
-
Calculating the new day of the week if the day was provided.
-
Constructing the output string with time, optional day, and information about days passed.
Below is a simplified version of the main function implementation. It can also be accessed via my GitHub repository at TriPham2006/Time_Calculator: Python Project (problem provided by freeCodeCamp.org - from Scientific Computing with Python course):
3 OUTPUT
Below in FIGURE 3 are some example calls to the add_time() function and their outputs:
These outputs show the time correctly updated with duration added, the period adjusted between AM and PM, and days passed clearly indicated. The function also properly calculates the new day when provided.
4 CONCLUSION AND DISCUSSION
This Time Calculator project successfully demonstrates how to handle time calculations programmatically with Python. The challenge primarily involves parsing and converting between 12-hour and 24-hour formats, performing arithmetic with carryover, and managing days passed.
Through this project, key Python concepts such as string manipulation, conditionals, modular arithmetic, and optional parameters were exercised. The function also improves user interaction by handling different input cases and formatting the output clearly.
One difficulty was ensuring the program correctly handles edge cases like noon, midnight, and multi-day durations. Careful attention to the conversion between time formats and day calculation logic was necessary to achieve the correct results.
Overall, this project is a practical exercise suitable for beginner programmers to deepen their understanding of Python and time-related problem solving. It also forms a useful base for developing more complex scheduling or calendar applications in the future.