Module: CoreExtensions::Time::FriendlyTimestamp

Defined in:
lib/core_extensions/time/friendly_timestamp.rb

Constant Summary collapse

ONE_MINUTE =
60
ONE_HOUR =
ONE_MINUTE * 60
ONE_DAY =
ONE_HOUR * 24
ONE_WEEK =
ONE_DAY * 7
ONE_MONTH =
ONE_DAY * 30
ONE_YEAR =
ONE_DAY * 365

Instance Method Summary collapse

Instance Method Details

#day_formatObject



46
47
48
49
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 46

def day_format
  in_days = diff_utc / ONE_DAY
  "#{word_form(in_days, 'day')} ago"
end

#diff_utcObject



79
80
81
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 79

def diff_utc
  (self.class.now - self).to_f.abs
end

#friendly_formatObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 11

def friendly_format
  # convert time to local time
  # using minute format if less hour
  # using hour format if less than a day
  # using full format otherwise (Thurs, Nov 12th 2016)
  case (self.class.now - self).to_i.abs
  when 0...ONE_MINUTE
    'a moment ago'
  when ONE_MINUTE...ONE_HOUR
    minute_format
  when ONE_HOUR...ONE_DAY
    hour_format
  when ONE_DAY...ONE_WEEK
    day_format
  when ONE_WEEK...ONE_MONTH
    week_format
  when ONE_MONTH...ONE_YEAR
    month_format
  when ONE_YEAR...ONE_YEAR * 5
    year_format
  else
    full_format
  end
end

#full_format(mn_offset = 0) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 66

def full_format(mn_offset = 0)
  # multiplle -1 here because js represent time difference varies from how rails represents
  # get offset in hour
  hour_offset = (-1 * mn_offset.to_f / 60)
  fts_local_time = in_time_zone(hour_offset)
  fts_local_time.strftime("%a, %b #{fts_local_time.day.ordinalize} %Y at %H:%M %Z")
end

#hour_formatObject



41
42
43
44
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 41

def hour_format
  in_hours = diff_utc / ONE_HOUR
  "#{word_form(in_hours, 'hour')} ago"
end

#minute_formatObject



36
37
38
39
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 36

def minute_format
  in_minutes = diff_utc / ONE_MINUTE
  "#{word_form(in_minutes, 'minute')} ago"
end

#month_formatObject



56
57
58
59
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 56

def month_format
  in_months = diff_utc / ONE_MONTH
  "#{word_form(in_months, 'month')} ago"
end

#week_formatObject



51
52
53
54
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 51

def week_format
  in_weeks = diff_utc / ONE_WEEK
  "#{word_form(in_weeks, 'week')} ago"
end

#word_form(num, word) ⇒ Object



74
75
76
77
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 74

def word_form(num, word)
  word = num.round > 1 ? word.pluralize : word.singularize
  num > num.round ? "more than #{num.round} #{word}" : "less than #{num.round} #{word}"
end

#year_formatObject



61
62
63
64
# File 'lib/core_extensions/time/friendly_timestamp.rb', line 61

def year_format
  in_years = diff_utc / ONE_YEAR
  "#{word_form(in_years, 'year')} ago"
end