From e57bd727df9d1e259b4ea9f2ab84595fe5a85b07 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 26 Feb 2006 00:46:18 +0000 Subject: 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 --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/core_ext/time/calculations.rb | 6 ++++++ activesupport/test/core_ext/time_ext_test.rb | 7 +++++++ 3 files changed, 15 insertions(+) (limited to 'activesupport') 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 ] * 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 -- cgit v1.2.3