Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quarter to date/time #45009

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

`ActiveSupport::Deprecation.behavior=` was supposed to accept any object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def end_of_quarter
end
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.
#
# today = Date.today # => Fri, 10 Jul 2015
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/core_ext/date_and_time_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def test_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

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
assert_equal date_time_init(2005, 1, 1, 0, 0, 0), date_time_init(2005, 2, 22, 10, 10, 10).beginning_of_year
end
Expand Down
10 changes: 9 additions & 1 deletion guides/source/active_support_core_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#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:

Expand All @@ -3320,6 +3327,7 @@ d.end_of_quarter # => Wed, 30 Jun 2010

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_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
Expand Down