I'm looking for an idiomatic way to get the time passed since a given date in hours, minutes and seconds.
If the given date is 2013-10-25 23:55:00 and the current date is 2013-10-27 20:55:09, the returning value should be 45:03:09. The time_difference and time_diff gems won't work with this requirement.
Look at
how to convert 270921sec into days + hours + minutes + sec ? (ruby)
which is very similar to your question, just starting from the difference in seconds
There's a method for that:
Time.now.minus_with_coercion(10.seconds.ago)
equals 10 (in fact, 9.99..., use
.round
if you want a 10).Source: http://apidock.com/rails/Time/minus_with_coercion
Hope I helped.
Or if your app is picky about rounding, just replace to_i to round:
Not sure about the idiomatic part though
Update: If time difference is expected to be more than 24 hours then the above code is not correct. If such is the case, one could follow the answer of @MrYoshiji or adjust above solution to calculate hours from a datetime object manually:
You can try with this:
And use it: