irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
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)
irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
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)
You can try something like below (change with conditional statements to fit your requirements of upto 2 numbers):
class Time
def duration
Time.now - self
end
def duration_string
difference = duration
days = (difference/(3600*24)).to_i
hours = ((difference%(3600*24))/3600).to_i
mins = ((difference%(3600))/60).to_i
secs = (difference%60).to_i
"#{days} days, #{hours} hours, #{mins} minutes and #{secs} seconds"
end
end
irb(main):003:0> (Time.now - start).duration
=> "25 seconds"
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)