aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJon Pascoe <jon.pascoe@me.com>2015-11-10 09:26:48 +0000
committerXavier Noria <fxn@hashref.com>2015-11-11 07:08:49 +0100
commit2f4f4d2cf1e4c5a442459fc250daf66186d110fa (patch)
treecc5410d285ed5d3de6257ba681fce53d7f31299e /activesupport
parent9ccb341fbf1ae1bb46a0f638bf35a4bcffe06297 (diff)
downloadrails-2f4f4d2cf1e4c5a442459fc250daf66186d110fa.tar.gz
rails-2f4f4d2cf1e4c5a442459fc250daf66186d110fa.tar.bz2
rails-2f4f4d2cf1e4c5a442459fc250daf66186d110fa.zip
Add days_in_year method
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md11
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb6
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb19
3 files changed, 33 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 3fe627a7cf..faa4a3d6fc 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -3,12 +3,17 @@
To opt-in load the [listen](https://github.com/guard/listen) gem in `Gemfile`:
- group :development do
- gem 'listen', '~> 3.0.4'
- end
+ group :development do
+ gem 'listen', '~> 3.0.4'
+ end
*Puneet Agarwal* and *Xavier Noria*
+* 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 82e003fc3b..675db8a36b 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 @@ class Time
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 <tt>Time.zone.now</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns <tt>Time.now</tt>.
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 2d0fb70a6b..e45df63fd5 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -617,6 +617,25 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
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