diff options
author | Aditya Sanghi <asanghi@me.com> | 2010-11-29 20:45:43 +0530 |
---|---|---|
committer | Aditya Sanghi <asanghi@me.com> | 2011-05-06 15:42:05 +0530 |
commit | ae7d0d816db3f9a94008d36ab9efbe7583700981 (patch) | |
tree | e19055231ebe51175c37fb531027a4f3d5262a5c /actionpack/lib/action_view | |
parent | 4c755f977c0f3acaa2c331dc9ab03c1ee1c28966 (diff) | |
download | rails-ae7d0d816db3f9a94008d36ab9efbe7583700981.tar.gz rails-ae7d0d816db3f9a94008d36ab9efbe7583700981.tar.bz2 rails-ae7d0d816db3f9a94008d36ab9efbe7583700981.zip |
Take leap years into account more seriously when calculating year distance [#6074 state:resolved]
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/helpers/date_helper.rb | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 9277359d5c..c5f2fb6b2d 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -94,9 +94,20 @@ module ActionView when 43200..86399 then locale.t :about_x_months, :count => 1 when 86400..525599 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round else - 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) + fyear = from_time.year + fyear += 1 if from_time.month >= 3 + tyear = to_time.year + tyear -= 1 if to_time.month < 3 + leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count{|x| Date.leap?(x)} + minute_offset_for_leap_year = leap_years * 1440 + # Discount the leap year days when calculating year distance. + # e.g. if there are 20 leap year days between 2 dates having the same day + # and month then the based on 365 days calculation + # the distance in years will come out to over 80 years when in written + # english it would read better as about 80 years. + minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year + remainder = (minutes_with_offset % 525600) + distance_in_years = (minutes_with_offset / 525600) if remainder < 131400 locale.t(:about_x_years, :count => distance_in_years) elsif remainder < 394200 |