Skip to content

Commit

Permalink
Enhancing distance_of_time_in_words to prefix year output with over a…
Browse files Browse the repository at this point in the history
…nd about depending upon how many months have elapsed

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#3106 state:committed]
  • Loading branch information
Jay Pignata authored and NZKoz committed Sep 28, 2009
1 parent a6757a0 commit 8ef1cd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 7 additions & 4 deletions actionpack/lib/action_view/helpers/date_helper.rb
Expand Up @@ -43,13 +43,14 @@ module DateHelper
# distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
# distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
# distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds
# distance_of_time_in_words(from_time, 3.years.from_now) # => over 3 years
# distance_of_time_in_words(from_time, 3.years.from_now) # => about 3 years
# distance_of_time_in_words(from_time, from_time + 60.hours) # => about 3 days
# distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute
# distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute
# distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
# distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
# distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => over 4 years
# distance_of_time_in_words(from_time, from_time + 3.years + 6.months) # => over 3 years
# distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => about 4 years
#
# to_time = Time.now + 6.years + 19.days
# distance_of_time_in_words(from_time, to_time, true) # => over 6 years
Expand Down Expand Up @@ -85,8 +86,10 @@ def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, o
when 2880..43199 then locale.t :x_days, :count => (distance_in_minutes / 1440).round
when 43200..86399 then locale.t :about_x_months, :count => 1
when 86400..525599 then locale.t :x_months, :count => (distance_in_minutes / 43200).round
when 525600..1051199 then locale.t :about_x_years, :count => 1
else locale.t :over_x_years, :count => (distance_in_minutes / 525600).round
else
return distance_in_minutes % 525600 < 262800 ?
locale.t(:about_x_years, :count => (distance_in_minutes / 525600).round) :
locale.t(:over_x_years, :count => (distance_in_minutes / 525600).round)
end
end
end
Expand Down
15 changes: 8 additions & 7 deletions actionpack/test/template/date_helper_test.rb
Expand Up @@ -69,13 +69,14 @@ def assert_distance_of_time_in_words(from, to=nil)
assert_equal "2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 30.seconds)
assert_equal "12 months", distance_of_time_in_words(from, to + 1.years - 31.seconds)

# 525600..1051199
# > 525599
assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years - 30.seconds)
assert_equal "about 1 year", distance_of_time_in_words(from, to + 2.years - 31.seconds)

# > 1051199
assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 30.seconds)
assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years)
assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years + 6.months - 1.day)
assert_equal "over 1 year", distance_of_time_in_words(from, to + 1.years + 6.months)
assert_equal "about 2 years", distance_of_time_in_words(from, to + 2.years + 30.seconds)
assert_equal "about 5 years", distance_of_time_in_words(from, to + 5.years + 5.months)
assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 6.months)
assert_equal "about 10 years", distance_of_time_in_words(from, to + 10.years)

# test to < from
assert_equal "about 4 hours", distance_of_time_in_words(from + 4.hours, to)
Expand Down Expand Up @@ -104,7 +105,7 @@ def test_distance_in_words_with_different_time_zones
def test_distance_in_words_with_dates
start_date = Date.new 1975, 1, 31
end_date = Date.new 1977, 1, 31
assert_equal("over 2 years", distance_of_time_in_words(start_date, end_date))
assert_equal("about 2 years", distance_of_time_in_words(start_date, end_date))
end

def test_distance_in_words_with_integers
Expand Down

0 comments on commit 8ef1cd9

Please sign in to comment.