Skip to content

Commit

Permalink
Allows pass argument for Time#prev_day and Time#next_day
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanvlviv committed Oct 24, 2017
1 parent 453ab17 commit 61ac216
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
27 changes: 27 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions activesupport/test/core_ext/date_and_time_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 61ac216

Please sign in to comment.