Permalink
Browse files

Allows pass argument for `Time#prev_day` and `Time#next_day`

  • Loading branch information...
bogdanvlviv committed Sep 16, 2017
1 parent 453ab17 commit 61ac2167eff741bffb44aec231f4ea13d004134e
@@ -1,3 +1,30 @@
* Add same method signature for `Time#prev_day` and `Time#next_day`
in accordance with `Date#prev_day`, `Date#next_day`.
Allows pass argument for `Time#prev_day` and `Time#next_day`.
Before:
```
Time.new(2017, 9, 16, 17, 0).prev_day # => 2017-09-15 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_day(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
Time.new(2017, 9, 16, 17, 0).next_day # => 2017-09-17 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_day(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
```
After:
```
Time.new(2017, 9, 16, 17, 0).prev_day # => 2017-09-15 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_day(1) # => 2017-09-15 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_day # => 2017-09-17 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_day(1) # => 2017-09-17 17:00:00 +0300
```
*bogdanvlviv*
* `IO#to_json` now returns the `to_s` representation, rather than
attempting to convert to an array. This fixes a bug where `IO#to_json`
would raise an `IOError` when called on an unreadable object.
@@ -20,19 +20,19 @@ def yesterday
advance(days: -1)
end
# Returns a new date/time representing the previous day.
def prev_day
advance(days: -1)
# Returns a new date/time the specified number of days ago.
def prev_day(days = 1)
advance(days: -days)
end
# Returns a new date/time representing tomorrow.
def tomorrow
advance(days: 1)
end
# Returns a new date/time representing the next day.
def next_day
advance(days: 1)
# Returns a new date/time the specified number of days in the future.
def next_day(days = 1)
advance(days: days)
end
# Returns true if the date/time is today.
@@ -9,6 +9,11 @@ def test_yesterday
end
def test_prev_day
assert_equal date_time_init(2005, 2, 24, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(-2)
assert_equal date_time_init(2005, 2, 23, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(-1)
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(0)
assert_equal date_time_init(2005, 2, 21, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(1)
assert_equal date_time_init(2005, 2, 20, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(2)
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
@@ -19,6 +24,11 @@ def test_tomorrow
end
def test_next_day
assert_equal date_time_init(2005, 2, 20, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(-2)
assert_equal date_time_init(2005, 2, 21, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(-1)
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(0)
assert_equal date_time_init(2005, 2, 23, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(1)
assert_equal date_time_init(2005, 2, 24, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(2)
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

0 comments on commit 61ac216

Please sign in to comment.