aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-05-28 23:58:43 +0900
committerGitHub <noreply@github.com>2019-05-28 23:58:43 +0900
commitfc530d866809403ed9259028336db8103b00ac62 (patch)
treeaaabf1aaa2f306f71ee82920aeb557cef0b1f879
parent17afd4b982f32761f1725b07cda37b7db03dbb33 (diff)
parent497e52f8c2d9d5bda485df5d1ff396935d23e43f (diff)
downloadrails-fc530d866809403ed9259028336db8103b00ac62.tar.gz
rails-fc530d866809403ed9259028336db8103b00ac62.tar.bz2
rails-fc530d866809403ed9259028336db8103b00ac62.zip
Merge pull request #36352 from kamipo/fast_pluck_datetime
Don't round off subseconds unless necessary
-rw-r--r--activemodel/lib/active_model/type/helpers/time_value.rb13
1 files changed, 10 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/type/helpers/time_value.rb b/activemodel/lib/active_model/type/helpers/time_value.rb
index 735b9a75a6..dab196d653 100644
--- a/activemodel/lib/active_model/type/helpers/time_value.rb
+++ b/activemodel/lib/active_model/type/helpers/time_value.rb
@@ -22,10 +22,17 @@ module ActiveModel
end
def apply_seconds_precision(value)
- return value unless precision && value.respond_to?(:usec)
- number_of_insignificant_digits = 6 - precision
+ return value unless precision && value.respond_to?(:nsec)
+
+ number_of_insignificant_digits = 9 - precision
round_power = 10**number_of_insignificant_digits
- value.change(usec: value.usec - value.usec % round_power)
+ rounded_off_nsec = value.nsec % round_power
+
+ if rounded_off_nsec > 0
+ value.change(nsec: value.nsec - rounded_off_nsec)
+ else
+ value
+ end
end
def type_cast_for_schema(value)