Class: Time

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

Overview

Date helpers

Instance Method Summary collapse

Instance Method Details

#humanize(seconds) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/doing/time.rb', line 18

def humanize(seconds)
  s = seconds
  m = (s / 60).floor
  s = (s % 60).floor
  h = (m / 60).floor
  m = (m % 60).floor
  d = (h / 24).floor
  h = h % 24

  output = []
  output.push("#{d} #{'day'.to_p(d)}") if d.positive?
  output.push("#{h} #{'hour'.to_p(h)}") if h.positive?
  output.push("#{m} #{'minute'.to_p(m)}") if m.positive?
  output.push("#{s} #{'second'.to_p(s)}") if s.positive?
  output.join(', ')
end

#relative_dateObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/doing/time.rb', line 6

def relative_date
  if self > Date.today.to_time
    strftime('%_I:%M%P')
  elsif self > (Date.today - 6).to_time
    strftime('%a %_I:%M%P')
  elsif self.year == Date.today.year || (self.year + 1 == Date.today.year && self.month > Date.today.month)
    strftime('%m/%d %_I:%M%P')
  else
    strftime('%m/%d/%y %_I:%M%P')
  end
end

#time_agoObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/doing/time.rb', line 35

def time_ago
  if self > Date.today.to_time
    output = humanize(Time.now - self)
    "#{output} ago"
  elsif self > (Date.today - 1).to_time
    "Yesterday at #{strftime('%_I:%M:%S%P')}"
  elsif self > (Date.today - 6).to_time
    strftime('%a %I:%M:%S%P')
  elsif self.year == Date.today.year
    strftime('%m/%d %I:%M:%S%P')
  else
    strftime('%m/%d/%Y %I:%M:%S%P')
  end
end