From 8f7232bc76cba944ecb7769bb3dab633ed15c3a6 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Fri, 25 Jan 2008 03:29:20 +0000 Subject: 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 --- .../lib/active_support/core_ext/time/calculations.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/time') 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 -- cgit v1.2.3