提交 4c53f58a 编写于 作者: G George Claghorn

Add #prev_day and #next_day as counterparts to #yesterday and #tomorrow for...

Add #prev_day and #next_day as counterparts to #yesterday and #tomorrow for Date, Time, and DateTime
上级 b6d0d0d1
* 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`.
......
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册