提交 4f700edc 编写于 作者: C Carlos Antonio da Silva

Merge pull request #6402 from paranoiase/prev-and-next-quarter

Add prev_quarter and next_quarter methods in Time/Date/DateTime calculations.

Similar to prev_month and next_month, it returns the date with the same day
in the previous or next quarter:

    t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
    t.prev_quarter             # => Mon, 08 Feb 2010
    t.next_quarter             # => Sun, 08 Aug 2010

Closes #6402
......@@ -8,7 +8,7 @@ else
gem 'arel'
end
gem 'minitest', '~> 3.0.0'
gem 'minitest', '~> 3.1.0'
gem 'mocha', '>= 0.11.2'
gem 'rack-test', github: "brynary/rack-test"
gem 'bcrypt-ruby', '~> 3.0.0'
......
## Rails 4.0.0 (unreleased) ##
* Add `Time#prev_quarter' and 'Time#next_quarter' short-hands for months_ago(3) and months_since(3). *SungHee Kang*
* Remove obsolete and unused `require_association` method from dependencies. *fxn*
* Add `:instance_accessor` option for `config_accessor`.
......
......@@ -202,6 +202,17 @@ def next_week(day = :monday)
acts_like?(:time) ? result.change(:hour => 0) : result
end
# Short-hand for months_ago(3)
def prev_quarter
months_ago(3)
end
alias_method :last_quarter, :prev_quarter
# Short-hand for months_since(3)
def next_quarter
months_since(3)
end
# Returns a new Date/DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
def beginning_of_month
acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
......
......@@ -186,6 +186,17 @@ def next_month
months_since(1)
end
# Short-hand for months_ago(3)
def prev_quarter
months_ago(3)
end
alias_method :last_quarter, :prev_quarter
# Short-hand for months_since(3)
def next_quarter
months_since(3)
end
# Returns number of days to start of this week, week starts on start_day (default is :monday).
def days_to_week_start(start_day = :monday)
start_day_number = DAYS_INTO_WEEK[start_day]
......
......@@ -111,6 +111,7 @@ def finish(name, id, payload)
finished = Time.now
event = @event_stack.pop
event.end = finished
event.payload.merge!(payload)
method = name.split('.').first
begin
......
......@@ -39,7 +39,7 @@ class ParallelEach
include Enumerable
# default to 2 cores
CORES = ENV['TEST_CORES'] || 2
CORES = (ENV['TEST_CORES'] || 2).to_i
def initialize list
@list = list
......
......@@ -289,6 +289,18 @@ def test_last_month_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
end
def test_next_quarter_on_31st
assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter
end
def test_prev_quarter_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter
end
def test_last_quarter_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter
end
def test_yesterday_constructor
assert_equal Date.current - 1, Date.yesterday
end
......
......@@ -271,6 +271,18 @@ def test_last_month_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
end
def test_next_quarter_on_31st
assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter
end
def test_prev_quarter_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter
end
def test_last_quarter_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter
end
def test_xmlschema
assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema)
assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema)
......
......@@ -906,4 +906,17 @@ def test_marshalling_preserves_fractional_seconds
assert_equal t.to_f, unmarshaled.to_f
assert_equal t, unmarshaled
end
def test_next_quarter_on_31st
assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter
end
def test_prev_quarter_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter
end
def test_last_quarter_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter
end
end
......@@ -3045,6 +3045,27 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
+prev_month+ is aliased to +last_month+.
h6. +prev_quarter+, +next_quarter+
Same as +prev_month+ and +next_month+. It returns the date with the same day in the previous or next quarter:
<ruby>
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
t.prev_quarter # => Mon, 08 Feb 2010
t.next_quarter # => Sun, 08 Aug 2010
</ruby>
If such a day does not exist, the last day of the corresponding month is returned:
<ruby>
Time.local(2000, 7, 31).prev_quarter # => Sun, 30 Apr 2000
Time.local(2000, 5, 31).prev_quarter # => Tue, 29 Feb 2000
Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000
Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001
</ruby>
+prev_quarter+ is aliased to +last_quarter+.
h6. +beginning_of_week+, +end_of_week+
The methods +beginning_of_week+ and +end_of_week+ return the dates for the
......
......@@ -5,6 +5,7 @@
module ApplicationTests
module RackTests
class LoggerTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include ActiveSupport::LogSubscriber::TestHelper
include Rack::Test::Methods
......@@ -17,6 +18,7 @@ def setup
end
def teardown
super
teardown_app
end
......
......@@ -3,12 +3,16 @@
module ApplicationTests
module RakeTests
class RakeNotesTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
require "rails/all"
super
end
def teardown
super
teardown_app
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册