diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index d505218c7a0b5fec3ba5ec180a8d5918270ff682..97ffdd88c112b61f284e10c7795122da0c9221ca 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Added `Time#days_in_year` to return the number of days in the given year, or the + current year if no argument is provided. + + *Jon Pascoe* + * Updated `parameterize` to preserve the case of a string, optionally. Example: diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 82e003fc3b921db9de490cdea1fc83d5c64e9dd4..675db8a36b02ba69057f185ee331c3db300e7f45 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -26,6 +26,12 @@ def days_in_month(month, year = current.year) end end + # Returns the number of days in the given year. + # If no year is specified, it will use the current year. + def days_in_year(year = current.year) + days_in_month(2, year) + 337 + end + # Returns Time.zone.now when Time.zone or config.time_zone are set, otherwise just returns Time.now. def current ::Time.zone ? ::Time.zone.now : ::Time.now diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 2d0fb70a6bf3f2752696317a86298b7b95cc10a7..e45df63fd505516116095d688ef59b03e5e61515 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -617,6 +617,25 @@ def test_days_in_month_feb_in_leap_year_without_year_arg end end + def test_days_in_year_with_year + assert_equal 365, Time.days_in_year(2005) + assert_equal 366, Time.days_in_year(2004) + assert_equal 366, Time.days_in_year(2000) + assert_equal 365, Time.days_in_year(1900) + end + + def test_days_in_year_in_common_year_without_year_arg + Time.stub(:now, Time.utc(2007)) do + assert_equal 365, Time.days_in_year + end + end + + def test_days_in_year_in_leap_year_without_year_arg + Time.stub(:now, Time.utc(2008)) do + assert_equal 366, Time.days_in_year + end + end + def test_last_month_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month end