Class: Time

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

Instance Method Summary collapse

Instance Method Details

#time_ago_in_wordsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tit.rb', line 43

def time_ago_in_words
  t = Time.now
  secs = t - self
  minutes = secs / 60
  hours = minutes / 60
  days = hours / 24
  if hours <= 12  # show a fuzzy time
    if hours > 2
      "#{hours.to_i} hours ago"
    elsif hours > 1
      "about an hour ago"
    elsif minutes > 2
      "#{minutes.to_i} minutes ago"
    elsif minutes > 1
      "about a minute ago"
    else
      "just now"
    end
  elsif days <= 15  # show time ago in days, with time if it was yesterday or today
    if t.day == day
      "today at #{strftime("%X")}"
    elsif t.day - day == 1
      "yesterday at #{strftime("%X")}"
    else
      "#{days.to_i} days ago"
    end
  else  # time in months or years
    months = t.month - month + (t.year - year) * 12
    if months <= 6
      if months == 1
        "last month"
      elsif t.year == year
        "this #{strftime("%B")}"
      else
        "last #{strftime("%B")}"
      end
    else
      if t.year - year == 1
        "last year"
      else
        "#{t.year - year} years ago"
      end
    end
  end
end