Allows pass argument for Time#prev_day and Time#next_day

This commit is contained in:
bogdanvlviv 2017-09-16 17:33:53 +03:00
parent 453ab1758b
commit 61ac2167ef
No known key found for this signature in database
GPG Key ID: E4ACD76A6DB6DFDD
3 changed files with 43 additions and 6 deletions

@ -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,9 +20,9 @@ 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.
@ -30,9 +30,9 @@ 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