Skip to content

Commit 0bf4537

Browse files
authored
opt out of truncating times (#72)
1 parent 6aeeb22 commit 0bf4537

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

lib/subroutine.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ def self.inheritable_field_options
3333
@inheritable_field_options ||= %i[mass_assignable field_reader field_writer groups aka]
3434
end
3535

36+
def self.preserve_time_precision=(bool)
37+
@preserve_time_precision = !!bool
38+
end
39+
40+
def self.preserve_time_precision?
41+
!!@preserve_time_precision
42+
end
43+
3644
end

lib/subroutine/type_caster.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'time'
55
require 'bigdecimal'
66
require 'securerandom'
7+
require 'active_support/json'
78
require 'active_support/core_ext/date_time/acts_like'
89
require 'active_support/core_ext/date_time/calculations'
910
require '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)
115116
end
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)
141137
end
142138

143139
::Subroutine::TypeCaster.register :hash, :object, :hashmap, :dict do |value, _options = {}|

test/subroutine/type_caster_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,11 @@ def test_iso_time_inputs
438438

439439
op.iso_time_input = '2022-12-22T10:30:24Z'
440440
assert_equal ::String, op.iso_time_input.class
441-
assert_equal '2022-12-22T10:30:24Z', op.iso_time_input
441+
assert_equal '2022-12-22T10:30:24.000Z', op.iso_time_input
442442

443-
op.iso_time_input = Time.parse('2022-12-22T10:30:24Z')
443+
op.iso_time_input = Time.parse('2022-12-22T10:30:24.123456Z')
444444
assert_equal ::String, op.iso_time_input.class
445-
assert_equal '2022-12-22T10:30:24Z', op.iso_time_input
445+
assert_equal '2022-12-22T10:30:24.123Z', op.iso_time_input
446446
end
447447

448448
def test_file_inputs

0 commit comments

Comments
 (0)