Class: Time

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/time.rb

Instance Method Summary collapse

Instance Method Details

#elapsedObject

How many seconds have elapsed since this time?



50
51
52
# File 'lib/epitools/core_ext/time.rb', line 50

def elapsed
  Time.now - self
end

#in_wordsObject

Relative time, in words. (eg: “1 second ago”, “2 weeks from now”, etc.)



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
# File 'lib/epitools/core_ext/time.rb', line 6

def in_words
  delta   = (Time.now-self).to_i
  a       = delta.abs

  amount  = case a
    when 0
      'just now'
    when 1
      '1 second'
    when 2..59
      "second".amount(a)
    when 1.minute...1.hour
      "minute".amount(a/1.minute)
    when 1.hour...1.day
      "hour".amount(a/1.hour)
    when 1.day...7.days
      "day".amount(a/1.day)
    when 1.week...1.month
      "week".amount(a/1.week)
    when 1.month...12.months
      "month".amount(a/1.month)
    else
      "year".amount(a/1.year)
  end

  if delta < 0
    amount += " from now"
  elsif delta > 0
    amount += " ago"
  end

  amount
end

#quarterObject

Which “quarter” of the year does this date fall into?



43
44
45
# File 'lib/epitools/core_ext/time.rb', line 43

def quarter
  (month / 3.0).ceil
end