0

Having such string 12:65:84 and told that it represents time in h:m:s AND values there can be not correct, e.g. 65 minutes that should be translated to 1 hour and 5 minutes

I need to reduce these numbers to total amount of seconds.

Naive solution will be:

time_string = '12:65:84'
hours, minutes, seconds = [int(i) for i in time_string.split(':')
total_seconds = hours * 60 * 60 + minutes * 60 + seconds   

Question: How it can be done better, ideally without using any import, maybe with some combination of map, reduce and their friends?

micgeronimo
  • 2,069
  • 5
  • 23
  • 44

1 Answers1

1

You can also use timedelta for the same as:

s='12:65:84'
h,m,s=[int(i) for i in s.split(':')]
t=timedelta(hours=h, seconds=s, minutes=m)
res=t.total_seconds()
shiva
  • 2,535
  • 2
  • 18
  • 32