`DateTime#to_f` now preserves fractional seconds.

Previously this method always returns `.0` in the fractional part. This commit
changes it to preserve the fractional seconds instead.

Fixes #15994.
This commit is contained in:
John Paul Ashenfelter 2014-07-01 09:43:46 -04:00 committed by Godfrey Chan
parent a4104278b5
commit d75992b0b2
3 changed files with 10 additions and 2 deletions

View File

@ -1,3 +1,10 @@
* `DateTime#to_f` now preserves the fractional seconds instead of always
rounding to `.0`.
Fixes #15994.
*John Paul Ashenfelter*
* Add `Hash#transform_values` to simplify a common pattern where the values of a
hash must change, but the keys are left the same.

View File

@ -71,9 +71,9 @@ class DateTime
civil(year, month, day, hour, min, sec, offset)
end
# Converts +self+ to a floating-point number of seconds since the Unix epoch.
# Converts +self+ to a floating-point number of seconds, including fractional microseconds, since the Unix epoch.
def to_f
seconds_since_unix_epoch.to_f
seconds_since_unix_epoch.to_f + sec_fraction
end
# Converts +self+ to an integer number of seconds since the Unix epoch.

View File

@ -338,6 +338,7 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
def test_to_f
assert_equal 946684800.0, DateTime.civil(2000).to_f
assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f
assert_equal 946684800.5, DateTime.civil(1999,12,31,19,0,0.5,Rational(-5,24)).to_f
end
def test_to_i