diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-30 06:21:08 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-30 06:21:08 +0000 |
commit | 6503da69910beb382b18127c51cd318631245607 (patch) | |
tree | 4095ad542ea7e0ba2e760430ea9a1f52a763be88 /activesupport | |
parent | a1c88273f4321e203ccf389ccff8a8e043c4f8f9 (diff) | |
download | rails-6503da69910beb382b18127c51cd318631245607.tar.gz rails-6503da69910beb382b18127c51cd318631245607.tar.bz2 rails-6503da69910beb382b18127c51cd318631245607.zip |
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
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/calculations.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/numeric_ext_test.rb | 18 |
5 files changed, 23 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 6e6eb1547d..ca8d43903a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -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] diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index dd30dd07bc..3e62891fcf 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -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 diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 5279c4435a..721bc18795 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -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 diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 1b7a091635..0aacf123c3 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -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 diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 402a255b51..ba6a646fad 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -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 |