Browse files

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]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8715 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ac03ad1 commit 8f7232bc76cba944ecb7769bb3dab633ed15c3a6 @gbuesing gbuesing committed Jan 25, 2008
View
2 activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* 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]
+
* 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]
* TimeZone#now returns an ActiveSupport::TimeWithZone [Geoff Buesing]
View
19 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -18,19 +18,14 @@ def self.included(base) #:nodoc:
end
end
+ COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
module ClassMethods
- # Return the number of days in the given month. If a year is given,
- # February will return the correct number of days for leap years.
- # Otherwise, this method will always report February as having 28
- # days.
- def days_in_month(month, year=nil)
- if month == 2
- !year.nil? && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) ? 29 : 28
- elsif month <= 7
- month % 2 == 0 ? 30 : 31
- else
- month % 2 == 0 ? 31 : 30
- end
+ # Return the number of days in the given month.
+ # If no year is specified, it will use the current year.
+ def days_in_month(month, year = now.year)
+ return 29 if month == 2 && ::Date.gregorian_leap?(year)
+ COMMON_YEAR_DAYS_IN_MONTH[month]
end
# Returns a new Time if requested year can be accommodated by Ruby's Time class
View
14 activesupport/test/core_ext/time_ext_test.rb
@@ -348,7 +348,7 @@ def test_fp_inaccuracy_ticket_1836
assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight
end
- def test_days_in_month
+ def test_days_in_month_with_year
assert_equal 31, Time.days_in_month(1, 2005)
assert_equal 28, Time.days_in_month(2, 2005)
@@ -367,6 +367,18 @@ def test_days_in_month
assert_equal 30, Time.days_in_month(11, 2005)
assert_equal 31, Time.days_in_month(12, 2005)
end
+
+ uses_mocha 'TestTimeDaysInMonthWithoutYearArg' do
+ def test_days_in_month_feb_in_common_year_without_year_arg
+ Time.stubs(:now).returns(Time.utc(2007))
+ assert_equal 28, Time.days_in_month(2)
+ end
+
+ def test_days_in_month_feb_in_leap_year_without_year_arg
+ Time.stubs(:now).returns(Time.utc(2008))
+ assert_equal 29, Time.days_in_month(2)
+ end
+ end
def test_time_with_datetime_fallback
assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)

0 comments on commit 8f7232b

Please sign in to comment.