Ticket #9863: months_since_and_ago_uses_advance_from_rails_root.diff
| File months_since_and_ago_uses_advance_from_rails_root.diff, 5.8 kB (added by danger, 1 year ago) |
|---|
-
activesupport/test/core_ext/time_ext_test.rb
old new 110 110 assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7) 111 111 assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12) 112 112 assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24) 113 assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1) 114 assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1) 115 assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1) 116 assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1) 113 117 end 114 118 115 119 def test_years_ago -
activesupport/test/core_ext/date_ext_test.rb
old new 85 85 assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7) 86 86 assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12) 87 87 assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24) 88 assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1) 89 assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1) 90 assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1) 91 assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1) 88 92 end 89 93 90 94 def test_years_ago -
activesupport/test/core_ext/date_time_ext_test.rb
old new 103 103 assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7) 104 104 assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12) 105 105 assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24) 106 assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1) 107 assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1) 108 assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1) 109 assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1) 106 110 end 107 111 108 112 def test_years_ago -
activesupport/lib/active_support/core_ext/time/calculations.rb
old new 98 98 99 99 # Returns a new Time representing the time a number of specified months ago 100 100 def months_ago(months) 101 months_since(-months)101 advance(:months => -months) 102 102 end 103 103 104 # Returns a new Time representing the time a number of specified months in the future 104 105 def months_since(months) 105 year, month, mday = self.year, self.month, self.mday 106 107 month += months 108 109 # in case months is negative 110 while month < 1 111 month += 12 112 year -= 1 113 end 114 115 # in case months is positive 116 while month > 12 117 month -= 12 118 year += 1 119 end 120 121 max = ::Time.days_in_month(month, year) 122 mday = max if mday > max 123 124 change(:year => year, :month => month, :day => mday) 106 advance(:months => months) 125 107 end 126 108 127 109 # Returns a new Time representing the time a number of specified years ago … … 129 111 change(:year => self.year - years) 130 112 end 131 113 114 # Returns a new Time representing the time a number of specified years in the future 132 115 def years_since(years) 133 116 change(:year => self.year + years) 134 117 end -
activesupport/lib/active_support/core_ext/date/calculations.rb
old new 93 93 94 94 # Returns a new Date/DateTime representing the time a number of specified months ago 95 95 def months_ago(months) 96 months_since(-months)96 advance(:months => -months) 97 97 end 98 98 99 # Returns a new Date/DateTime representing the time a number of specified months in the future 99 100 def months_since(months) 100 year, month, day = self.year, self.month, self.day 101 102 month += months 103 104 # in case months is negative 105 while month < 1 106 month += 12 107 year -= 1 108 end 109 110 # in case months is positive 111 while month > 12 112 month -= 12 113 year += 1 114 end 115 116 max = ::Time.days_in_month(month, year) 117 day = max if day > max 118 119 change(:year => year, :month => month, :day => day) 101 advance(:months => months) 120 102 end 121 103 122 104 # Returns a new Date/DateTime representing the time a number of specified years ago … … 124 106 change(:year => self.year - years) 125 107 end 126 108 109 # Returns a new Date/DateTime representing the time a number of specified years in the future 127 110 def years_since(years) 128 111 change(:year => self.year + years) 129 112 end