Module: ActiveModel::Type::Helpers::TimeValue

Included in:
DateTime, Time
Defined in:
activemodel/lib/active_model/type/helpers/time_value.rb

Instance Method Summary collapse

Instance Method Details

#apply_seconds_precision(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 24

def apply_seconds_precision(value)
  return value unless precision && value.respond_to?(:nsec)

  number_of_insignificant_digits = 9 - precision
  round_power = 10**number_of_insignificant_digits
  rounded_off_nsec = value.nsec % round_power

  if rounded_off_nsec > 0
    value.change(nsec: value.nsec - rounded_off_nsec)
  else
    value
  end
end

#serialize(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 10

def serialize(value)
  value = apply_seconds_precision(value)

  if value.acts_like?(:time)
    if is_utc?
      value = value.getutc if value.respond_to?(:getutc) && !value.utc?
    else
      value = value.getlocal if value.respond_to?(:getlocal)
    end
  end

  value
end

#type_cast_for_schema(value) ⇒ Object



38
39
40
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 38

def type_cast_for_schema(value)
  value.to_s(:db).inspect
end

#user_input_in_time_zone(value) ⇒ Object



42
43
44
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 42

def user_input_in_time_zone(value)
  value.in_time_zone
end