Changeset 8715
- Timestamp:
- 01/25/08 03:29:20 (1 year ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/time/calculations.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/time_ext_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r8711 r8715 1 1 *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] 2 4 3 5 * 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 19 19 end 20 20 21 COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 22 21 23 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] 34 29 end 35 30 trunk/activesupport/test/core_ext/time_ext_test.rb
r8711 r8715 349 349 end 350 350 351 def test_days_in_month 351 def test_days_in_month_with_year 352 352 assert_equal 31, Time.days_in_month(1, 2005) 353 353 … … 367 367 assert_equal 30, Time.days_in_month(11, 2005) 368 368 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 369 381 end 370 382