Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9390 from chris-baynes/datetime_sec_fraction

Keep second fraction when DateTime#change is called.
  • Loading branch information...
commit d5141f208e10378ebe3195fa7a9ff523a1296461 2 parents f80a6a5 + fa891a2
@pixeltrix pixeltrix authored
View
5 activesupport/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Prevent `DateTime#change` from truncating the second fraction, when seconds
+ do not need to be changed.
+
+ *Chris Baynes*
+
* Added `ActiveSupport::TimeWithZone#to_r` for `Time#at` compatibility.
Before this change:
View
2  activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -59,7 +59,7 @@ def change(options)
options.fetch(:day, day),
options.fetch(:hour, hour),
options.fetch(:min, options[:hour] ? 0 : min),
- options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
+ options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec + sec_fraction),
options.fetch(:offset, offset),
options.fetch(:start, start)
)
View
3  activesupport/test/core_ext/date_time_ext_test.rb
@@ -129,6 +129,9 @@ def test_change
assert_equal DateTime.civil(2005,2,22,16), DateTime.civil(2005,2,22,15,15,10).change(:hour => 16)
assert_equal DateTime.civil(2005,2,22,16,45), DateTime.civil(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
assert_equal DateTime.civil(2005,2,22,15,45), DateTime.civil(2005,2,22,15,15,10).change(:min => 45)
+
+ # datetime with fractions of a second
+ assert_equal DateTime.civil(2005,2,1,15,15,10.7), DateTime.civil(2005,2,22,15,15,10.7).change(:day => 1)
end
def test_advance
Please sign in to comment.
Something went wrong with that request. Please try again.