Skip to content Skip to sidebar Skip to footer

Converting Decimal Time (hh.hhh) Into Hh:mm:ss In Python

I have hours in this format, 72.345, 72.629, 71.327, ... as a result of performing a calculation in Python. It seems that the simplest way to convert these into HH:MM:SS format is

Solution 1:

You do not have to use datetime. You can easily compute hours, minutes and seconds from your decimal time.

You should also notice that you can use string formatting which is really easier to use than string concatenation.

time = 72.345

hours = int(time)
minutes = (time*60) % 60
seconds = (time*3600) % 60print("%d:%02d.%02d" % (hours, minutes, seconds))
>> 72:20:42

Solution 2:

If you want it to be a little easier to look at, you can always put in:

Time = 72.345

Hours = Time
Minutes = 60 * (Hours % 1)
Seconds = 60 * (Minutes % 1)

print("%d:%02d:%02d" % (Hours, Minutes, Seconds))

Putting %d in the string will cut off any decimals for you.

Solution 3:

I am copying the best answer here to the question How do I convert seconds to hours, minutes and seconds?, because to my eyes, the following use of divmod and tuple assignment is so pleasing.

hours = 72.345
seconds = hours * 3600
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print"%d:%02d:%02d" % (h, m, s)

Solution 4:

A functional approach:

First define a helper method, frac, which is somehow the same with math.modf:

>>>deffrac(n):...    i = int(n)...    f = round((n - int(n)), 4)...return (i, f)...>>>frac(53.45)
(53, 0.45) # A tuple

Then a function to format the hours decimal:

>>>deffrmt(hour): # You can rewrite it with 'while' if you wish...    hours, _min = frac(hour)...    minutes, _sec = frac(_min*60)...    seconds, _msec = frac(_sec*60)...return"%s:%s:%s"%(hours, minutes, seconds)...>>>frmt(72.345)
'72:20:42'
>>>l = [72.345, 72.629, 71.327]>>>map(frmt, l)
['72:20:42', '72:37:44', '71:19:37']

Solution 5:

In this function you get a straight-forward answer with just 2 lines, which are very readable since they hide the math operations.

from dateutil.relativedelta import relativedelta
from typing importTuple, Uniondefdecimal_hours_to_HMS(hours: float, as_tuple: bool = False) -> Union[str, Tuple]:
    """ 
    Converts a float number representing hours to a readable string of the form "HH:MM:SS"
    - as_tuple: set it to True if instead of a string, you want a tuple as (hours, minutes, seconds)
    
    Example:
    --------
    >> decimal_hours_to_HMS(hours=72.345)
    '72:20:42'
    >>> decimal_hours_to_HMS(hours=72.345, as_tuple=True)
    (72, 20, 42)
    """
    rd = relativedelta(hours=hours).normalized()
    str_repr = f"{rd.days * 24 + rd.hours}:{rd.minutes}:{rd.seconds}"return str_repr ifnot as_tuple elsetuple(map(int, str_repr.split(":")))
  1. The first line is like a datetime.timedelta that accepts decimal values, which is normalized to translate the info to integer values.
  2. The second line extracts the total hours, the minutes and the seconds, in the string format you wanted.

As a bonus in the return, it lets you choose, with the parameter as_tuple, whether or not you want the output to be a tuple with the values of the hours, minutes and seconds. This functionality could come handy in many situations.

Post a Comment for "Converting Decimal Time (hh.hhh) Into Hh:mm:ss In Python"