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

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#apply_seconds_precision(value) ⇒ Object


33
34
35
36
37
38
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 33

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 / round_power * round_power)
end

#default_timezoneObject


25
26
27
28
29
30
31
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 25

def default_timezone
  if is_utc?
    :utc
  else
    :local
  end
end

#is_utc?Boolean

Returns:


21
22
23
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 21

def is_utc?
  ::Time.zone_default.nil? || ::Time.zone_default =~ 'UTC'
end

#serialize(value) ⇒ Object


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

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


40
41
42
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 40

def type_cast_for_schema(value)
  "'#{value.to_s(:db)}'"
end

#user_input_in_time_zone(value) ⇒ Object


44
45
46
# File 'activemodel/lib/active_model/type/helpers/time_value.rb', line 44

def user_input_in_time_zone(value)
  value.in_time_zone
end