aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-07-02 15:02:22 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2014-07-02 15:02:22 -0700
commit6ee17968db8f8a078e843cb97eb5ab62e3d021d5 (patch)
tree94f8360a188fe2b6aae286d0962913efb3f3ddc8
parenta4104278b5cc5a7e7b15473c1ea74125f915f2db (diff)
parentd75992b0b22b656d16a28c04eac771a93486f465 (diff)
downloadrails-6ee17968db8f8a078e843cb97eb5ab62e3d021d5.tar.gz
rails-6ee17968db8f8a078e843cb97eb5ab62e3d021d5.tar.bz2
rails-6ee17968db8f8a078e843cb97eb5ab62e3d021d5.zip
Merge pull request #16001 from johnpaulashenfelter/jp_fix_datetime_to_f
`DateTime#to_f` now preserves fractional seconds.
-rw-r--r--activesupport/CHANGELOG.md7
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/conversions.rb4
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb1
3 files changed, 10 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index efaa6cd2a3..005bcffa26 100644
--- a/activesupport/CHANGELOG.md
+++ b/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.
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
index 6ddfb72a0d..2a9c09fc29 100644
--- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -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.
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index 2c08b46791..74319ecd09 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -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