diff options
-rw-r--r-- | activemodel/lib/active_model/type/helpers/time_value.rb | 13 |
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) |