Module: ApiResource::Typecast::TimeTypecaster

Defined in:
lib/api_resource/typecasters/time_typecaster.rb

Class Method Summary collapse

Class Method Details

.from_api(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/api_resource/typecasters/time_typecaster.rb', line 11

def self.from_api(value)
  return value if value.is_a?(Time)
  value = value.to_s
  return nil if value.empty?

  if value =~ ApiResource::Typecast::ISO_DATETIME
    micro = ($7.to_f * 1_000_000).to_i
    return self.new_time(false, $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, micro)
  end

  time_info = Date._parse(value)
  time_info[:micro] = ((time_info[:sec_fraction].to_f % 1) * 1_000_000).to_i

  if time_info[:zone].present? && time_info[:zone] =~ /^(\+|\-)[0-9]{1,2}:[0-9]{2}$/
    new_time(true, *time_info.values_at(:year, :mon, :mday, :hour, :min, :sec, :zone))
  else
    new_time(false, *time_info.values_at(:year, :mon, :mday, :hour, :min, :sec, :micro))
  end

end

.to_api(value) ⇒ Object



32
33
34
# File 'lib/api_resource/typecasters/time_typecaster.rb', line 32

def self.to_api(value)
  return value.to_s
end