Module | Merb::Helpers::DateAndTime |
In: |
merb-helpers/lib/merb-helpers/date_time_helpers.rb
|
Provides a number of methods for displaying and dealing with dates and times
Parts were strongly based on ar-code.svn.engineyard.com/plugins/relative_time_helpers/, and active_support
The key methods are `relative_date`, `relative_date_span`, and `relative_time_span`. This also gives you the Rails style Time DSL for working with numbers eg. 3.months.ago or 5.days.until(1.year.from_now)
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 20 20: def self.time_class 21: @@time_class 22: end
format<Symbol>: | time format to use |
locale<String, Symbol>: | An optional value which can be used by localization plugins |
String: | a string used to format time using strftime |
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 30 30: def self.time_output(format, locale=nil) 31: @@time_output[format] 32: end
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 180 180: def prettier_time(time, ampm=true, locale=nil) 181: time.strftime("%I:%M#{" %p" if ampm}").sub(/^0/, '') 182: end
Gives you a relative date in an attractive format
time<~to_date>: | The Date or Time to test |
locale<String, Symbol>: | An optional value which can be used by localization plugins |
String: | Relative date |
relative_date(Time.now.utc) => "today" relative_date(5.days.ago) => "March 5th" relative_date(1.year.ago) => "March 10th, 2007"
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 47 47: def relative_date(time, locale=nil) 48: date = time.to_date 49: today = DateAndTime.time_class.now.to_date 50: if date == today 51: DateAndTime.time_output(:today, locale) 52: elsif date == (today - 1) 53: DateAndTime.time_output(:yesterday, locale) 54: elsif date == (today + 1) 55: DateAndTime.time_output(:tomorrow, locale) 56: else 57: fmt = DateAndTime.time_output(:initial_format, locale).dup 58: fmt << DateAndTime.time_output(:year_format, locale) unless date.year == today.year 59: time.strftime_ordinalized(fmt, locale) 60: end 61: end
Gives you a relative date span in an attractive format
times<~first,~last>: | The Dates or Times to test |
String: | The sexy relative date span |
relative_date([1.second.ago, 10.seconds.ago]) => "March 10th" relative_date([1.year.ago, 1.year.ago) => "March 10th, 2007" relative_date([Time.now, 1.day.from_now]) => "March 10th - 11th" relative_date([Time.now, 1.year.ago]) => "March 10th, 2007 - March 10th, 2008"
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 76 76: def relative_date_span(times) 77: times = [times.first, times.last].collect! { |t| t.to_date } 78: times.sort! 79: if times.first == times.last 80: relative_date(times.first) 81: else 82: first = times.first; last = times.last; now = DateAndTime.time_class.now 83: arr = [first.strftime_ordinalized('%b %d')] 84: arr << ", #{first.year}" unless first.year == last.year 85: arr << ' - ' 86: arr << last.strftime('%b') << ' ' unless first.year == last.year && first.month == last.month 87: arr << last.day.ordinalize 88: arr << ", #{last.year}" unless first.year == last.year && last.year == now.year 89: arr.to_s 90: end 91: end
Gives you a relative date span in an attractive format
times<~first,~last>: | The Dates or Times to test |
String: | The sexy relative time span |
relative_time_span([1.second.ago, 10.seconds.ago]) => "12:00 - 12:09 AM March 10th" relative_time_span([1.year.ago, 1.year.ago) => "12:09 AM March 10th, 2007" relative_time_span([Time.now, 13.hours.from_now]) => "12:09 AM - 1:09 PM March 10th" relative_time_span([Time.now, 1.year.ago]) => "12:09 AM March 10th, 2007 - 12:09 AM March 10th, 2008"
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 106 106: def relative_time_span(times) 107: times = [times.first, times.last].collect! { |t| t.to_time } 108: times.sort! 109: if times.first == times.last 110: "#{prettier_time(times.first)} #{relative_date(times.first)}" 111: elsif times.first.to_date == times.last.to_date 112: same_half = (times.first.hour/12 == times.last.hour/12) 113: "#{prettier_time(times.first, !same_half)} - #{prettier_time(times.last)} #{relative_date(times.first)}" 114: 115: else 116: first = times.first; last = times.last; now = DateAndTime.time_class.now 117: arr = [prettier_time(first)] 118: arr << ' ' 119: arr << first.strftime_ordinalized('%b %d') 120: arr << ", #{first.year}" unless first.year == last.year 121: arr << ' - ' 122: arr << prettier_time(last) 123: arr << ' ' 124: arr << last.strftime('%b') << ' ' unless first.year == last.year && first.month == last.month 125: arr << last.day.ordinalize 126: arr << ", #{last.year}" unless first.year == last.year && last.year == now.year 127: arr.to_s 128: end 129: end
Alias for time_lost_in_words
Condenses time… very similar to time_ago_in_words in ActionPack
from_time<~to_time>: | The Date or Time to start from |
to_time<~to_time>: | The Date or Time to go to, Defaults to Time.now.utc |
include_seconds<Boolean>: | Count the seconds initially, Defaults to false |
locale<String, Symbol>: | An optional value which can be used by localization plugins |
String: | The time distance |
time_lost_in_words(3.minutes.from_now) # => 3 minutes time_lost_in_words(Time.now - 15.hours) # => 15 hours time_lost_in_words(Time.now, 3.minutes.from_now) # => 3 minutes time_lost_in_words(Time.now) # => less than a minute time_lost_in_words(Time.now, Time.now, true) # => less than 5 seconds
# File merb-helpers/lib/merb-helpers/date_time_helpers.rb, line 149 149: def time_lost_in_words(from_time, to_time = Time.now.utc, include_seconds = false, locale=nil) 150: from_time = from_time.to_time if from_time.respond_to?(:to_time) 151: to_time = to_time.to_time if to_time.respond_to?(:to_time) 152: distance_in_minutes = (((to_time - from_time).abs)/60).round 153: distance_in_seconds = ((to_time - from_time).abs).round 154: 155: case distance_in_minutes 156: when 0..1 157: return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds 158: case distance_in_seconds 159: when 0..4 then 'less than 5 seconds' 160: when 5..9 then 'less than 10 seconds' 161: when 10..19 then 'less than 20 seconds' 162: when 20..39 then 'half a minute' 163: when 40..59 then 'less than a minute' 164: else '1 minute' 165: end 166: 167: when 2..44 then "#{distance_in_minutes} minutes" 168: when 45..89 then 'about 1 hour' 169: when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" 170: when 1440..2879 then '1 day' 171: when 2880..43199 then "#{(distance_in_minutes / 1440).round} days" 172: when 43200..86399 then 'about 1 month' 173: when 86400..525599 then "#{(distance_in_minutes / 43200).round} months" 174: when 525600..1051199 then 'about 1 year' 175: else "over #{(distance_in_minutes / 525600).round} years" 176: end 177: end