Class: Time

Inherits:
Object
  • Object
show all
Includes:
Inflector
Defined in:
lib/relative_time.rb

Instance Method Summary collapse

Instance Method Details

#relative_to_now(reference_time = Time.now) ⇒ Object



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 = 86400 * 365 days (1 year)
  # 2592000 = 86400 * 30 days (1 month)
  # 604800 = 86400 seconds * 7 days (1 week)
  # 86400 = 60 seconds * 60 minutes * 24 h (1 day)
  # 3600 = 60 seconds * 60 minutes (1 hour)
  [ [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