aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorJohn Trupiano <jtrupiano@gmail.com>2009-09-26 15:42:18 -0400
committerMichael Koziarski <michael@koziarski.com>2009-09-28 14:37:31 +1300
commitc9318e9010712aeae33b1dd0e8bed4795ae37caf (patch)
tree140f14f3ec0d5cbe31a5636a431a3908a2b78fef /actionpack/lib
parent8ef1cd9733cd12fc4e5ea25c149956a33fdffa70 (diff)
downloadrails-c9318e9010712aeae33b1dd0e8bed4795ae37caf.tar.gz
rails-c9318e9010712aeae33b1dd0e8bed4795ae37caf.tar.bz2
rails-c9318e9010712aeae33b1dd0e8bed4795ae37caf.zip
Introduce :almost keyword for distance_of_time_in_words. Make 1.75 days - 2 days return '2 days'.
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#3266 state:committed]
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/date_helper.rb29
-rw-r--r--actionpack/lib/action_view/locale/en.yml3
2 files changed, 22 insertions, 10 deletions
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index a9422e92fa..8a7a870b99 100644
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -26,8 +26,10 @@ module ActionView
# 47 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
# 29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month
# 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
- # 1 yr <-> 2 yrs minus 1 secs # => about 1 year
- # 2 yrs <-> max time or date # => over [2..X] years
+ # 1 yr <-> 1 yr, 3 months # => about 1 year
+ # 1 yr, 3 months <-> 1 yr, 9 months # => over 1 year
+ # 1 yr, 9 months <-> 2 yr minus 1 sec # => almost 2 years
+ # 2 yrs <-> max time or date # => (same rules as 1 yr)
#
# With <tt>include_seconds</tt> = true and the difference < 1 minute 29 seconds:
# 0-4 secs # => less than 5 seconds
@@ -53,8 +55,8 @@ module ActionView
# 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
- # distance_of_time_in_words(to_time, from_time, true) # => over 6 years
+ # distance_of_time_in_words(from_time, to_time, true) # => about 6 years
+ # distance_of_time_in_words(to_time, from_time, true) # => about 6 years
# distance_of_time_in_words(Time.now, Time.now) # => less than a minute
#
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
@@ -82,14 +84,21 @@ module ActionView
when 2..44 then locale.t :x_minutes, :count => distance_in_minutes
when 45..89 then locale.t :about_x_hours, :count => 1
when 90..1439 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round
- when 1440..2879 then locale.t :x_days, :count => 1
- when 2880..43199 then locale.t :x_days, :count => (distance_in_minutes / 1440).round
+ when 1440..2529 then locale.t :x_days, :count => 1
+ when 2530..43199 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).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 86400..525599 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).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)
+ distance_in_years = distance_in_minutes / 525600
+ minute_offset_for_leap_year = (distance_in_years / 4) * 1440
+ remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525600)
+ if remainder < 131400
+ locale.t(:about_x_years, :count => distance_in_years)
+ elsif remainder < 394200
+ locale.t(:over_x_years, :count => distance_in_years)
+ else
+ locale.t(:almost_x_years, :count => distance_in_years + 1)
+ end
end
end
end
diff --git a/actionpack/lib/action_view/locale/en.yml b/actionpack/lib/action_view/locale/en.yml
index c82cd07ec2..84d94fd700 100644
--- a/actionpack/lib/action_view/locale/en.yml
+++ b/actionpack/lib/action_view/locale/en.yml
@@ -91,6 +91,9 @@
over_x_years:
one: "over 1 year"
other: "over {{count}} years"
+ almost_x_years:
+ one: "almost 1 year"
+ other: "almost {{count}} years"
prompts:
year: "Year"
month: "Month"