aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb6
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb7
3 files changed, 15 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index e6e8f2d1ea..b68e2cad15 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added Time#beginning_of_quarter #3607 [cohen.jeff@gmail.com]
+
* Fix Object.subclasses_of to only return currently defined objects [Jonathan Viney <jonathan@bluewire.net.nz>]
* Fix constantize to properly handle names beginning with '::'. [Nicholas Seckar]
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 43424df8f4..6a80762a62 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -152,6 +152,12 @@ module ActiveSupport #:nodoc:
change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0)
end
alias :at_end_of_month :end_of_month
+
+ # Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
+ def beginning_of_quarter
+ beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
+ end
+ alias :at_beginning_of_quarter :beginning_of_quarter
# Returns a new Time representing the start of the year (1st of january, 0:00)
def beginning_of_year
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 5dce75b6f4..203f9d1a48 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -30,6 +30,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
end
+ def test_beginning_of_quarter
+ assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter
+ assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter
+ assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter
+ assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter
+ end
+
def test_end_of_month
assert_equal Time.local(2005,3,31,0,0,0), Time.local(2005,3,20,10,10,10).end_of_month
assert_equal Time.local(2005,2,28,0,0,0), Time.local(2005,2,20,10,10,10).end_of_month