Reputation: 2149
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?
Upvotes: 0
Views: 51
Reputation: 2699
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()
Upvotes: 1