mirror of
https://github.com/github/rails.git
synced 2026-01-31 01:08:19 -05:00
Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. Closes #8575.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6937 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. #8575 [Geoff Buesing]
|
||||
|
||||
* String#to_time overflows to DateTime. Add String#to_datetime. #8572 [Geoff Buesing]
|
||||
|
||||
* Date.yesterday and .tomorrow. #8571 [Geoff Buesing]
|
||||
|
||||
@@ -23,6 +23,32 @@ module ActiveSupport #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
||||
# and then subtracts the specified number of seconds
|
||||
def ago(seconds)
|
||||
to_time.since(-seconds)
|
||||
end
|
||||
|
||||
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
||||
# and then adds the specified number of seconds
|
||||
def since(seconds)
|
||||
to_time.since(seconds)
|
||||
end
|
||||
alias :in :since
|
||||
|
||||
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
||||
def beginning_of_day
|
||||
to_time
|
||||
end
|
||||
alias :midnight :beginning_of_day
|
||||
alias :at_midnight :beginning_of_day
|
||||
alias :at_beginning_of_day :beginning_of_day
|
||||
|
||||
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
||||
def end_of_day
|
||||
to_time.end_of_day
|
||||
end
|
||||
|
||||
def plus_with_duration(other) #:nodoc:
|
||||
if ActiveSupport::Duration === other
|
||||
other.since(self)
|
||||
|
||||
@@ -65,15 +65,12 @@ module ActiveSupport
|
||||
|
||||
def sum(sign, time = ::Time.now) #:nodoc:
|
||||
parts.inject(time) do |t,(type,number)|
|
||||
if t.acts_like?(:time)
|
||||
if t.acts_like?(:time) || t.acts_like?(:date)
|
||||
if type == :seconds
|
||||
t.since(sign * number)
|
||||
else
|
||||
t.advance(type => sign * number)
|
||||
end
|
||||
elsif t.acts_like?(:date)
|
||||
raise ArgumentError, "Adding seconds to a Date does not make sense" if type == :seconds
|
||||
t.advance(type => sign * number)
|
||||
else
|
||||
raise ArgumentError, "expected a time or date, got #{time.inspect}"
|
||||
end
|
||||
|
||||
@@ -139,4 +139,20 @@ class DateExtCalculationsTest < Test::Unit::TestCase
|
||||
def test_tomorrow_constructor
|
||||
assert_equal Date.today + 1, Date.tomorrow
|
||||
end
|
||||
|
||||
def test_since
|
||||
assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45)
|
||||
end
|
||||
|
||||
def test_ago
|
||||
assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45)
|
||||
end
|
||||
|
||||
def test_beginning_of_day
|
||||
assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day
|
||||
end
|
||||
|
||||
def test_end_of_day
|
||||
assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day
|
||||
end
|
||||
end
|
||||
|
||||
@@ -90,7 +90,9 @@ class NumericExtDateTest < Test::Unit::TestCase
|
||||
def test_date_plus_duration
|
||||
assert_equal @today + 1, @today + 1.day
|
||||
assert_equal @today >> 1, @today + 1.month
|
||||
assert_raises(ArgumentError) { @today + 1.second }
|
||||
assert_equal @today.to_time.since(1), @today + 1.second
|
||||
assert_equal @today.to_time.since(60), @today + 1.minute
|
||||
assert_equal @today.to_time.since(60*60), @today + 1.hour
|
||||
end
|
||||
|
||||
def test_chaining_duration_operations
|
||||
|
||||
Reference in New Issue
Block a user