Transform [hh:mm] into float (ex: 01:30 ==> 1.5) in python

Asked

Viewed 243 times

-2

I have a data list in [hh:mm] format and I want to convert it to float.

ex:

hours 01:00 01:35 00:45 00:20

I would like to transform these varoles in 1.0, 1.5833, 0.75, 0.3333 to achieve 3.67 as an answer, and finally show 03:40 as a final answer.

2 answers

2

If you want to add up these values, then in fact they are not "hours", but "durations". They are different concepts:

  • one timetable represents a specific time of day. Ex: the meeting will be at two o'clock in the afternoon.
  • one duration represents a amount of time. Ex: the meeting lasted two hours (I didn’t say what hours it started or ended, just how long it lasted)

What can be confusing is that they both use the same words (hours, minutes, seconds) and can even be written the same way ("10:00" can be either 10 am or a stopwatch showing that 10 hours have passed).

So, what you have there are representations of different durations, because it makes no sense to "add times": 2 pm + 3 am is equal to what? It doesn’t make sense - but adding a duration of 2 hours to another 3 hours makes sense (one meeting lasted 2 hours and another lasted 3 hours, the total is 5 hours long).


That being said, in Python durations are represented by timedelta. Then it would be enough to create the timedelta from these strings, add them and get the final result:

from datetime import timedelta

duracoes = ['01:00', '01:35', '00:45', '00:20']
total = timedelta()

for d in duracoes:
    horas, minutos = map(int, d.split(':'))
    total += timedelta(hours = horas, minutes = minutos)

total_minutes = int(total.total_seconds()) // 60
print(f'{total_minutes // 60:02}:{total_minutes % 60:02}')

And to format, I used the format 02 so that the number is printed with the zero on the left if necessary (if you do only print(total), the result will be 3:40:00).


Notice that you nay needs to turn "01:35" into 1.5833, to then add it all up and turn it back into "3:40," you can add the timedelta's directly.

But of course, if you want to, you can:

duracoes = ['01:00', '01:35', '00:45', '00:20']
total = 0
for d in duracoes:
    h, m = map(int, d.split(':'))
    total += h + (m / 60)

horas = int(total)
minutos = int(60 * (total - horas))
print(f'{horas:02}:{minutos:02}')

Depending on the case, maybe give difference with the previous solution because of some rounding, but the basic idea is this (anyway, I still prefer to use timedelta, who already takes care of the details of how to add up times, without you having to worry about it).


Another way to do it is to create a function that transforms the string into timedelta, and use sum along with map:

from datetime import timedelta

def to_timedelta(duracao):
    horas, minutos = map(int, duracao.split(':'))
    return timedelta(hours = horas, minutes = minutos)

duracoes = ['01:00', '01:35', '00:45', '00:20']
# transforma todas as strings em timedelta e soma
total = sum(map(to_timedelta, duracoes), timedelta())

total_minutes = int(total.total_seconds()) // 60
print(f'{total_minutes // 60:02}:{total_minutes % 60:02}')

1

Whereas the time is in a string, you could first separate the hours from the minutes:

entrada = '01:35'
hora, minuto = entrada.split(':')

Then convert the minutes to the range of 0 to 1 and then add in the hours:

resultado = int(hora) + (int(minuto) / 60)
print(resultado) # 1.5833...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.