From 959707bfb62108fe9924ed4fefd6f3458972493a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 17 Jun 2005 13:42:23 +0000 Subject: r1335@iwill: jeremy | 2005-06-17 11:41:50 -0700 Ticket 1458 - distance_of_time_in_words r1336@iwill: jeremy | 2005-06-17 11:44:50 -0700 Update changelog r1337@iwill: jeremy | 2005-06-17 11:45:44 -0700 Applied patch. r1361@iwill: jeremy | 2005-06-17 11:48:02 -0700 Merge changelog git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1449 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/date_helper.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 6b1fbf7268..214f9b29d9 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -13,14 +13,19 @@ module ActionView module DateHelper DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX") - # Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return + # Reports the approximate distance in time between to Time objects or integers. + # For example, if the distance is 47 minutes, it'll return # "about 1 hour". See the source for the complete wording list. - #Set include_seconds to true if you want more detailed approximations if distance < 1 minute - def distance_of_time_in_words(from_time, to_time, include_seconds = false) - from_time = from_time.to_time - to_time = to_time.to_time - distance_in_minutes = ((to_time - from_time) / 60).round.abs - distance_in_seconds = ((to_time - from_time)).round.abs + # + # Integers are interpreted as seconds. So, + # distance_of_time_in_words(50) returns "less than a minute". + # + # Set include_seconds to true if you want more detailed approximations if distance < 1 minute + 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 -- cgit v1.2.3