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
36 37 38 39 40 41 |
# File 'lib/active_model/type/helpers/time_value.rb', line 36 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
28 29 30 31 32 33 34 |
# File 'lib/active_model/type/helpers/time_value.rb', line 28 def default_timezone if is_utc? :utc else :local end end |
#is_utc? ⇒ Boolean
24 25 26 |
# File 'lib/active_model/type/helpers/time_value.rb', line 24 def is_utc? ::Time.zone_default.nil? || ::Time.zone_default =~ "UTC" end |
#serialize(value) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_model/type/helpers/time_value.rb', line 10 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
43 44 45 |
# File 'lib/active_model/type/helpers/time_value.rb', line 43 def type_cast_for_schema(value) value.to_s(:db).inspect end |
#user_input_in_time_zone(value) ⇒ Object
47 48 49 |
# File 'lib/active_model/type/helpers/time_value.rb', line 47 def user_input_in_time_zone(value) value.in_time_zone end |