Module: ActiveModel::Type::Helpers::TimeValue
Instance Method Summary collapse
- #apply_seconds_precision(value) ⇒ Object
- #default_timezone ⇒ Object
- #is_utc? ⇒ Boolean
- #serialize(value) ⇒ Object
- #type_cast_for_schema(value) ⇒ Object
- #user_input_in_time_zone(value) ⇒ Object
Instance Method Details
#apply_seconds_precision(value) ⇒ Object
35 36 37 38 39 40 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 35 def apply_seconds_precision(value) return value unless precision && value.respond_to?(:usec) number_of_insignificant_digits = 6 - precision round_power = 10**number_of_insignificant_digits value.change(usec: value.usec - value.usec % round_power) end |
#default_timezone ⇒ Object
27 28 29 30 31 32 33 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 27 def default_timezone if is_utc? :utc else :local end end |
#is_utc? ⇒ Boolean
23 24 25 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 23 def is_utc? ::Time.zone_default.nil? || ::Time.zone_default =~ "UTC" end |
#serialize(value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 9 def serialize(value) value = apply_seconds_precision(value) if value.acts_like?(:time) zone_conversion_method = is_utc? ? :getutc : :getlocal if value.respond_to?(zone_conversion_method) value = value.send(zone_conversion_method) end end value end |
#type_cast_for_schema(value) ⇒ Object
42 43 44 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 42 def type_cast_for_schema(value) value.to_s(:db).inspect end |
#user_input_in_time_zone(value) ⇒ Object
46 47 48 |
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 46 def user_input_in_time_zone(value) value.in_time_zone end |