Module: TimeWillTell::Helpers::DateHelper

Defined in:
lib/time_will_tell/helpers/date_helper.rb

Constant Summary collapse

DISTANCES_OF_TIME =
[
  { unit: :year,   minutes_per_unit: 365*24*60 },
  { unit: :month,  minutes_per_unit: 30*24*60 },
  { unit: :day,    minutes_per_unit: 24*60 },
  { unit: :hour,   minutes_per_unit: 60 },
  { unit: :minute, minutes_per_unit: 1 }
].freeze

Instance Method Summary collapse

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/time_will_tell/helpers/date_helper.rb', line 13

def exact_distance_of_time_in_words(from_time, to_time, options = {})
  options = {
    scope: :'time_will_tell.distance_in_words'
  }.merge!(options)

  include_seconds = options.fetch(:include_seconds, false)

  from_time, to_time = to_time, from_time if from_time > to_time
  timespan = to_time - from_time
  distance_in_minutes = (timespan / 60.0).to_i

  distance_in_words = []

  I18n.with_options scope: options[:scope] do |locale|
    DISTANCES_OF_TIME.each do |distance_of_time|
      unit =             distance_of_time[:unit]
      minutes_per_unit = distance_of_time[:minutes_per_unit].to_f

      count = (distance_in_minutes / minutes_per_unit).to_i

      next if count == 0

      if count == 30 && unit == :minute
        if distance_in_words.empty?
          distance_in_words << locale.t(:'special.half_an_hour')
        else
          distance_in_words << locale.t(:'special.and_a_half')
        end
      else
        distance_in_words << pluralize_unit_with_locale(count, unit, locale)
      end

      distance_in_minutes -= count * minutes_per_unit
    end

    if include_seconds && (seconds_count = (timespan % 60).to_i) > 0
      distance_in_words << pluralize_unit_with_locale(seconds_count, 'second', locale)
    end

    if distance_in_words.length > 2
      locale.t(:template_long, rest: distance_in_words[0..-2].join(' '), last: distance_in_words.last)
    else
      distance_in_words.join(' ')
    end
  end
end