44require 'time'
55require 'bigdecimal'
66require 'securerandom'
7+ require 'active_support/json'
78require 'active_support/core_ext/date_time/acts_like'
89require 'active_support/core_ext/date_time/calculations'
910require 'active_support/core_ext/object/acts_like'
@@ -111,7 +112,7 @@ def self.cast(value, options = {})
111112 t ||= value if value . is_a? ( ::Time )
112113 t ||= value if value . try ( :acts_like? , :time )
113114 t ||= ::Time . parse ( String ( value ) )
114- t . utc . iso8601
115+ t . utc . iso8601 ( :: ActiveSupport :: JSON :: Encoding . time_precision )
115116end
116117
117118::Subroutine ::TypeCaster . register :date do |value , _options = { } |
@@ -123,21 +124,16 @@ def self.cast(value, options = {})
123124::Subroutine ::TypeCaster . register :time , :timestamp , :datetime do |value , options = { } |
124125 next nil unless value . present?
125126
126- if options [ :precision ] == :high
127- if value . try ( :acts_like? , :time )
128- value . to_time
129- else
130- ::Time . parse ( String ( value ) )
131- end
132- else # precision == :seconds
133- time = if value . try ( :acts_like? , :time )
134- value . to_time
135- else
136- ::Time . parse ( String ( value ) )
137- end
138-
139- time . change ( usec : 0 )
127+ value = if value . try ( :acts_like? , :time )
128+ value . to_time
129+ else
130+ ::Time . parse ( String ( value ) )
140131 end
132+
133+ # High precision must be opted into. The original implementation is to set usec:0
134+ next value if options [ :precision ] == :high || ::Subroutine . preserve_time_precision?
135+
136+ value . change ( usec : 0 )
141137end
142138
143139::Subroutine ::TypeCaster . register :hash , :object , :hashmap , :dict do |value , _options = { } |
0 commit comments