I’m trying to figure out the best practice for handling a duration
field (for example “time it takes someone to run a mile.” )
All I care about is seconds (not milliseconds) so figured I’d store
the field as an integer.
I want to allow the user to enter in the time manually as:
//1 minute 5 seconds:
1:05
OR
01:05
//2 hours 4 minutes 6 seconds:
2:04:06
OR
02:04:06
I have a helper that will convert seconds to a time display as:
def seconds_to_time seconds
Time.at(seconds).gmtime.strftime(’%R:%S’)
end
But how should I handle the conversion the other way around from the
input (ie convert 01:35 to seconds?)
Do I need to do some complex parsing of the String itself parsing out
: (remember it could be xx:xx:xx or just xx:xx )
or can I somehow leverage Time.parse ?
I would think this would come up quite often so there must be a best
practice or easy way to handle this kind of thing.