3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/relative_time.rb', line 3
def relative_to_now(reference_time=Time.now)
secs = self.to_i - reference_time.to_i
sufix = if secs < 0 then 'ago' else 'from now' end
secs = secs.abs
result = nil
[ [31536000, 'year'], [2592000, 'month'], [604800, 'week'],
[86400, 'day'], [3600, 'hour'], [60, 'minute']].
each do |x|
if result.nil? && secs >= x[0] then
number = secs / x[0]
result = filter_time_to(number, x[1], sufix)
end
end
return result || 'Less than one minute ago'.t
end
|