Search This Blog

Tuesday, July 15, 2025

Time Calculator Project (Project 2) - Scientific Computing with Python (freeCodeCamp.org)

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

  1. ABSTRACT

  2. INTRODUCTION

  3. PYTHON CODE AND PERSONAL SOLUTION

  4. OUTPUT

  5. 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):

def add_time(start, duration, day=None): # Separate strings and prepare numbers for calculation start_time, period = start.split() start_hour, start_minute = map(int, start_time.split(':')) added_hour, added_minute = map(int, duration.split(':')) # Define target hour and minute printed out # Convert start_hour to 24 hour clock format for easier calculating if period == "AM": if start_hour == 12: start_hour = 0 elif period == "PM": if start_hour != 12: start_hour += 12 # Calculate desired hour, minute, and days for output total_minutes = start_minute + added_minute total_hours = start_hour + added_hour + (total_minutes // 60) minute = total_minutes % 60 hour = total_hours % 24 n_day = total_hours // 24 # Define new periods printed out if hour < 12: final_period = "AM" elif hour >= 12: final_period = "PM" # Convert into 12 hour clock format if needed if hour > 12: hour -= 12 elif hour == 0: hour = 12 # Step 2 - Identify day in the week # Make a list of days in a week days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] # Refine format of minute unit if minute < 10: time = f"{hour}:0{minute} {final_period}" else: time = f"{hour}:{minute} {final_period}" # Define final time if day: # day.index(...) is the index of current week day within the list of days in a week start_day_index = (days.index(day.capitalize())) end_day_index = (start_day_index + n_day) % 7 end_day = days[end_day_index] # Using .capitalize() function to uppercase only the first letter of the whole word time += f", {end_day}" if n_day == 1: time += " (next day)" elif n_day > 1: time += f" ({n_day} days later)" return time print(add_time('3:30 PM', '2:12')) # 1 ➜ 5:42 PM print(add_time('11:55 AM', '3:12')) # 2 ➜ 3:07 PM print(add_time('10:10 PM', '3:30')) # 3 ➜ 1:40 AM (next day) print(add_time('11:59 AM', '12:01')) # 4 ➜ 12:00 PM *(AM→PM boundary test)* print(add_time('2:59 AM', '24:00')) # 5 ➜ 2:59 AM (next day) print(add_time('11:59 PM', '24:05')) # 6 ➜ 12:04 AM (2 days later) print(add_time('8:16 PM', '466:02')) # 7 ➜ 6:18 AM (20 days later) print(add_time('3:00 PM', '0:00')) # 8 ➜ 3:00 PM *(no change)* print(add_time('3:30 PM', '2:12', 'Monday')) # 9 ➜ 5:42 PM, Monday print(add_time('2:59 AM', '24:00', 'saturDay')) #10 ➜ 2:59 AM, Sunday (next day) print(add_time('11:59 PM', '24:05', 'Wednesday')) #11 ➜ 12:04 AM, Friday (2 days later) print(add_time('8:16 PM', '466:02', 'tuesday')) #12 ➜ 6:18 AM, Monday (20 days later)


3 OUTPUT

Below in FIGURE 3 are some example calls to the add_time() function and their outputs:


FIGURE 1. The output of add_time() after 12 tests were successfully passed.

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.


REFERENCE

freeCodeCamp (n.d.) Build a Time Calculator Project. Available at: https://www.freecodecamp.org/learn/scientific-computing-with-python/build-a-time-calculator-project/build-a-time-calc (Accessed: 15 July 2025).


APPENDICES

Here below are pages of how I did the ideation step before starting coding. image image

No comments:

Post a Comment