Class: InputSanitizer::V1::TimeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/input_sanitizer/v1/default_converters.rb

Constant Summary collapse

ISO_RE =
/\A\d{4}-?\d{2}-?\d{2}([T ]?\d{2}(:?\d{2}(:?\d{2}((\.)?\d{0,3}(Z)?)?)?)?)?\Z/

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/input_sanitizer/v1/default_converters.rb', line 35

def call(value)
  case value
  when Time
    value.getutc
  when String
    if value =~ ISO_RE
      strip_timezone(Time.parse(value))
    else
      raise InputSanitizer::ConversionError.new("invalid time")
    end
  else
    raise InputSanitizer::ConversionError.new("invalid time")
  end
rescue ArgumentError
  raise InputSanitizer::ConversionError.new("invalid time")
end

#strip_timezone(time) ⇒ Object



52
53
54
# File 'lib/input_sanitizer/v1/default_converters.rb', line 52

def strip_timezone(time)
  Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec)
end