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

Changeset 7862

Show
Ignore:
Timestamp:
10/13/07 20:12:36 (11 months ago)
Author:
bitsweat
Message:

Refactor Time and Date#months_since and #months_ago to use #advance. Closes #9863.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r7828 r7862  
    11*SVN* 
     2 
     3* Refactor Time and Date#months_since and #months_ago to use #advance.  #9863 [Geoff Buesing] 
    24 
    35* Rebundle Builder 2.1.2 but prefer a newer RubyGem if available.  [Jeremy Kemper] 
  • trunk/activesupport/lib/active_support/core_ext/date/calculations.rb

    r7508 r7862  
    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 
    9898 
     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 
     
    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) 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r7666 r7862  
    9999        # Returns a new Time representing the time a number of specified months ago 
    100100        def months_ago(months) 
    101           months_since(-months) 
    102         end 
    103  
     101          advance(:months => -months) 
     102        end 
     103 
     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 
     
    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) 
  • trunk/activesupport/test/core_ext/date_ext_test.rb

    r7733 r7862  
    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 
  • trunk/activesupport/test/core_ext/date_time_ext_test.rb

    r7733 r7862  
    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 
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r7666 r7862  
    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