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
36
37
38
39
40
41
42
43
|
# File 'lib/time_duration_humanizer.rb', line 6
def self.humanize(seconds, options = {}, units = {})
options = {
and_at_end: true,
days_in_year: 1.year / 1.day
}.merge!(options.symbolize_keys)
units = {
years: true,
months: true,
weeks: false,
days: true,
hours: true,
minutes: true,
seconds: true
}.merge!(units.symbolize_keys)
values = []
duration = ''
units.each do |k, v|
if v == true && @@known_units.include?(k)
u = (k == :years ? options[:days_in_year].days : 1.send(k)).to_i
value = seconds >= u ? seconds / u : 0
if value > 0
values << { name: k.to_s, value: value }
seconds -= value * u
end
end
end
l = values.length
values.each_with_index do |v, i|
separator = i == l - 1 ? '' : (i == l - 2 && options[:and_at_end] == true ? " #{I18n.t('time_duration_humanizer.and')} " : ', ')
duration += "#{v[:value]} #{I18n.t("time_duration_humanizer.#{v[:value] == 1 ? v[:name].singularize : v[:name]}")}#{separator}"
end
duration
end
|