diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 03:29:20 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 03:29:20 +0000 |
commit | 8f7232bc76cba944ecb7769bb3dab633ed15c3a6 (patch) | |
tree | df6692bb783be16f6f2bbfb692a3272c66a64499 /activesupport/lib/active_support/core_ext/time | |
parent | ac03ad1f78d88f40924225089a4b4bfebc8c74d8 (diff) | |
download | rails-8f7232bc76cba944ecb7769bb3dab633ed15c3a6.tar.gz rails-8f7232bc76cba944ecb7769bb3dab633ed15c3a6.tar.bz2 rails-8f7232bc76cba944ecb7769bb3dab633ed15c3a6.zip |
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
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 534571fe61..eb342084a8 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -18,19 +18,14 @@ module ActiveSupport #: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 |