Module: TimeMethods

Defined in:
lib/time_methods.rb

Overview

perform relative time conversions really easily.

Class Method Summary collapse

Class Method Details

.factual_time(t) ⇒ String

convert time to non-relative human-readable format



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

def self.factual_time(t)
  msg = ""
  if t.day == Time.now.day
    msg += "Today"
  elsif Time.now-3600*48 < t
    msg += "Yesterday"
  else
    msg += t.strftime('%-d %B %Y')
  end
  msg += t.strftime(' %H:%M:%S')
  # msg += " "
  # msg += t.hour.to_s+":"+t.min.to_s+":"+t.sec.to_s
  msg
end

.pluralize(number, text) ⇒ String

Pluralizes a string by adding “s” to the element name when there are 0 or 2+ elements.



11
12
13
14
# File 'lib/time_methods.rb', line 11

def self.pluralize(number, text)
  return "#{text}s" if number != 1
  text
end

.relative_time(diff_seconds) ⇒ String

Converts time from an integer or float amount of seconds to words.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/time_methods.rb', line 37

def self.relative_time(diff_seconds)
  delta_t = (diff_seconds.class == Float) ? diff_seconds.to_i : diff_seconds
  case delta_t
    when 0 .. 5
      if diff_seconds.class == Float
        "#{sprintf('%.2f',diff_seconds)} "+pluralize(delta_t, 'second')
      else
        "#{delta_t} "+pluralize(delta_t, 'second')
      end
    when 6 .. 59
      "#{delta_t} "+pluralize(delta_t, 'second')
    when 60 .. (3600-1)
      "#{delta_t/60} "+pluralize((delta_t/60), 'minute')
    when 3600 .. (3600*24-1)
      "#{delta_t/3600} "+pluralize((delta_t/3600), 'hour')
    when (3600*24) .. (3600*24*7-1) 
      "#{delta_t/(3600*24)} "+pluralize((delta_t/(3600*24)), 'day')
    when (3600*24*7) .. (3600*24*30-1)
      "#{delta_t/(3600*24*7)} "+pluralize((delta_t/(3600*24*7)), 'week')
    when (3600*24*30) .. (3600*24*365.25)
      "#{delta_t/(3600*24*30)} "+pluralize((delta_t/(3600*24*30)), 'month')
    when (3600*24*365.25) .. (3600*24*3652.5)
      "#{delta_t/(3600*24*30)} "+pluralize((delta_t/(3600*24*365.25)), 'year')
    else
      "#{delta_t}s"
  end
end

.time_ago(start_time) ⇒ String

Converts time into words with a Facebook like lingo (“just now”, “an hour ago”, etc…).



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/time_methods.rb', line 68

def self.time_ago(start_time)
  diff_seconds = Time.now.to_i - start_time.to_i
  case diff_seconds
    when 0 .. 59
      "just now"
    when 60 .. (3000-1)
      "#{diff_seconds/60} "+pluralize((diff_seconds/60), 'minute')+" ago"
    when 3000 .. (3500-1)
      "almost an hour ago"
    when 3500 .. (3700-1)
      "an hour ago"
    when 3700 .. (3900-1)
      "over an hour ago"
    when 3900 .. (3600*24-1)
      "#{diff_seconds/3600} "+pluralize((diff_seconds/3600), 'hour')+" ago"
    when (3600*24) .. (3600*24*7-1) 
      "#{diff_seconds/(3600*24)} "+pluralize((diff_seconds/(3600*24)), 'day')+" ago"
    when (3600*24*7) .. (3600*24*30-1)
      "#{diff_seconds/(3600*24*7)} "+pluralize((diff_seconds/(3600*24*7)), 'week')+" ago"
    when (3600*24*30) .. (3600*24*365.25)
      "#{diff_seconds/(3600*24*30)} "+pluralize((diff_seconds/(3600*24*30)), 'month')+" ago"
    else
      start_time.strftime('%-d %B %Y')
  end
end