mirror of https://github.com/rails/rails
Add `quarter` method to date/time (#45009)
Co-authored-by: David Heinemeier Hansson <david@hey.com>
This commit is contained in:
parent
4f818c7202
commit
959d46ef87
|
@ -1,3 +1,9 @@
|
||||||
|
* Add `quarter` method to date/time
|
||||||
|
|
||||||
|
*Matt Swanson*
|
||||||
|
|
||||||
|
* Add `urlsafe` option to `ActiveSupport::MessageVerifier` initializer
|
||||||
|
|
||||||
* Fix `NoMethodError` on custom `ActiveSupport::Deprecation` behavior.
|
* Fix `NoMethodError` on custom `ActiveSupport::Deprecation` behavior.
|
||||||
|
|
||||||
`ActiveSupport::Deprecation.behavior=` was supposed to accept any object
|
`ActiveSupport::Deprecation.behavior=` was supposed to accept any object
|
||||||
|
|
|
@ -157,6 +157,16 @@ module DateAndTime
|
||||||
end
|
end
|
||||||
alias :at_end_of_quarter :end_of_quarter
|
alias :at_end_of_quarter :end_of_quarter
|
||||||
|
|
||||||
|
# Returns the quarter for a date/time.
|
||||||
|
#
|
||||||
|
# Date.new(2010, 1, 31).quarter # => 1
|
||||||
|
# Date.new(2010, 4, 12).quarter # => 2
|
||||||
|
# Date.new(2010, 9, 15).quarter # => 3
|
||||||
|
# Date.new(2010, 12, 25).quarter # => 4
|
||||||
|
def quarter
|
||||||
|
(month / 3.0).ceil
|
||||||
|
end
|
||||||
|
|
||||||
# Returns a new date/time at the beginning of the year.
|
# Returns a new date/time at the beginning of the year.
|
||||||
#
|
#
|
||||||
# today = Date.today # => Fri, 10 Jul 2015
|
# today = Date.today # => Fri, 10 Jul 2015
|
||||||
|
|
|
@ -92,6 +92,21 @@ module DateAndTimeBehavior
|
||||||
assert_equal date_time_init(2008, 6, 30, 23, 59, 59, Rational(999999999, 1000)), date_time_init(2008, 5, 31, 0, 0, 0).end_of_quarter
|
assert_equal date_time_init(2008, 6, 30, 23, 59, 59, Rational(999999999, 1000)), date_time_init(2008, 5, 31, 0, 0, 0).end_of_quarter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_quarter
|
||||||
|
assert_equal 1, date_time_init(2005, 1, 1, 0, 0, 0).quarter
|
||||||
|
assert_equal 1, date_time_init(2005, 2, 15, 12, 0, 0).quarter
|
||||||
|
assert_equal 1, date_time_init(2005, 3, 31, 23, 59, 59).quarter
|
||||||
|
assert_equal 2, date_time_init(2005, 4, 1, 0, 0, 0).quarter
|
||||||
|
assert_equal 2, date_time_init(2005, 5, 15, 12, 0, 0).quarter
|
||||||
|
assert_equal 2, date_time_init(2005, 6, 30, 23, 59, 59).quarter
|
||||||
|
assert_equal 3, date_time_init(2005, 7, 1, 0, 0, 0).quarter
|
||||||
|
assert_equal 3, date_time_init(2005, 8, 15, 12, 0, 0).quarter
|
||||||
|
assert_equal 3, date_time_init(2005, 9, 30, 23, 59, 59).quarter
|
||||||
|
assert_equal 4, date_time_init(2005, 10, 1, 0, 0, 0).quarter
|
||||||
|
assert_equal 4, date_time_init(2005, 11, 15, 12, 0, 0).quarter
|
||||||
|
assert_equal 4, date_time_init(2005, 12, 31, 23, 59, 59).quarter
|
||||||
|
end
|
||||||
|
|
||||||
def test_beginning_of_year
|
def test_beginning_of_year
|
||||||
assert_equal date_time_init(2005, 1, 1, 0, 0, 0), date_time_init(2005, 2, 22, 10, 10, 10).beginning_of_year
|
assert_equal date_time_init(2005, 1, 1, 0, 0, 0), date_time_init(2005, 2, 22, 10, 10, 10).beginning_of_year
|
||||||
end
|
end
|
||||||
|
|
|
@ -3306,7 +3306,14 @@ NOTE: Defined in `active_support/core_ext/date_and_time/calculations.rb`.
|
||||||
[DateAndTime::Calculations#beginning_of_month]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-beginning_of_month
|
[DateAndTime::Calculations#beginning_of_month]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-beginning_of_month
|
||||||
[DateAndTime::Calculations#end_of_month]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-end_of_month
|
[DateAndTime::Calculations#end_of_month]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-end_of_month
|
||||||
|
|
||||||
##### `beginning_of_quarter`, `end_of_quarter`
|
##### `quarter`, `beginning_of_quarter`, `end_of_quarter`
|
||||||
|
|
||||||
|
The method [`quarter`][DateAndTime::Calculations#quarter] returns the quarter of the receiver's calendar year:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
d = Date.new(2010, 5, 9) # => Sun, 09 May 2010
|
||||||
|
d.quarter # => 2
|
||||||
|
```
|
||||||
|
|
||||||
The methods [`beginning_of_quarter`][DateAndTime::Calculations#beginning_of_quarter] and [`end_of_quarter`][DateAndTime::Calculations#end_of_quarter] return the dates for the beginning and end of the quarter of the receiver's calendar year:
|
The methods [`beginning_of_quarter`][DateAndTime::Calculations#beginning_of_quarter] and [`end_of_quarter`][DateAndTime::Calculations#end_of_quarter] return the dates for the beginning and end of the quarter of the receiver's calendar year:
|
||||||
|
|
||||||
|
@ -3320,6 +3327,7 @@ d.end_of_quarter # => Wed, 30 Jun 2010
|
||||||
|
|
||||||
NOTE: Defined in `active_support/core_ext/date_and_time/calculations.rb`.
|
NOTE: Defined in `active_support/core_ext/date_and_time/calculations.rb`.
|
||||||
|
|
||||||
|
[DateAndTime::Calculations#quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-quarter
|
||||||
[DateAndTime::Calculations#at_beginning_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-at_beginning_of_quarter
|
[DateAndTime::Calculations#at_beginning_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-at_beginning_of_quarter
|
||||||
[DateAndTime::Calculations#at_end_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-at_end_of_quarter
|
[DateAndTime::Calculations#at_end_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-at_end_of_quarter
|
||||||
[DateAndTime::Calculations#beginning_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-beginning_of_quarter
|
[DateAndTime::Calculations#beginning_of_quarter]: https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-beginning_of_quarter
|
||||||
|
|
Loading…
Reference in New Issue