Permalink
Browse files

Keep second fraction when DateTime#change is called.

  • Loading branch information...
1 parent f80a6a5 commit fa891a2b9dd5548d17ce3609d64142b6e31d2410 @chris-baynes chris-baynes committed Feb 23, 2013
@@ -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 fa891a2

Please sign in to comment.