1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixed that Time.local(2005,12).months_since(1) would raise "ArgumentError: argument out of range" #1311 [jhahn@niveon.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1308 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-05-19 16:36:04 +00:00
parent aa20834222
commit e84deb71d2
2 changed files with 4 additions and 1 deletions

View file

@ -48,7 +48,8 @@ module ActiveSupport #:nodoc:
def months_since(months)
if months + self.month > 12
change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1)))
old_time = self
change(:year => self.year + 1, :month => 1).months_since(months + old_time.month - 12 - 1)
else
change(:year => self.year, :month => self.month + months)
end

View file

@ -37,7 +37,9 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_months_since
assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1)
assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1)
assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6)
assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6)
assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7)
assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12)
assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24)