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))
then you can print it out what ever way you wish,
i.e
Try a ruby gem for that https://rubygems.org/gems/time_difference - Time Difference gem for Ruby Documentation at https://github.com/tmlee/time_difference
Building on Michael Richard's answer, here's a replacement for the
if
block that gets English pluralization right, and won't say things like "14 days and 0 hours":I've given an alternative implementation for this when used in script logs here [copied from there]:
How to generate a human readable time range using ruby on rails
If you want to show significant durations in the seconds to days range, an alternative would be (as it doesn't have to perform the best):
Alternatively you may be only interested in stripping the seconds part when it doesn't matter (also demonstrating another approach):
Hope that helps.
As an alternative you can do this:
I could not withstand to put a generic solution here - although: has a year 365 days?
Additional I put an
abs
when converting self.to_intand with translation