Skip to content
This repository
Browse code

Add prev_quarter and next_quarter method in Time/Date/DateTime

  • Loading branch information...
commit 2773257ac64c82affae527436b08e6d627853c1d 1 parent 1bb2f5a
authored carlosantoniodasilva committed
2  activesupport/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,7 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add `Time#prev_quarter' and 'Time#next_quarter' short-hands for months_ago(3) and months_since(3). *SungHee Kang*
  4
+
3 5
 *   Remove obsolete and unused `require_association` method from dependencies. *fxn*
4 6
 
5 7
 *   Add `:instance_accessor` option for `config_accessor`.
11  activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -202,6 +202,17 @@ def next_week(day = :monday)
202 202
     acts_like?(:time) ? result.change(:hour => 0) : result
203 203
   end
204 204
 
  205
+  # Short-hand for months_ago(3)
  206
+  def prev_quarter
  207
+    months_ago(3)
  208
+  end
  209
+  alias_method :last_quarter, :prev_quarter
  210
+
  211
+  # Short-hand for months_since(3)
  212
+  def next_quarter
  213
+    months_since(3)
  214
+  end
  215
+
205 216
   # Returns a new Date/DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
206 217
   def beginning_of_month
207 218
     acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
11  activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -186,6 +186,17 @@ def next_month
186 186
     months_since(1)
187 187
   end
188 188
 
  189
+  # Short-hand for months_ago(3)
  190
+  def prev_quarter
  191
+    months_ago(3)
  192
+  end
  193
+  alias_method :last_quarter, :prev_quarter
  194
+
  195
+  # Short-hand for months_since(3)
  196
+  def next_quarter
  197
+    months_since(3)
  198
+  end
  199
+
189 200
   # Returns number of days to start of this week, week starts on start_day (default is :monday).
190 201
   def days_to_week_start(start_day = :monday)
191 202
     start_day_number = DAYS_INTO_WEEK[start_day]
12  activesupport/test/core_ext/date_ext_test.rb
@@ -289,6 +289,18 @@ def test_last_month_on_31st
289 289
     assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
290 290
   end
291 291
 
  292
+  def test_next_quarter_on_31st
  293
+    assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter
  294
+  end
  295
+
  296
+  def test_prev_quarter_on_31st
  297
+    assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter
  298
+  end
  299
+
  300
+  def test_last_quarter_on_31st
  301
+    assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter
  302
+  end
  303
+
292 304
   def test_yesterday_constructor
293 305
     assert_equal Date.current - 1, Date.yesterday
294 306
   end
12  activesupport/test/core_ext/date_time_ext_test.rb
@@ -271,6 +271,18 @@ def test_last_month_on_31st
271 271
     assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
272 272
   end
273 273
 
  274
+  def test_next_quarter_on_31st
  275
+    assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter
  276
+  end
  277
+
  278
+  def test_prev_quarter_on_31st
  279
+    assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter
  280
+  end
  281
+
  282
+  def test_last_quarter_on_31st
  283
+    assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter
  284
+  end
  285
+
274 286
   def test_xmlschema
275 287
     assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema)
276 288
     assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema)
13  activesupport/test/core_ext/time_ext_test.rb
@@ -906,4 +906,17 @@ def test_marshalling_preserves_fractional_seconds
906 906
     assert_equal t.to_f, unmarshaled.to_f
907 907
     assert_equal t, unmarshaled
908 908
   end
  909
+
  910
+
  911
+  def test_next_quarter_on_31st
  912
+    assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter
  913
+  end
  914
+
  915
+  def test_prev_quarter_on_31st
  916
+    assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter
  917
+  end
  918
+
  919
+  def test_last_quarter_on_31st
  920
+    assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter
  921
+  end
909 922
 end
21  guides/source/active_support_core_extensions.textile
Source Rendered
@@ -3045,6 +3045,27 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
3045 3045
 
3046 3046
 +prev_month+ is aliased to +last_month+.
3047 3047
 
  3048
+h6. +prev_quarter+, +next_quarter+
  3049
+
  3050
+Same as +prev_month+ and +next_month+. It returns the date with the same day in the previous or next quarter:
  3051
+
  3052
+<ruby>
  3053
+t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
  3054
+t.prev_quarter             # => Mon, 08 Feb 2010
  3055
+t.next_quarter             # => Sun, 08 Aug 2010
  3056
+</ruby>
  3057
+
  3058
+If such a day does not exist, the last day of the corresponding month is returned:
  3059
+
  3060
+<ruby>
  3061
+Time.local(2000, 7, 31).prev_quarter  # => Sun, 30 Apr 2000
  3062
+Time.local(2000, 5, 31).prev_quarter  # => Tue, 29 Feb 2000
  3063
+Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000
  3064
+Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001
  3065
+</ruby>
  3066
+
  3067
++prev_quarter+ is aliased to +last_quarter+.
  3068
+
3048 3069
 h6. +beginning_of_week+, +end_of_week+
3049 3070
 
3050 3071
 The methods +beginning_of_week+ and +end_of_week+ return the dates for the

0 notes on commit 2773257

Please sign in to comment.
Something went wrong with that request. Please try again.