Permalink
Browse files

Merge pull request #9390 from chris-baynes/datetime_sec_fraction

Keep second fraction when DateTime#change is called.
  • Loading branch information...
pixeltrix committed Feb 25, 2013
2 parents f80a6a5 + fa891a2 commit d5141f208e10378ebe3195fa7a9ff523a1296461
@@ -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:
@@ -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)
)
@@ -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

0 comments on commit d5141f2

Please sign in to comment.