Browse files

forgot to add some helper functions to the repo

  • Loading branch information...
1 parent cfabdbe commit baf5dccf9218a4c1bed6d3d3c5d4ca5ed404963a @remi committed Oct 9, 2008
Showing with 115 additions and 0 deletions.
  1. +35 −0 lib/timetray/distance_of_time_in_words.rb
  2. +80 −0 lib/timetray/relative_time.rb
View
35 lib/timetray/distance_of_time_in_words.rb
@@ -0,0 +1,35 @@
+# taken from Rails
+# http://api.rubyonrails.com/classes/ActionView/Helpers/DateHelper.html
+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
+
+def time_ago_in_words(from_time, include_seconds = false)
+ distance_of_time_in_words(from_time, Time.now, include_seconds)
+end
View
80 lib/timetray/relative_time.rb
@@ -0,0 +1,80 @@
+# taken from the relative_time_helpers Rails plugin by Rick Olsen
+# http://github.com/technoweenie/relative_time_helpers
+
+module RelativeTimeHelpers
+ mattr_accessor :time_class
+ mattr_accessor :time_output
+
+ self.time_class = Time
+ self.time_output = {
+ :today => 'today',
+ :yesterday => 'yesterday',
+ :tomorrow => 'tomorrow',
+ :initial_format => '%b %d',
+ :year_format => ', %Y'
+ }
+
+ 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
+
+ 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
+ returning [first.strftime_ordinalized('%b %d')] 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
+
+ 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
+
+ def prettier_time(time, ampm=true)
+ time.strftime("%I:%M#{" %p" if ampm}").sub(/^0/, '')
+ end
+end
+
+include RelativeTimeHelpers

0 comments on commit baf5dcc

Please sign in to comment.