Method: ETL::Util#distance_of_time_in_words
- Defined in:
- lib/etl/util.rb
#distance_of_time_in_words(from_time, to_time = Time.now) ⇒ Object
Return the distance of time in words from the given from_time to the specified to_time. If to_time is not specified then Time.now is used. By default seconds are included…set the include_seconds argument to false to disable the seconds.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/etl/util.rb', line 6 def distance_of_time_in_words(from_time, to_time=Time.now) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) seconds = (to_time - from_time).round distance_in_days = (seconds/(60*60*24)).round seconds = seconds % (60*60*24) distance_in_hours = (seconds/(60*60)).round seconds = seconds % (60*60) distance_in_minutes = (seconds/60).round seconds = seconds % 60 distance_in_seconds = seconds s = '' s << "#{distance_in_days} days," if distance_in_days > 0 s << "#{distance_in_hours} hours, " if distance_in_hours > 0 s << "#{distance_in_minutes} minutes, " if distance_in_minutes > 0 s << "#{distance_in_seconds} seconds" s end |