Class: Time

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

Instance Method Summary collapse

Instance Method Details

#day_formatObject



48
49
50
51
# File 'lib/friendly_timestamp.rb', line 48

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

#friendly_formatObject



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

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 (Time.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



68
69
70
71
72
73
74
# File 'lib/friendly_timestamp.rb', line 68

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



43
44
45
46
# File 'lib/friendly_timestamp.rb', line 43

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

#minute_formatObject



38
39
40
41
# File 'lib/friendly_timestamp.rb', line 38

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

#month_formatObject



58
59
60
61
# File 'lib/friendly_timestamp.rb', line 58

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

#week_formatObject



53
54
55
56
# File 'lib/friendly_timestamp.rb', line 53

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

#year_formatObject



63
64
65
66
# File 'lib/friendly_timestamp.rb', line 63

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