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
# 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($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

  new_time(*time_info.values_at(:year, :mon, :mday, :hour, :min, :sec, :micro))

end

.to_api(value) ⇒ Object



28
29
30
# File 'lib/api_resource/typecasters/time_typecaster.rb', line 28

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