Skip to content

Commit 61ac216

Browse files
committed
Allows pass argument for Time#prev_day and Time#next_day
1 parent 453ab17 commit 61ac216

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

activesupport/CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
* Add same method signature for `Time#prev_day` and `Time#next_day`
2+
in accordance with `Date#prev_day`, `Date#next_day`.
3+
4+
Allows pass argument for `Time#prev_day` and `Time#next_day`.
5+
6+
Before:
7+
```
8+
Time.new(2017, 9, 16, 17, 0).prev_day # => 2017-09-15 17:00:00 +0300
9+
Time.new(2017, 9, 16, 17, 0).prev_day(1)
10+
# => ArgumentError: wrong number of arguments (given 1, expected 0)
11+
12+
Time.new(2017, 9, 16, 17, 0).next_day # => 2017-09-17 17:00:00 +0300
13+
Time.new(2017, 9, 16, 17, 0).next_day(1)
14+
# => ArgumentError: wrong number of arguments (given 1, expected 0)
15+
```
16+
17+
After:
18+
```
19+
Time.new(2017, 9, 16, 17, 0).prev_day # => 2017-09-15 17:00:00 +0300
20+
Time.new(2017, 9, 16, 17, 0).prev_day(1) # => 2017-09-15 17:00:00 +0300
21+
22+
Time.new(2017, 9, 16, 17, 0).next_day # => 2017-09-17 17:00:00 +0300
23+
Time.new(2017, 9, 16, 17, 0).next_day(1) # => 2017-09-17 17:00:00 +0300
24+
```
25+
26+
*bogdanvlviv*
27+
128
* `IO#to_json` now returns the `to_s` representation, rather than
229
attempting to convert to an array. This fixes a bug where `IO#to_json`
330
would raise an `IOError` when called on an unreadable object.

activesupport/lib/active_support/core_ext/date_and_time/calculations.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ def yesterday
2020
advance(days: -1)
2121
end
2222

23-
# Returns a new date/time representing the previous day.
24-
def prev_day
25-
advance(days: -1)
23+
# Returns a new date/time the specified number of days ago.
24+
def prev_day(days = 1)
25+
advance(days: -days)
2626
end
2727

2828
# Returns a new date/time representing tomorrow.
2929
def tomorrow
3030
advance(days: 1)
3131
end
3232

33-
# Returns a new date/time representing the next day.
34-
def next_day
35-
advance(days: 1)
33+
# Returns a new date/time the specified number of days in the future.
34+
def next_day(days = 1)
35+
advance(days: days)
3636
end
3737

3838
# Returns true if the date/time is today.

activesupport/test/core_ext/date_and_time_behavior.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ def test_yesterday
99
end
1010

1111
def test_prev_day
12+
assert_equal date_time_init(2005, 2, 24, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(-2)
13+
assert_equal date_time_init(2005, 2, 23, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(-1)
14+
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(0)
15+
assert_equal date_time_init(2005, 2, 21, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(1)
16+
assert_equal date_time_init(2005, 2, 20, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day(2)
1217
assert_equal date_time_init(2005, 2, 21, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_day
1318
assert_equal date_time_init(2005, 2, 28, 10, 10, 10), date_time_init(2005, 3, 2, 10, 10, 10).prev_day.prev_day
1419
end
@@ -19,6 +24,11 @@ def test_tomorrow
1924
end
2025

2126
def test_next_day
27+
assert_equal date_time_init(2005, 2, 20, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(-2)
28+
assert_equal date_time_init(2005, 2, 21, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(-1)
29+
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(0)
30+
assert_equal date_time_init(2005, 2, 23, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(1)
31+
assert_equal date_time_init(2005, 2, 24, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day(2)
2232
assert_equal date_time_init(2005, 2, 23, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_day
2333
assert_equal date_time_init(2005, 3, 2, 10, 10, 10), date_time_init(2005, 2, 28, 10, 10, 10).next_day.next_day
2434
end

0 commit comments

Comments
 (0)