Module: PreciseDistanceOfTimeInWords

Defined in:
lib/precise_distance_of_time_in_words.rb,
lib/precise_distance_of_time_in_words/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#precise_distance_of_time_in_words(from_time, to_time = 0, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/precise_distance_of_time_in_words.rb', line 4

def precise_distance_of_time_in_words(from_time, to_time=0, options = {})
  options = {
    scope: :'datetime.precise_distance_in_words'
  }.merge!(options)

  # using to_time casting method of objects when presented
  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)

  # make sure distance is positive
  from_time, to_time = to_time, from_time if from_time > to_time

  dist = (to_time - from_time).round

  hours = dist / 3600
  minutes = (dist - hours*60*60) / 60
  seconds = (dist - hours*60*60 - minutes*60)

  words = ''

  I18n.with_options( locale: options[:locale], scope: options[:scope] ) do |locale|
    words << locale.t(:x_hours, count: hours) if hours > 0
    words << " #{locale.t(:and)} " if (hours > 0 && minutes > 0) # add_and_between?
    words << locale.t(:x_minutes, count: minutes) if minutes > 0

    if seconds > 0 && (minutes <= 0 && hours <= 0)
      words << locale.t(:x_seconds, count: seconds)
    end
  end

  return words
end