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

Changeset 8715

Show
Ignore:
Timestamp:
01/25/08 03:29:20 (1 year ago)
Author:
gbuesing
Message:

Time.days_in_month defaults to current year if no year is supplied as argument, uses Date.gregorian_leap? to determine leap year, and uses constant lookup to determine days in month. Closes #10799 [Radar]

Files:

Legend:

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

    r8711 r8715  
    11*SVN* 
     2 
     3* Time.days_in_month defaults to current year if no year is supplied as argument #10799 [Radar], uses Date.gregorian_leap? to determine leap year, and uses constant lookup to determine days in month [Geoff Buesing]   
    24 
    35* Adding Time and DateTime #compare_with_coercion, which layers behavior on #<=> so that any combination of Time, DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r8711 r8715  
    1919        end 
    2020 
     21        COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 
     22 
    2123        module ClassMethods 
    22           # Return the number of days in the given month. If a year is given, 
    23           # February will return the correct number of days for leap years. 
    24           # Otherwise, this method will always report February as having 28 
    25           # days. 
    26           def days_in_month(month, year=nil) 
    27             if month == 2 
    28               !year.nil? && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) ?  29 : 28 
    29             elsif month <= 7 
    30               month % 2 == 0 ? 30 : 31 
    31             else 
    32               month % 2 == 0 ? 31 : 30 
    33             end 
     24          # Return the number of days in the given month.  
     25          # If no year is specified, it will use the current year.  
     26          def days_in_month(month, year = now.year) 
     27            return 29 if month == 2 && ::Date.gregorian_leap?(year) 
     28            COMMON_YEAR_DAYS_IN_MONTH[month] 
    3429          end 
    3530 
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r8711 r8715  
    349349  end 
    350350 
    351   def test_days_in_month 
     351  def test_days_in_month_with_year 
    352352    assert_equal 31, Time.days_in_month(1, 2005) 
    353353 
     
    367367    assert_equal 30, Time.days_in_month(11, 2005) 
    368368    assert_equal 31, Time.days_in_month(12, 2005) 
     369  end 
     370   
     371  uses_mocha 'TestTimeDaysInMonthWithoutYearArg' do 
     372    def test_days_in_month_feb_in_common_year_without_year_arg 
     373      Time.stubs(:now).returns(Time.utc(2007)) 
     374      assert_equal 28, Time.days_in_month(2) 
     375    end 
     376   
     377    def test_days_in_month_feb_in_leap_year_without_year_arg 
     378      Time.stubs(:now).returns(Time.utc(2008)) 
     379      assert_equal 29, Time.days_in_month(2) 
     380    end 
    369381  end 
    370382