aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
diff options
context:
space:
mode:
authorShota Iguchi <shota-iguchi@cookpad.com>2017-05-30 20:13:29 +0900
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-05-30 13:13:29 +0200
commit3fbe657e9b65814b3837fd13628e7a812dc0a0ea (patch)
tree45f2ffb1b7732140024a2c2c207cbd7b835cff66 /activesupport/lib/active_support/core_ext/date_and_time/calculations.rb
parent5d9d6a4fbf72029266bfa643929a9a80f9286fbe (diff)
downloadrails-3fbe657e9b65814b3837fd13628e7a812dc0a0ea.tar.gz
rails-3fbe657e9b65814b3837fd13628e7a812dc0a0ea.tar.bz2
rails-3fbe657e9b65814b3837fd13628e7a812dc0a0ea.zip
Add next occur and previous occurred day of week API (#26600)
Diffstat (limited to 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/date_and_time/calculations.rb16
1 files changed, 16 insertions, 0 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 f2ba7fdda5..e2e1d3e359 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
@@ -320,6 +320,22 @@ module DateAndTime
beginning_of_year..end_of_year
end
+ # Returns specific next occurring day of week
+ def next_occurring(day_of_week)
+ current_day_number = wday != 0 ? wday - 1 : 6
+ from_now = DAYS_INTO_WEEK.fetch(day_of_week) - current_day_number
+ from_now += 7 unless from_now > 0
+ since(from_now.days)
+ end
+
+ # Returns specific previous occurring day of week
+ def prev_occurring(day_of_week)
+ current_day_number = wday != 0 ? wday - 1 : 6
+ ago = current_day_number - DAYS_INTO_WEEK.fetch(day_of_week)
+ ago += 7 unless ago > 0
+ ago(ago.days)
+ end
+
private
def first_hour(date_or_time)
date_or_time.acts_like?(:time) ? date_or_time.beginning_of_day : date_or_time