Convert Time to a Fraction
If you come across the need to store time as a float in ruby (I won’t judge you), you can use this bit of code. It assumes that each subpart of the time string is 60 parts of the previous part. This works for hours, minutes and seconds, regardless of how it is entered, and will always use the first part as the base measurement.
j = 0
timer = 0
"12:40:30".split(':').each do |y|
timer += y.to_f / (60**j); j += 1;
end
In this example, the numbers 12, 0.66, and 0.008 are summed to give 12.6724 hours.