From 4c53f58a0fc89dd8bf0ea9abd40b7a198dd13b99 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Tue, 6 Jan 2015 05:30:04 -0500 Subject: [PATCH] Add #prev_day and #next_day as counterparts to #yesterday and #tomorrow for Date, Time, and DateTime --- activesupport/CHANGELOG.md | 5 +++++ .../core_ext/date_and_time/calculations.rb | 22 ++++++++++++++----- .../test/core_ext/date_and_time_behavior.rb | 10 +++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f301cbbd87..2756f7e0e2 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add `#prev_day` and `#next_day` counterparts to `#yesterday` and + `#tomorrow` for `Date`, `Time`, and `DateTime`. + + *George Claghorn* + * Add `same_time` option to `#next_week` and `#prev_week` for `Date`, `Time`, and `DateTime`. 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 5f16794926..e4bfd9f7c5 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 @@ -13,12 +13,22 @@ module Calculations # Returns a new date/time representing yesterday. def yesterday - advance(:days => -1) + advance(days: -1) + end + + # Returns a new date/time representing the previous day. + def prev_day + advance(days: -1) end # Returns a new date/time representing tomorrow. def tomorrow - advance(:days => 1) + advance(days: 1) + end + + # Returns a new date/time representing the next day. + def next_day + advance(days: 1) end # Returns true if the date/time is today. @@ -125,10 +135,10 @@ def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) # Returns a new date/time representing the next weekday. def next_weekday - if tomorrow.on_weekend? + if next_day.on_weekend? next_week(:monday, same_time: true) else - tomorrow + next_day end end @@ -159,10 +169,10 @@ def prev_week(start_day = Date.beginning_of_week, same_time: false) # Returns a new date/time representing the previous weekday. def prev_weekday - if yesterday.on_weekend? + if prev_day.on_weekend? copy_time_to(beginning_of_week(:friday)) else - yesterday + prev_day end end alias_method :last_weekday, :prev_weekday diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb index 6b6e1a2a4c..784547bdf8 100644 --- a/activesupport/test/core_ext/date_and_time_behavior.rb +++ b/activesupport/test/core_ext/date_and_time_behavior.rb @@ -6,11 +6,21 @@ def test_yesterday assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday end + def test_prev_day + assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).prev_day + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).prev_day.prev_day + end + def test_tomorrow assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow end + def test_next_day + assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).next_day + assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).next_day.next_day + end + def test_days_ago assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1) assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5) -- GitLab