Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9863: months_since_and_ago_uses_advance.diff

File months_since_and_ago_uses_advance.diff, 5.6 kB (added by gbuesing, 1 year ago)
  • test/core_ext/time_ext_test.rb

    old new  
    110110    assert_equal Time.local(2006,1,5,10),  Time.local(2005,6,5,10,0,0).months_since(7) 
    111111    assert_equal Time.local(2006,6,5,10),  Time.local(2005,6,5,10,0,0).months_since(12) 
    112112    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) 
    113117  end 
    114118 
    115119  def test_years_ago 
  • test/core_ext/date_ext_test.rb

    old new  
    8585    assert_equal Date.new(2006,1,5),  Date.new(2005,6,5).months_since(7) 
    8686    assert_equal Date.new(2006,6,5),  Date.new(2005,6,5).months_since(12) 
    8787    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) 
    8892  end 
    8993 
    9094  def test_years_ago 
  • test/core_ext/date_time_ext_test.rb

    old new  
    103103    assert_equal DateTime.civil(2006,1,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(7) 
    104104    assert_equal DateTime.civil(2006,6,5,10),  DateTime.civil(2005,6,5,10,0,0).months_since(12) 
    105105    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) 
    106110  end 
    107111 
    108112  def test_years_ago 
  • lib/active_support/core_ext/time/calculations.rb

    old new  
    9898 
    9999        # Returns a new Time representing the time a number of specified months ago 
    100100        def months_ago(months) 
    101           months_since(-months) 
     101          advance(:months => -months) 
    102102        end 
    103103 
     104        # Returns a new Time representing the time a number of specified months in the future 
    104105        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) 
    125107        end 
    126108 
    127109        # Returns a new Time representing the time a number of specified years ago 
     
    129111          change(:year => self.year - years) 
    130112        end 
    131113 
     114        # Returns a new Time representing the time a number of specified years in the future 
    132115        def years_since(years) 
    133116          change(:year => self.year + years) 
    134117        end 
  • lib/active_support/core_ext/date/calculations.rb

    old new  
    9393         
    9494        # Returns a new Date/DateTime representing the time a number of specified months ago 
    9595        def months_ago(months) 
    96           months_since(-months) 
     96          advance(:months => -months) 
    9797        end 
    98  
     98         
     99        # Returns a new Date/DateTime representing the time a number of specified months in the future 
    99100        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) 
    120102        end 
    121103 
    122104        # Returns a new Date/DateTime representing the time a number of specified years ago 
     
    124106          change(:year => self.year - years) 
    125107        end 
    126108 
     109        # Returns a new Date/DateTime representing the time a number of specified years in the future 
    127110        def years_since(years) 
    128111          change(:year => self.year + years) 
    129112        end