Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Time#beginning_of_quarter (closes #3607) [cohen.jeff@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3659 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e57bd727df9d1e259b4ea9f2ab84595fe5a85b07 1 parent 3d0a9ff
@dhh dhh authored
View
2  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]
View
6 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -152,6 +152,12 @@ def end_of_month
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
View
7 activesupport/test/core_ext/time_ext_test.rb
@@ -30,6 +30,13 @@ def test_beginning_of_month
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
Please sign in to comment.
Something went wrong with that request. Please try again.