Module: Cinch::Helpers::NaturalLanguage

Defined in:
lib/cinch/helpers/natural_language.rb

Class Method Summary collapse

Class Method Details

.time_distance(from_time, to_time, opts = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cinch/helpers/natural_language.rb', line 8

def self.time_distance(from_time, to_time, opts={})
  opts = {show_seconds: false}.merge(opts)
  if from_time > to_time # swap if reversed
    from_time, to_time = to_time, from_time
    reversed = true
  end
  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)
  intervals = %w(year month day hour minute second)
  values = intervals.map do |interval|
    distance_in_seconds = (to_time.to_i - from_time.to_i).round(1)
    delta = (distance_in_seconds / 1.send(interval)).floor
    delta -= 1 if from_time + delta.send(interval) > to_time
    from_time += delta.send(interval)
    delta
  end
  mapping = intervals.zip(values).reject {|(l,i)| !opts[:show_seconds] && l.eql?('second') || i < 1 }
  mapping.map! {|(l,i)| '%d %s' % [i, i != 1 ? l.pluralize : l] }
  reversed ? mapping.to_sentence + ' ago' : mapping.to_sentence
end