Permalink
Browse files

Allows pass argument for `Time#prev_year` and `Time#next_year`.

  • Loading branch information...
bogdanvlviv committed Sep 16, 2017
1 parent f2c1e3a commit ee9d81837b5eba9d5ec869ae7601d7ffce763e3e
@@ -1,3 +1,30 @@
* Add same method signature for `Time#prev_year` and `Time#next_year`
in accordance with `Date#prev_year`, `Date#next_year`.
Allows pass argument for `Time#prev_year` and `Time#next_year`.
Before:
```
Time.new(2017, 9, 16, 17, 0).prev_year # => 2016-09-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_year(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
Time.new(2017, 9, 16, 17, 0).next_year # => 2018-09-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_year(1)
# => ArgumentError: wrong number of arguments (given 1, expected 0)
```
After:
```
Time.new(2017, 9, 16, 17, 0).prev_year # => 2016-09-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).prev_year(1) # => 2016-09-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_year # => 2018-09-16 17:00:00 +0300
Time.new(2017, 9, 16, 17, 0).next_year(1) # => 2018-09-16 17:00:00 +0300
```
*bogdanvlviv*
* Add same method signature for `Time#prev_month` and `Time#next_month`
in accordance with `Date#prev_month`, `Date#next_month`.
@@ -198,9 +198,9 @@ def next_quarter
months_since(3)
end
# Short-hand for years_since(1).
def next_year
years_since(1)
# Returns a new date/time the specified number of years in the future.
def next_year(years = 1)
advance(years: years)
end
# Returns a new date/time representing the given day in the previous week.
@@ -239,11 +239,15 @@ def prev_quarter
end
alias_method :last_quarter, :prev_quarter
# Returns a new date/time the specified number of years ago.
def prev_year(years = 1)
advance(years: -years)
end
# Short-hand for years_ago(1).
def prev_year
def last_year
years_ago(1)
end
alias_method :last_year, :prev_year
# Returns the number of days to the start of the week on the given day.
# Week is assumed to start on +start_day+, default is
@@ -180,7 +180,13 @@ def test_next_quarter_on_31st
end
def test_next_year
assert_equal date_time_init(2003, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year(-2)
assert_equal date_time_init(2004, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year(-1)
assert_equal date_time_init(2005, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year(0)
assert_equal date_time_init(2006, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year(1)
assert_equal date_time_init(2007, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year(2)
assert_equal date_time_init(2006, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year
assert_equal date_time_init(2007, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).next_year.next_year
end
def test_prev_week
@@ -242,13 +248,23 @@ def test_prev_quarter_on_31st
end
def test_prev_year
assert_equal date_time_init(2007, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year(-2)
assert_equal date_time_init(2006, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year(-1)
assert_equal date_time_init(2005, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year(0)
assert_equal date_time_init(2004, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year(1)
assert_equal date_time_init(2003, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year(2)
assert_equal date_time_init(2004, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year
assert_equal date_time_init(2003, 6, 5, 10, 10, 10), date_time_init(2005, 6, 5, 10, 10, 10).prev_year.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_last_year
assert_equal date_time_init(2004, 6, 5, 10, 0, 0), date_time_init(2005, 6, 5, 10, 0, 0).last_year
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)
@@ -120,10 +120,6 @@ def test_prev_year_in_calendar_reform
assert_equal Date.new(1582, 10, 4), Date.new(1583, 10, 14).prev_year
end
def test_last_year
assert_equal Date.new(2004, 6, 5), Date.new(2005, 6, 5).last_year
end
def test_last_year_in_leap_years
assert_equal Date.new(1999, 2, 28), Date.new(2000, 2, 29).last_year
end
@@ -162,10 +162,6 @@ def test_end_of_month
assert_equal DateTime.civil(2005, 4, 30, 23, 59, Rational(59999999999, 1000000000)), DateTime.civil(2005, 4, 20, 10, 10, 10).end_of_month
end
def test_last_year
assert_equal DateTime.civil(2004, 6, 5, 10), DateTime.civil(2005, 6, 5, 10, 0, 0).last_year
end
def test_ago
assert_equal DateTime.civil(2005, 2, 22, 10, 10, 9), DateTime.civil(2005, 2, 22, 10, 10, 10).ago(1)
assert_equal DateTime.civil(2005, 2, 22, 9, 10, 10), DateTime.civil(2005, 2, 22, 10, 10, 10).ago(3600)
@@ -178,10 +178,6 @@ def test_end_of_minute
assert_equal Time.local(2005, 2, 4, 19, 30, 59, Rational(999999999, 1000)), Time.local(2005, 2, 4, 19, 30, 10).end_of_minute
end
def test_last_year
assert_equal Time.local(2004, 6, 5, 10), Time.local(2005, 6, 5, 10, 0, 0).last_year
end
def test_ago
assert_equal Time.local(2005, 2, 22, 10, 10, 9), Time.local(2005, 2, 22, 10, 10, 10).ago(1)
assert_equal Time.local(2005, 2, 22, 9, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).ago(3600)
@@ -2998,8 +2998,6 @@ d.prev_year # => Sun, 28 Feb 1999
d.next_year # => Wed, 28 Feb 2001
```
`prev_year` is aliased to `last_year`.
##### `prev_month`, `next_month`
In Ruby 1.9 `prev_month` and `next_month` return the date with the same day in the last or next month:
@@ -3157,6 +3155,8 @@ Date.new(2012, 2, 29).years_ago(3) # => Sat, 28 Feb 2009
Date.new(2012, 2, 29).years_since(3) # => Sat, 28 Feb 2015
```
`last_year` is short-hand for `#years_ago(1)`.
##### `months_ago`, `months_since`
The methods `months_ago` and `months_since` work analogously for months:
@@ -3362,7 +3362,8 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
prev_year (last_year)
prev_year
last_year
next_year
on_weekday?
on_weekend?
@@ -3551,7 +3552,8 @@ beginning_of_year (at_beginning_of_year)
end_of_year (at_end_of_year)
years_ago
years_since
prev_year (last_year)
prev_year
last_year
next_year
on_weekday?
on_weekend?

0 comments on commit ee9d818

Please sign in to comment.