Merge pull request #30620 from bogdanvlviv/method_signature_prev-next-day-month-year_for_time
Mirror the API of Ruby stdlib for #prev_day, #next_day, #prev_month, #next_month, #prev_year, #next_year
This commit is contained in:
commit
12aa9e5b2d
|
@ -1,3 +1,84 @@
|
|||
* 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`.
|
||||
|
||||
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`.
|
||||
|
||||
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 @@ module DateAndTime
|
|||
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 @@ module DateAndTime
|
|||
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.
|
||||
|
@ -188,9 +188,9 @@ module DateAndTime
|
|||
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)
|
||||
|
@ -198,9 +198,9 @@ module DateAndTime
|
|||
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.
|
||||
|
@ -223,11 +223,15 @@ module DateAndTime
|
|||
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
|
||||
|
@ -235,11 +239,15 @@ module DateAndTime
|
|||
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
|
||||
|
|
|
@ -9,6 +9,11 @@ module DateAndTimeBehavior
|
|||
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 @@ module DateAndTimeBehavior
|
|||
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
|
||||
|
@ -151,6 +161,16 @@ module DateAndTimeBehavior
|
|||
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
|
||||
|
@ -160,7 +180,13 @@ module DateAndTimeBehavior
|
|||
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
|
||||
|
@ -203,6 +229,16 @@ module DateAndTimeBehavior
|
|||
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
|
||||
|
@ -212,7 +248,21 @@ module DateAndTimeBehavior
|
|||
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
|
||||
|
|
|
@ -120,10 +120,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
@ -185,10 +181,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
|
@ -162,10 +162,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
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)
|
||||
|
@ -248,10 +244,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
|
@ -178,10 +178,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
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)
|
||||
|
@ -664,10 +660,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
|
@ -2970,6 +2970,32 @@ Extensions to `Date`
|
|||
|
||||
NOTE: All the following methods are defined in `active_support/core_ext/date/calculations.rb`.
|
||||
|
||||
```ruby
|
||||
yesterday
|
||||
tomorrow
|
||||
beginning_of_week (at_beginning_of_week)
|
||||
end_of_week (at_end_of_week)
|
||||
monday
|
||||
sunday
|
||||
weeks_ago
|
||||
prev_week (last_week)
|
||||
next_week
|
||||
months_ago
|
||||
months_since
|
||||
beginning_of_month (at_beginning_of_month)
|
||||
end_of_month (at_end_of_month)
|
||||
last_month
|
||||
beginning_of_quarter (at_beginning_of_quarter)
|
||||
end_of_quarter (at_end_of_quarter)
|
||||
beginning_of_year (at_beginning_of_year)
|
||||
end_of_year (at_end_of_year)
|
||||
years_ago
|
||||
years_since
|
||||
last_year
|
||||
on_weekday?
|
||||
on_weekend?
|
||||
```
|
||||
|
||||
INFO: The following calculation methods have edge cases in October 1582, since days 5..14 just do not exist. This guide does not document their behavior around those days for brevity, but it is enough to say that they do what you would expect. That is, `Date.new(1582, 10, 4).tomorrow` returns `Date.new(1582, 10, 15)` and so on. Please check `test/core_ext/date_ext_test.rb` in the Active Support test suite for expected behavior.
|
||||
|
||||
#### `Date.current`
|
||||
|
@ -2980,68 +3006,6 @@ When making Date comparisons using methods which honor the user time zone, make
|
|||
|
||||
#### Named dates
|
||||
|
||||
##### `prev_year`, `next_year`
|
||||
|
||||
In Ruby 1.9 `prev_year` and `next_year` return a date with the same day/month in the last or next year:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
|
||||
d.prev_year # => Fri, 08 May 2009
|
||||
d.next_year # => Sun, 08 May 2011
|
||||
```
|
||||
|
||||
If date is the 29th of February of a leap year, you obtain the 28th:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2000, 2, 29) # => Tue, 29 Feb 2000
|
||||
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:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
|
||||
d.prev_month # => Thu, 08 Apr 2010
|
||||
d.next_month # => Tue, 08 Jun 2010
|
||||
```
|
||||
|
||||
If such a day does not exist, the last day of the corresponding month is returned:
|
||||
|
||||
```ruby
|
||||
Date.new(2000, 5, 31).prev_month # => Sun, 30 Apr 2000
|
||||
Date.new(2000, 3, 31).prev_month # => Tue, 29 Feb 2000
|
||||
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:
|
||||
|
||||
```ruby
|
||||
t = Time.local(2010, 5, 8) # => Sat, 08 May 2010
|
||||
t.prev_quarter # => Mon, 08 Feb 2010
|
||||
t.next_quarter # => Sun, 08 Aug 2010
|
||||
```
|
||||
|
||||
If such a day does not exist, the last day of the corresponding month is returned:
|
||||
|
||||
```ruby
|
||||
Time.local(2000, 7, 31).prev_quarter # => Sun, 30 Apr 2000
|
||||
Time.local(2000, 5, 31).prev_quarter # => Tue, 29 Feb 2000
|
||||
Time.local(2000, 10, 31).prev_quarter # => Mon, 30 Oct 2000
|
||||
Time.local(2000, 11, 31).next_quarter # => Wed, 28 Feb 2001
|
||||
```
|
||||
|
||||
`prev_quarter` is aliased to `last_quarter`.
|
||||
|
||||
##### `beginning_of_week`, `end_of_week`
|
||||
|
||||
The methods `beginning_of_week` and `end_of_week` return the dates for the
|
||||
|
@ -3159,6 +3123,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:
|
||||
|
@ -3175,6 +3141,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:
|
||||
|
@ -3337,35 +3305,7 @@ WARNING: `DateTime` is not aware of DST rules and so some of these methods have
|
|||
|
||||
NOTE: All the following methods are defined in `active_support/core_ext/date_time/calculations.rb`.
|
||||
|
||||
The class `DateTime` is a subclass of `Date` so by loading `active_support/core_ext/date/calculations.rb` you inherit these methods and their aliases, except that they will always return datetimes:
|
||||
|
||||
```ruby
|
||||
yesterday
|
||||
tomorrow
|
||||
beginning_of_week (at_beginning_of_week)
|
||||
end_of_week (at_end_of_week)
|
||||
monday
|
||||
sunday
|
||||
weeks_ago
|
||||
prev_week (last_week)
|
||||
next_week
|
||||
months_ago
|
||||
months_since
|
||||
beginning_of_month (at_beginning_of_month)
|
||||
end_of_month (at_end_of_month)
|
||||
prev_month (last_month)
|
||||
next_month
|
||||
beginning_of_quarter (at_beginning_of_quarter)
|
||||
end_of_quarter (at_end_of_quarter)
|
||||
beginning_of_year (at_beginning_of_year)
|
||||
end_of_year (at_end_of_year)
|
||||
years_ago
|
||||
years_since
|
||||
prev_year (last_year)
|
||||
next_year
|
||||
on_weekday?
|
||||
on_weekend?
|
||||
```
|
||||
The class `DateTime` is a subclass of `Date` so by loading `active_support/core_ext/date/calculations.rb` you inherit these methods and their aliases, except that they will always return datetimes.
|
||||
|
||||
The following methods are reimplemented so you do **not** need to load `active_support/core_ext/date/calculations.rb` for these ones:
|
||||
|
||||
|
@ -3513,8 +3453,6 @@ Extensions to `Time`
|
|||
|
||||
NOTE: All the following methods are defined in `active_support/core_ext/time/calculations.rb`.
|
||||
|
||||
Active Support adds to `Time` many of the methods available for `DateTime`:
|
||||
|
||||
```ruby
|
||||
past?
|
||||
today?
|
||||
|
@ -3526,6 +3464,8 @@ change
|
|||
advance
|
||||
ago
|
||||
since (in)
|
||||
prev_day
|
||||
next_day
|
||||
beginning_of_day (midnight, at_midnight, at_beginning_of_day)
|
||||
end_of_day
|
||||
beginning_of_hour (at_beginning_of_hour)
|
||||
|
@ -3541,15 +3481,17 @@ 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)
|
||||
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?
|
||||
|
@ -3607,6 +3549,74 @@ now.all_year
|
|||
# => Fri, 01 Jan 2010 00:00:00 UTC +00:00..Fri, 31 Dec 2010 23:59:59 UTC +00:00
|
||||
```
|
||||
|
||||
#### `prev_day`, `next_day`
|
||||
|
||||
In Ruby 1.9 `prev_day` and `next_day` return the date in the last or next day:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
|
||||
d.prev_day # => Fri, 07 May 2010
|
||||
d.next_day # => Sun, 09 May 2010
|
||||
```
|
||||
|
||||
#### `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:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
|
||||
d.prev_month # => Thu, 08 Apr 2010
|
||||
d.next_month # => Tue, 08 Jun 2010
|
||||
```
|
||||
|
||||
If such a day does not exist, the last day of the corresponding month is returned:
|
||||
|
||||
```ruby
|
||||
Date.new(2000, 5, 31).prev_month # => Sun, 30 Apr 2000
|
||||
Date.new(2000, 3, 31).prev_month # => Tue, 29 Feb 2000
|
||||
Date.new(2000, 5, 31).next_month # => Fri, 30 Jun 2000
|
||||
Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
|
||||
```
|
||||
|
||||
#### `prev_year`, `next_year`
|
||||
|
||||
In Ruby 1.9 `prev_year` and `next_year` return a date with the same day/month in the last or next year:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
|
||||
d.prev_year # => Fri, 08 May 2009
|
||||
d.next_year # => Sun, 08 May 2011
|
||||
```
|
||||
|
||||
If date is the 29th of February of a leap year, you obtain the 28th:
|
||||
|
||||
```ruby
|
||||
d = Date.new(2000, 2, 29) # => Tue, 29 Feb 2000
|
||||
d.prev_year # => Sun, 28 Feb 1999
|
||||
d.next_year # => Wed, 28 Feb 2001
|
||||
```
|
||||
|
||||
#### `prev_quarter`, `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) # => 2010-05-08 00:00:00 +0300
|
||||
t.prev_quarter # => 2010-02-08 00:00:00 +0200
|
||||
t.next_quarter # => 2010-08-08 00:00:00 +0300
|
||||
```
|
||||
|
||||
If such a day does not exist, the last day of the corresponding month is returned:
|
||||
|
||||
```ruby
|
||||
Time.local(2000, 7, 31).prev_quarter # => 2000-04-30 00:00:00 +0300
|
||||
Time.local(2000, 5, 31).prev_quarter # => 2000-02-29 00:00:00 +0200
|
||||
Time.local(2000, 10, 31).prev_quarter # => 2000-07-31 00:00:00 +0300
|
||||
Time.local(2000, 11, 31).next_quarter # => 2001-03-01 00:00:00 +0200
|
||||
```
|
||||
|
||||
`prev_quarter` is aliased to `last_quarter`.
|
||||
|
||||
### Time Constructors
|
||||
|
||||
Active Support defines `Time.current` to be `Time.zone.now` if there's a user time zone defined, with fallback to `Time.now`:
|
||||
|
|
Loading…
Reference in New Issue