Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allows pass argument for Time#prev_month and Time#next_month
  • Loading branch information
bogdanvlviv committed Oct 24, 2017
1 parent 61ac216 commit f2c1e3a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 22 deletions.
27 changes: 27 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,30 @@
* Add same method signature for `Time#prev_month` and `Time#next_month`
in accordance with `Date#prev_month`, `Date#next_month`.

Allows pass argument for `Time#prev_month` and `Time#next_month`.

Before:
```
Time.new(2017, 9, 16, 17, 0).prev_month # => 2017-08-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_month(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
Time.new(2017, 9, 16, 17, 0).next_month # => 2017-10-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_month(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
```

After:
```
Time.new(2017, 9, 16, 17, 0).prev_month # => 2017-08-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_month(1) # => 2017-08-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_month # => 2017-10-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_month(1) # => 2017-10-16 17:00:00 +0300
```

*bogdanvlviv*

* Add same method signature for `Time#prev_day` and `Time#next_day`
in accordance with `Date#prev_day`, `Date#next_day`.

Expand Down
Expand Up @@ -188,9 +188,9 @@ def next_weekday
end
end

# Short-hand for months_since(1).
def next_month
months_since(1)
# Returns a new date/time the specified number of months in the future.
def next_month(months = 1)
advance(months: months)
end

# Short-hand for months_since(3)
Expand Down Expand Up @@ -223,11 +223,15 @@ def prev_weekday
end
alias_method :last_weekday, :prev_weekday

# Returns a new date/time the specified number of months ago.
def prev_month(months = 1)
advance(months: -months)
end

# Short-hand for months_ago(1).
def prev_month
def last_month
months_ago(1)
end
alias_method :last_month, :prev_month

# Short-hand for months_ago(3).
def prev_quarter
Expand Down
24 changes: 24 additions & 0 deletions activesupport/test/core_ext/date_and_time_behavior.rb
Expand Up @@ -161,6 +161,16 @@ def test_next_weekday_on_saturday
assert_equal date_time_init(2015, 1, 5, 15, 15, 10), date_time_init(2015, 1, 3, 15, 15, 10).next_weekday
end

def test_next_month
assert_equal date_time_init(2004, 12, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month(-2)
assert_equal date_time_init(2005, 1, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month(-1)
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month(0)
assert_equal date_time_init(2005, 3, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month(1)
assert_equal date_time_init(2005, 4, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month(2)
assert_equal date_time_init(2005, 3, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month
assert_equal date_time_init(2005, 4, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).next_month.next_month
end

def test_next_month_on_31st
assert_equal date_time_init(2005, 9, 30, 15, 15, 10), date_time_init(2005, 8, 31, 15, 15, 10).next_month
end
Expand Down Expand Up @@ -213,6 +223,16 @@ def test_prev_weekday_on_sunday
assert_equal date_time_init(2015, 1, 2, 15, 15, 10), date_time_init(2015, 1, 4, 15, 15, 10).prev_weekday
end

def test_prev_month
assert_equal date_time_init(2005, 4, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month(-2)
assert_equal date_time_init(2005, 3, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month(-1)
assert_equal date_time_init(2005, 2, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month(0)
assert_equal date_time_init(2005, 1, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month(1)
assert_equal date_time_init(2004, 12, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month(2)
assert_equal date_time_init(2005, 1, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month
assert_equal date_time_init(2004, 12, 22, 10, 10, 10), date_time_init(2005, 2, 22, 10, 10, 10).prev_month.prev_month
end

def test_prev_month_on_31st
assert_equal date_time_init(2004, 2, 29, 10, 10, 10), date_time_init(2004, 3, 31, 10, 10, 10).prev_month
end
Expand All @@ -225,6 +245,10 @@ def test_prev_year
assert_equal date_time_init(2004, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year
end

def test_last_month_on_31st
assert_equal date_time_init(2004, 2, 29, 0, 0, 0), date_time_init(2004, 3, 31, 0, 0, 0).last_month
end

def test_days_to_week_start
assert_equal 0, date_time_init(2011, 11, 01, 0, 0, 0).days_to_week_start(:tuesday)
assert_equal 1, date_time_init(2011, 11, 02, 0, 0, 0).days_to_week_start(:tuesday)
Expand Down
4 changes: 0 additions & 4 deletions activesupport/test/core_ext/date_ext_test.rb
Expand Up @@ -185,10 +185,6 @@ def test_next_week_in_calendar_reform
assert_equal Date.new(1582, 10, 18), Date.new(1582, 10, 4).next_week
end

def test_last_month_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
end

def test_last_quarter_on_31st
assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter
end
Expand Down
4 changes: 0 additions & 4 deletions activesupport/test/core_ext/date_time_ext_test.rb
Expand Up @@ -248,10 +248,6 @@ def test_date_time_should_have_correct_last_week_for_leap_year
assert_equal DateTime.civil(2016, 2, 29), DateTime.civil(2016, 3, 7).last_week
end

def test_last_month_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
end

def test_last_quarter_on_31st
assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter
end
Expand Down
4 changes: 0 additions & 4 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -664,10 +664,6 @@ def test_days_in_year_in_leap_year_without_year_arg
end
end

def test_last_month_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
end

def test_xmlschema_is_available
assert_nothing_raised { Time.now.xmlschema }
end
Expand Down
12 changes: 7 additions & 5 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -3019,11 +3019,9 @@ Date.new(2000, 5, 31).next_month # => Fri, 30 Jun 2000
Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
```

`prev_month` is aliased to `last_month`.

##### `prev_quarter`, `next_quarter`

Same as `prev_month` and `next_month`. It returns the date with the same day in the previous or next quarter:
`prev_quarter` and `next_quarter` return the date with the same day in the previous or next quarter:

```ruby
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
Expand Down Expand Up @@ -3175,6 +3173,8 @@ Date.new(2010, 4, 30).months_ago(2) # => Sun, 28 Feb 2010
Date.new(2009, 12, 31).months_since(2) # => Sun, 28 Feb 2010
```

`last_month` is short-hand for `#months_ago(1)`.

##### `weeks_ago`

The method `weeks_ago` works analogously for weeks:
Expand Down Expand Up @@ -3353,8 +3353,9 @@ months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
prev_month (last_month)
prev_month
next_month
last_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
beginning_of_year (at_beginning_of_year)
Expand Down Expand Up @@ -3541,8 +3542,9 @@ months_ago
months_since
beginning_of_month (at_beginning_of_month)
end_of_month (at_end_of_month)
prev_month (last_month)
prev_month
next_month
last_month
beginning_of_quarter (at_beginning_of_quarter)
end_of_quarter (at_end_of_quarter)
beginning_of_year (at_beginning_of_year)
Expand Down

0 comments on commit f2c1e3a

Please sign in to comment.