mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Time durations use since instead of + for accuracy. Closes #8513.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6901 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
a1c88273f4
commit
6503da6991
5 changed files with 23 additions and 3 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Time durations use since instead of + for accuracy. #8513 [Geoff Buesing]
|
||||
|
||||
* escape <'s and >'s in JSON strings. #8371 [Rick]
|
||||
|
||||
* Inflections: MatrixTest -> MatrixTests instead of MatricesTest. #8496 [jbwiv]
|
||||
|
|
|
@ -45,7 +45,7 @@ module ActiveSupport #:nodoc:
|
|||
# Returns a new DateTime representing the time a number of seconds since the instance time
|
||||
# Do not use this method in combination with x.months, use months_since instead!
|
||||
def since(seconds)
|
||||
self + Rational(seconds, 86400)
|
||||
self + Rational(seconds.round, 86400)
|
||||
end
|
||||
alias :in :since
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ module ActiveSupport
|
|||
parts.inject(time) do |t,(type,number)|
|
||||
if t.acts_like?(:time)
|
||||
if type == :seconds
|
||||
t + (sign * number)
|
||||
t.since(sign * number)
|
||||
else
|
||||
t.advance(type => sign * number)
|
||||
end
|
||||
|
|
|
@ -112,6 +112,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
|
|||
assert_equal DateTime.civil(2005,2,22,11,10,10), DateTime.civil(2005,2,22,10,10,10).since(3600)
|
||||
assert_equal DateTime.civil(2005,2,24,10,10,10), DateTime.civil(2005,2,22,10,10,10).since(86400*2)
|
||||
assert_equal DateTime.civil(2005,2,24,11,10,35), DateTime.civil(2005,2,22,10,10,10).since(86400*2 + 3600 + 25)
|
||||
assert_equal DateTime.civil(2005,2,22,10,10,11), DateTime.civil(2005,2,22,10,10,10).since(1.333)
|
||||
assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667)
|
||||
end
|
||||
|
||||
def test_yesterday
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
require File.dirname(__FILE__) + '/../abstract_unit'
|
||||
|
||||
class NumericExtTimeTest < Test::Unit::TestCase
|
||||
class NumericExtTimeAndDateTimeTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@now = Time.now
|
||||
@dtnow = DateTime.now
|
||||
@seconds = {
|
||||
1.minute => 60,
|
||||
10.minutes => 600,
|
||||
|
@ -40,12 +41,19 @@ class NumericExtTimeTest < Test::Unit::TestCase
|
|||
assert_equal @now.advance(:months => 1), 1.month.since(@now)
|
||||
assert_equal @now.advance(:months => -1), 1.month.until(@now)
|
||||
assert_equal @now.advance(:years => 20), 20.years.since(@now)
|
||||
assert_equal @dtnow.advance(:days => 3000), 3000.days.since(@dtnow)
|
||||
assert_equal @dtnow.advance(:months => 1), 1.month.since(@dtnow)
|
||||
assert_equal @dtnow.advance(:months => -1), 1.month.until(@dtnow)
|
||||
assert_equal @dtnow.advance(:years => 20), 20.years.since(@dtnow)
|
||||
end
|
||||
|
||||
def test_duration_addition
|
||||
assert_equal @now.advance(:days => 1, :months => 1), (1.day + 1.month).since(@now)
|
||||
assert_equal @now.advance(:days => 7), (1.week + 5.seconds - 5.seconds).since(@now)
|
||||
assert_equal @now.advance(:years => 2), (4.years - 2.years).since(@now)
|
||||
assert_equal @dtnow.advance(:days => 1, :months => 1), (1.day + 1.month).since(@dtnow)
|
||||
assert_equal @dtnow.advance(:days => 7), (1.week + 5.seconds - 5.seconds).since(@dtnow)
|
||||
assert_equal @dtnow.advance(:years => 2), (4.years - 2.years).since(@dtnow)
|
||||
end
|
||||
|
||||
def test_time_plus_duration
|
||||
|
@ -53,16 +61,24 @@ class NumericExtTimeTest < Test::Unit::TestCase
|
|||
assert_equal @now + 22.9, @now + 22.9.seconds
|
||||
assert_equal @now.advance(:days => 15), @now + 15.days
|
||||
assert_equal @now.advance(:months => 1), @now + 1.month
|
||||
assert_equal @dtnow.since(8), @dtnow + 8.seconds
|
||||
assert_equal @dtnow.since(22.9), @dtnow + 22.9.seconds
|
||||
assert_equal @dtnow.advance(:days => 15), @dtnow + 15.days
|
||||
assert_equal @dtnow.advance(:months => 1), @dtnow + 1.month
|
||||
end
|
||||
|
||||
def test_chaining_duration_operations
|
||||
assert_equal @now.advance(:days => 2).advance(:months => -3), @now + 2.days - 3.months
|
||||
assert_equal @now.advance(:days => 1).advance(:months => 2), @now + 1.day + 2.months
|
||||
assert_equal @dtnow.advance(:days => 2).advance(:months => -3), @dtnow + 2.days - 3.months
|
||||
assert_equal @dtnow.advance(:days => 1).advance(:months => 2), @dtnow + 1.day + 2.months
|
||||
end
|
||||
|
||||
def test_duration_after_convertion_is_no_longer_accurate
|
||||
assert_equal 30.days.to_i.since(@now), 1.month.to_i.since(@now)
|
||||
assert_equal 365.25.days.to_f.since(@now), 1.year.to_f.since(@now)
|
||||
assert_equal 30.days.to_i.since(@dtnow), 1.month.to_i.since(@dtnow)
|
||||
assert_equal 365.25.days.to_f.since(@dtnow), 1.year.to_f.since(@dtnow)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue