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)
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)
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) 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
|