Permalink
Browse files

Added months and years to the resolution of DateHelper#distance_of_ti…

…me_in_words, such that "60 days ago" becomes "2 months ago" (closes #5611) [pjhyett@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4674 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 21cf3c6 commit 8eaf479156a413b8c6e98649b6326c7baa010b43 @dhh dhh committed Aug 5, 2006
View
@@ -1,5 +1,7 @@
*SVN*
+* Added months and years to the resolution of DateHelper#distance_of_time_in_words, such that "60 days ago" becomes "2 months ago" #5611 [pjhyett@gmail.com]
+
* Short documentation to mention use of Mime::Type.register. #5710 [choonkeat@gmail.com]
* Make controller_path available as an instance method. #5724 [jmckible@gmail.com]
@@ -39,11 +39,15 @@ def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
else '1 minute'
end
- when 2..45 then "#{distance_in_minutes} minutes"
- when 46..90 then 'about 1 hour'
- when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
- when 1441..2880 then '1 day'
- else "#{(distance_in_minutes / 1440).round} days"
+ when 2..45 then "#{distance_in_minutes} minutes"
+ when 46..90 then 'about 1 hour'
+ when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
+ when 1441..2880 then '1 day'
+ when 2881..43220 then "#{(distance_in_minutes / 1440).round} days"
+ when 43201..86400 then 'about 1 month'
+ when 86401..525960 then "#{(distance_in_minutes / 43200).round} months"
+ when 525961..1051920 then 'about 1 year'
+ else "over #{(distance_in_minutes / 525600).round} years"
end
end
@@ -18,6 +18,16 @@ def test_distance_in_words
assert_equal "about 3 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 0, 41))
assert_equal "about 4 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 1, 20))
assert_equal "2 days", distance_of_time_in_words(from, Time.mktime(2004, 3, 9, 15, 40))
+
+ # test greater date separation
+ assert_equal "29 days", distance_of_time_in_words(from, Time.mktime(2004, 4, 5, 21, 41, 18))
+ assert_equal "about 1 month", distance_of_time_in_words(from, Time.mktime(2004, 4, 6, 21, 41, 18))
+ assert_equal "about 1 month", distance_of_time_in_words(from, Time.mktime(2004, 4, 7, 21, 41, 18))
+ assert_equal "2 months", distance_of_time_in_words(from, Time.mktime(2004, 5, 6, 21, 41, 18))
+ assert_equal "11 months", distance_of_time_in_words(from, Time.mktime(2005, 2, 6, 21, 41, 18))
+ assert_equal "about 1 year", distance_of_time_in_words(from, Time.mktime(2005, 4, 6, 21, 41, 18))
+ assert_equal "about 1 year", distance_of_time_in_words(from, Time.mktime(2005, 4, 12, 21, 41, 18))
+ assert_equal "over 2 years", distance_of_time_in_words(from, Time.mktime(2006, 4, 6, 21, 41, 18))
# include seconds
assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 19), false)

0 comments on commit 8eaf479

Please sign in to comment.