Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #16001 from johnpaulashenfelter/jp_fix_datetime_to_f

`DateTime#to_f` now preserves fractional seconds.
  • Loading branch information...
commit 6ee17968db8f8a078e843cb97eb5ab62e3d021d5 2 parents a410427 + d75992b
@chancancode chancancode authored
View
7 activesupport/CHANGELOG.md
@@ -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
4 activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -71,9 +71,9 @@ def self.civil_from_format(utc_or_local, year, month=1, day=1, hour=0, min=0, se
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
1  activesupport/test/core_ext/date_time_ext_test.rb
@@ -338,6 +338,7 @@ def test_compare_with_time_with_zone
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
Please sign in to comment.
Something went wrong with that request. Please try again.