Can Ruby do something like this?
irb(main):001:0> start = Time.now
=> Thu Nov 05 01:02:54 -0800 2009
irb(main):002:0> Time.now - start
=> 25.239
irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
(the duration method doesn't exist now)... and similarly, report
23 minutes and 35 seconds
1 hour and 33 minutes
2 days and 3 hours
(either report the whole duration, up to how many seconds, or report up to 2 numbers and units (if day and hour is reported, then no need to tell how many minutes))
Have a look at the Rails DateHelper.distance_of_time_in_words method. It will give you a great starting place. Despite being loaded with magic numbers, the approach should work for you.
I came with this solution
Be aware that this won't work for differences bigger than 60 days (In that case you will need to add condition)
Time difference a pretty printed string:
Time difference as an Array:
Example:
Here's a quick and simple way to implement this. Set predefined measurements for seconds, minutes, hours and days. Then depending on the size of the number, output the appropriate string with the those units. We'll extend
Numeric
so that you can invoke the method on any numeric class (Fixnum
,Bignum
, or in your caseFloat
).There is a gem available https://rubygems.org/gems/time_diff
Which gives the difference in a hash