Permalink
Browse files

Add #on_weekend?, #next_weekday, and #prev_weekday methods to Date, T…

…ime, and DateTime

`#on_weekend?` returns true if the receiving date/time falls on a Saturday or
Sunday.

`#next_weekday` returns a new date/time representing the next day that does
not fall on a Saturday or Sunday.

`#prev_weekday` returns a new date/time representing the previous day that
does not fall on a Saturday or Sunday.
  • Loading branch information...
georgeclaghorn committed Jan 5, 2015
1 parent de4f408 commit adf5fc3b3c9b5774c0ad621ac3503d236b8ddd9c
View
@@ -1,3 +1,17 @@
* Add `#on_weekend?`, `#next_weekday`, `#prev_weekday` methods to `Date`,
`Time`, and `DateTime`.
`#on_weekend?` returns true if the receiving date/time falls on a Saturday
or Sunday.
`#next_weekday` returns a new date/time representing the next day that does
not fall on a Saturday or Sunday.
`#prev_weekday` returns a new date/time representing the previous day that
does not fall on a Saturday or Sunday.
*George Claghorn*
* Change the default test order from `:sorted` to `:random`.
*Rafael Mendonça França*
@@ -9,6 +9,7 @@ module Calculations
:saturday => 5,
:sunday => 6
}
WEEKEND_DAYS = [ 6, 0 ]
# Returns a new date/time representing yesterday.
def yesterday
@@ -35,6 +36,11 @@ def future?
self > self.class.current
end
# Returns true if the date/time falls on a Saturday or Sunday.
def on_weekend?
wday.in?(WEEKEND_DAYS)
end
# Returns a new date/time the specified number of days ago.
def days_ago(days)
advance(:days => -days)
@@ -116,6 +122,15 @@ def next_week(given_day_in_next_week = Date.beginning_of_week)
first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week)))
end
# Returns a new date/time representing the next weekday.
def next_weekday
if tomorrow.on_weekend?
next_week(:monday).change(hour: hour, min: min, sec: sec, usec: try(:usec))
else
tomorrow
end
end
# Short-hand for months_since(1).
def next_month
months_since(1)
@@ -140,6 +155,16 @@ def prev_week(start_day = Date.beginning_of_week)
end
alias_method :last_week, :prev_week
# Returns a new date/time representing the previous weekday.
def prev_weekday
if yesterday.on_weekend?
beginning_of_week(:friday).change(hour: hour, min: min, sec: sec, usec: try(:usec))
else
yesterday
end
end
alias_method :last_weekday, :prev_weekday
# Short-hand for months_ago(1).
def prev_month
months_ago(1)
@@ -115,6 +115,21 @@ def test_next_week_with_default_beginning_of_week_set
end
end
def test_next_weekday_on_wednesday
assert_equal date_time_init(2015,1,8,0,0,0), date_time_init(2015,1,7,0,0,0).next_weekday
assert_equal date_time_init(2015,1,8,15,15,10), date_time_init(2015,1,7,15,15,10).next_weekday
end
def test_next_weekday_on_friday
assert_equal date_time_init(2015,1,5,0,0,0), date_time_init(2015,1,2,0,0,0).next_weekday
assert_equal date_time_init(2015,1,5,15,15,10), date_time_init(2015,1,2,15,15,10).next_weekday
end
def test_next_weekday_on_saturday
assert_equal date_time_init(2015,1,5,0,0,0), date_time_init(2015,1,3,0,0,0).next_weekday
assert_equal date_time_init(2015,1,5,15,15,10), date_time_init(2015,1,3,15,15,10).next_weekday
end
def test_next_month_on_31st
assert_equal date_time_init(2005,9,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_month
end
@@ -144,6 +159,21 @@ def test_prev_week_with_default_beginning_of_week
end
end
def test_prev_weekday_on_wednesday
assert_equal date_time_init(2015,1,6,0,0,0), date_time_init(2015,1,7,0,0,0).prev_weekday
assert_equal date_time_init(2015,1,6,15,15,10), date_time_init(2015,1,7,15,15,10).prev_weekday
end
def test_prev_weekday_on_monday
assert_equal date_time_init(2015,1,2,0,0,0), date_time_init(2015,1,5,0,0,0).prev_weekday
assert_equal date_time_init(2015,1,2,15,15,10), date_time_init(2015,1,5,15,15,10).prev_weekday
end
def test_prev_weekday_on_sunday
assert_equal date_time_init(2015,1,2,0,0,0), date_time_init(2015,1,4,0,0,0).prev_weekday
assert_equal date_time_init(2015,1,2,15,15,10), date_time_init(2015,1,4,15,15,10).prev_weekday
end
def test_prev_month_on_31st
assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,3,31,10,10,10).prev_month
end
@@ -231,6 +261,21 @@ def test_sunday_with_default_beginning_of_week_set
end
end
def test_on_weekend_on_saturday
assert date_time_init(2015,1,3,0,0,0).on_weekend?
assert date_time_init(2015,1,3,15,15,10).on_weekend?
end
def test_on_weekend_on_sunday
assert date_time_init(2015,1,4,0,0,0).on_weekend?
assert date_time_init(2015,1,4,15,15,10).on_weekend?
end
def test_on_weekend_on_monday
assert_not date_time_init(2015,1,5,0,0,0).on_weekend?
assert_not date_time_init(2015,1,5,15,15,10).on_weekend?
end
def with_bw_default(bw = :monday)
old_bw = Date.beginning_of_week
Date.beginning_of_week = bw

0 comments on commit adf5fc3

Please sign in to comment.