Module: Merbiful::RelativeTimeHelpers
- Included in:
- Admin
- Defined in:
- lib/merbiful-release/relative_time.rb
Class Attribute Summary collapse
-
.time_class ⇒ Object
Returns the value of attribute time_class.
-
.time_output ⇒ Object
Returns the value of attribute time_output.
Instance Method Summary collapse
- #distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) ⇒ Object
- #prettier_time(time, ampm = true) ⇒ Object
- #relative_date(time) ⇒ Object
- #relative_date_span(times) ⇒ Object
- #relative_time_span(times) ⇒ Object
- #time_ago_in_words(from_time, include_seconds = false) ⇒ Object
Class Attribute Details
.time_class ⇒ Object
Returns the value of attribute time_class.
7 8 9 |
# File 'lib/merbiful-release/relative_time.rb', line 7 def time_class @time_class end |
.time_output ⇒ Object
Returns the value of attribute time_output.
8 9 10 |
# File 'lib/merbiful-release/relative_time.rb', line 8 def time_output @time_output end |
Instance Method Details
#distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/merbiful-release/relative_time.rb', line 20 def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) distance_in_minutes = (((to_time - from_time).abs)/60).round distance_in_seconds = ((to_time - from_time).abs).round case distance_in_minutes when 0..1 return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds case distance_in_seconds when 0..4 then 'less than 5 seconds' when 5..9 then 'less than 10 seconds' when 10..19 then 'less than 20 seconds' when 20..39 then 'half a minute' when 40..59 then 'less than a minute' else '1 minute' end when 2..44 then "#{distance_in_minutes} minutes" when 45..89 then 'about 1 hour' when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" when 1440..2879 then '1 day' when 2880..43199 then "#{(distance_in_minutes / 1440).round} days" when 43200..86399 then 'about 1 month' when 86400..525599 then "#{(distance_in_minutes / 43200).round} months" when 525600..1051199 then 'about 1 year' else "over #{(distance_in_minutes / 525600).round} years" end end |
#prettier_time(time, ampm = true) ⇒ Object
112 113 114 |
# File 'lib/merbiful-release/relative_time.rb', line 112 def prettier_time(time, ampm=true) time.strftime("%I:%M#{" %p" if ampm}").sub(/^0/, '') end |
#relative_date(time) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/merbiful-release/relative_time.rb', line 54 def relative_date(time) date = time.to_date today = time_class.now.to_date if date == today time_output[:today] elsif date == (today - 1) time_output[:yesterday] elsif date == (today + 1) time_output[:tomorrow] else fmt = time_output[:initial_format].dup fmt << time_output[:year_format] unless date.year == today.year time.strftime_ordinalized(fmt) end end |
#relative_date_span(times) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/merbiful-release/relative_time.rb', line 70 def relative_date_span(times) times = [times.first, times.last].collect!(&:to_date) times.sort! if times.first == times.last relative_date(times.first) else first = times.first; last = times.last; now = time_class.now [first.strftime_ordinalized('%b %d')].tap do |arr| arr << ", #{first.year}" unless first.year == last.year arr << ' - ' arr << last.strftime('%b') << ' ' unless first.year == last.year && first.month == last.month arr << last.day.ordinalize arr << ", #{last.year}" unless first.year == last.year && last.year == now.year end.to_s end end |
#relative_time_span(times) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/merbiful-release/relative_time.rb', line 87 def relative_time_span(times) times = [times.first, times.last].collect!(&:to_time) times.sort! if times.first == times.last "#{prettier_time(times.first)} #{relative_date(times.first)}" elsif times.first.to_date == times.last.to_date same_half = (times.first.hour/12 == times.last.hour/12) "#{prettier_time(times.first, !same_half)} - #{prettier_time(times.last)} #{relative_date(times.first)}" else first = times.first; last = times.last; now = time_class.now [prettier_time(first)].tap do |arr| arr << ' ' arr << first.strftime_ordinalized('%b %d') arr << ", #{first.year}" unless first.year == last.year arr << ' - ' arr << prettier_time(last) arr << ' ' arr << last.strftime('%b') << ' ' unless first.year == last.year && first.month == last.month arr << last.day.ordinalize arr << ", #{last.year}" unless first.year == last.year && last.year == now.year end.to_s end end |
#time_ago_in_words(from_time, include_seconds = false) ⇒ Object
50 51 52 |
# File 'lib/merbiful-release/relative_time.rb', line 50 def time_ago_in_words(from_time, include_seconds = false) distance_of_time_in_words(from_time, Time.now, include_seconds) end |