aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorKrzysztof Rybka <krzysiek1507@users.noreply.github.com>2019-01-14 22:11:53 +0100
committerRafael França <rafaelmfranca@gmail.com>2019-01-14 16:11:53 -0500
commit40da1c430bc2d8d5cd9a9349993fbdf62e68529f (patch)
tree943fc85a3e29c53bcf904add9758fc122c857f14 /activesupport/lib
parent0ffafd475bf4dbc2d0d30b0f06c99af9fbe22f01 (diff)
downloadrails-40da1c430bc2d8d5cd9a9349993fbdf62e68529f.tar.gz
rails-40da1c430bc2d8d5cd9a9349993fbdf62e68529f.tar.bz2
rails-40da1c430bc2d8d5cd9a9349993fbdf62e68529f.zip
Refactor calculating beginning_of_quarter and end_of_quarter (#34927)
* Calculate first month of quarter instead of finding * Calculate last month of quarter instead of finding [Krzysztof Rybka + Rafael Mendonça França]
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/date_and_time/calculations.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
index 05abd83221..e2e11545e2 100644
--- a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
@@ -134,7 +134,7 @@ module DateAndTime
# now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
# now.beginning_of_quarter # => Wed, 01 Jul 2015 00:00:00 +0000
def beginning_of_quarter
- first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
+ first_quarter_month = month - (2 + month) % 3
beginning_of_month.change(month: first_quarter_month)
end
alias :at_beginning_of_quarter :beginning_of_quarter
@@ -149,7 +149,7 @@ module DateAndTime
# now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
# now.end_of_quarter # => Wed, 30 Sep 2015 23:59:59 +0000
def end_of_quarter
- last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
+ last_quarter_month = month + (12 - month) % 3
beginning_of_month.change(month: last_quarter_month).end_of_month
end
alias :at_end_of_quarter :end_of_quarter