mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use leap years trick in distance_of_time_in_words only for distances between real date points
This commit is contained in:
parent
5fdd4cd9e4
commit
7322bd45f0
2 changed files with 18 additions and 12 deletions
|
@ -95,18 +95,22 @@ 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
|
||||
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
|
||||
if from_time.acts_like?(:time) && to_time.acts_like?(:time)
|
||||
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
|
||||
else
|
||||
minutes_with_offset = distance_in_minutes
|
||||
end
|
||||
remainder = (minutes_with_offset % 525600)
|
||||
distance_in_years = (minutes_with_offset / 525600)
|
||||
if remainder < 131400
|
||||
|
|
|
@ -133,6 +133,8 @@ class DateHelperTest < ActionView::TestCase
|
|||
assert_equal "about 1 hour", distance_of_time_in_words(60*60)
|
||||
assert_equal "1 minute", distance_of_time_in_words(0, 59)
|
||||
assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0)
|
||||
assert_equal "about 3 years", distance_of_time_in_words(10**8)
|
||||
assert_equal "about 3 years", distance_of_time_in_words(0, 10**8)
|
||||
end
|
||||
|
||||
def test_distance_in_words_with_times
|
||||
|
|
Loading…
Reference in a new issue