Python program to convert seconds into hours, minutes and seconds (original) (raw)

Given an integer **n (in seconds), convert it into hours, minutes and seconds.

**Examples:

**Input : 12345
**Output : 3:25:45

**Input : 3600
**Output : 1:00:00

Let's look at different ways of doing it in Python.

1. Naive Approach

This approach uses simple mathematical calculations to get the hours, minutes, and seconds.

Python `

def convert(seconds): seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60

return "%d:%02d:%02d" % (hour, minutes, seconds)

Driver program

n = 12345 print(convert(n))

`

**Explanation:

2. Using divmod()

Using the **divmod() function allows for a more efficient solution by performing the division and modulus operations in one step.

Python `

def convert(seconds): min, sec = divmod(seconds, 60) hour, min = divmod(min, 60) return '%d:%02d:%02d' % (hour, min, sec)

Driver program

n = 12345 print(convert(n))

`

**Explanation:

3. Using timedelta (from datetime module)

The **datetime module provides a timedelta object which can represent durations, making it easy to convert seconds into a human-readable format.

Python `

import datetime

def convert(n): return str(datetime.timedelta(seconds = n))

Driver program

n = 12345 print(convert(n))

`

**Explanation: timedelta(seconds=n) function automatically converts the given seconds into the format hours:minutes:seconds.

4. Using time.strftime()

The time.strftime() function gives more control over formatting, and time.gmtime() is used to convert seconds into a special tuple format that strftime() accepts.

Python `

import time

def convert(seconds): return time.strftime("%H:%M:%S", time.gmtime(seconds))

Driver program

n = 12345 print(convert(n))

`

**Explanation:

5. Using dateutil.relativedelta()

The dateutil library provides a convenient way to convert seconds into hours, minutes, and seconds using the relativedelta function.

We need to first install the dateutil library using command:

pip install python-dateutil

The dateutil library provides a convenient way to convert seconds into hours, minutes, and seconds using the relativedelta function. Here is an example of how this can be done:

Python `

from dateutil import relativedelta

def convert(n): rd = relativedelta.relativedelta(seconds=n) return "{}:{:02d}:{:02d}".format(rd.hours, rd.minutes, rd.seconds)

Driver program

n = 12345 print(convert(n)) #This code is contributed by Edula Vinay Kumar Reddy

`

**Explanation:

6. Using a Dictionary for Calculations

This approach uses a dictionary to store the number of seconds in each unit of time (hours, minutes, seconds). It iterates over the dictionary, calculating the number of each unit and formatting the result.

Python `

def convert_seconds(seconds): units = {"hours": 3600, "minutes": 60, "seconds": 1} values = [] for unit, value in units.items(): count = seconds // value seconds -= count * value values.append(count) return f"{values[0]:02d}:{values[1]:02d}:{values[2]:02d}"

Driver program

seconds = 12345 print(convert_seconds(seconds))

`