Simplify Time Conversion: Seconds to Hours, Minutes, and Seconds with Python
Understanding how to work with time conversions is a useful skill when developing various applications. While Python provides built-in modules that handle these conversions easily, it’s a great learning opportunity to first build a custom function yourself. In this tutorial, we’ll walk through creating a Python function to convert time in seconds into hours, minutes, and seconds.
Afterward, we’ll also explore some of Python’s built-in tools for achieving the same results.
Custom Python Function for Time Conversion
Before diving into any existing solutions, let’s try solving the problem manually. To convert a given time in seconds into hours, minutes, and seconds, we can break it down into a few logical steps:
1. Handling Large Numbers of Seconds
If the input seconds exceed the number of seconds in a day, we can calculate how much time remains after dividing by the number of seconds in a day:
seconds = seconds % (24 * 3600)
The modulo operation ensures we only work with the time under 24 hours.
2. Extracting Hours
To get the number of hours, we divide the total seconds by the number of seconds in an hour (3600). Using floor division (//
) ensures we get the integer part of the quotient:
hours = seconds // 3600
3. Calculating Minutes
After calculating hours, we use the modulo operator again to get the remaining seconds. Then, we repeat the process for minutes by dividing the remaining seconds by 60:
seconds %= 3600
minutes = seconds // 60
4. Getting Seconds
Finally, we apply the modulo operation to find the remaining seconds:
seconds %= 60
Let’s compile all of this into a Python function:
def convert_to_time_format(sec):
sec = sec % (24 * 3600) # Restricting to a 24-hour format
hours = sec // 3600
sec %= 3600
minutes = sec // 60
sec %= 60
return "{:02d}:{:02d}:{:02d}".format(hours, minutes, sec)
n = 10000
print("Time in hours, minutes, and seconds:", convert_to_time_format(n))
Running the above code will give the following output:
Time in hours, minutes, and seconds: 02:46:40
Using Python’s time
Module
While writing custom functions enhances understanding, Python’s time
module can achieve the same results with fewer lines of code. This module allows you to format time based on an epoch (the starting point, often defined as January 1, 1970).
Here’s how to use the time
module to convert seconds into hours, minutes, and seconds:
import time
n = 10000
formatted_time = time.strftime("%H:%M:%S", time.gmtime(n))
print("Time in hours, minutes, and seconds:", formatted_time)
The output will be:
Time in hours, minutes, and seconds: 02:46:40
This method is straightforward, converting the seconds directly into a structured format using Unix time.
Additional Features with the time
Module
The time
module allows you to display additional information such as the day of the week and the month. For example:
n = 100000000000
formatted_time = time.strftime("Day: %a, Time: %H:%M:%S, Month: %b", time.gmtime(n))
print("Formatted Time:", formatted_time)
Output:
Formatted Time: Day: Wed, Time: 09:46:40, Month: Nov
Using Python’s datetime
Module
The datetime
module offers another powerful way to work with time. You can use timedelta
to convert seconds into a readable format that includes days, hours, minutes, and seconds:
import datetime
n = 10000000
formatted_time = str(datetime.timedelta(seconds=n))
print("Time in days, hours, minutes, and seconds:", formatted_time)
This code will output:
Time in days, hours, minutes, and seconds: 115 days, 17:46:40
Conclusion
In this tutorial, we explored three different methods for converting seconds into a readable time format. First, we built our own function, which gave us a deeper understanding of the process. Then, we explored Python’s built-in time
and datetime
modules, which offer convenient, ready-made solutions for handling time.
Whether you prefer building custom functions or leveraging Python’s existing libraries, having the flexibility to work with time in various formats is a valuable skill in software development.