Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/git_tools/extensions/time.rb

Constant Summary collapse

DAYS_IN_YEAR =
365.2424
SECONDS_IN_MINUTE =
60
SECONDS_IN_HOUR =
3_600
SECONDS_IN_DAY =
86_400
SECONDS_IN_WEEK =
604_800
SECONDS_IN_YEAR =

AVERAGE

31_556_736
SECONDS_IN_MONTH =

AVERAGE

SECONDS_IN_YEAR/12
HUMAN_TIMES =
[
  [SECONDS_IN_MINUTE, 120, "minutes"],
  [SECONDS_IN_HOUR  ,  72, "hours"],
  [SECONDS_IN_DAY   ,  21, "days"],
  [SECONDS_IN_WEEK  ,  12, "weeks"],
  [SECONDS_IN_MONTH ,  12, "months"]
  #[SECONDS_IN_YEAR  ,  10, "years"]
]

Instance Method Summary collapse

Instance Method Details

#relative(t0 = Time.now) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_tools/extensions/time.rb', line 23

def relative(t0 = Time.now)
  dt = self - t0

  if dt < 0
    tense = 'ago'
    dt = dt.abs - 1
  else
    tense = 'from now'
  end

  if dt < SECONDS_IN_MINUTE
    return 'now'
  else
    HUMAN_TIMES.each do |time|
      seconds    = time[0]
      limit      = time[1]
      time_unit  = time[2]

      if dt < seconds * limit
        return "{time} #{time_unit} #{tense}".multi_gsub!(:time => (dt/seconds).ceil.to_i)
      end
    end
    # Above the higest limit
    "over a year #{tense}"
  end
end