Class: FlatKit::FieldType::TimestampType

Inherits:
FlatKit::FieldType show all
Defined in:
lib/flat_kit/field_type/timestamp_type.rb

Constant Summary

Constants inherited from FlatKit::FieldType

CoerceFailure

Class Method Summary collapse

Methods inherited from FlatKit::FieldType

best_guess, candidate_types, weight

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Class Method Details

.coerce(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flat_kit/field_type/timestamp_type.rb', line 27

def self.coerce(data)
  case data
  when Time
    data
  when String
    parse_formats.each do |format|
      begin
        coerced_data = Time.strptime(data, format).utc
        return coerced_data
      rescue => _
        # do nothing
      end
    end
    CoerceFailure
  else
    CoerceFailure
  end
end

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/flat_kit/field_type/timestamp_type.rb', line 22

def self.matches?(data)
  coerced = coerce(data)
  return coerced.kind_of?(Time)
end

.parse_formatsObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/flat_kit/field_type/timestamp_type.rb', line 5

def self.parse_formats
  @timestamp_formats ||= [
    "%Y-%m-%d %H:%M:%S.%NZ",
    "%Y-%m-%d %H:%M:%S.%N",
    "%Y-%m-%dT%H:%M:%S.%N%z", # w3cdtf
    "%Y-%m-%d %H:%M:%S",
    "%Y-%m-%dT%H:%M:%S%z",
    "%Y-%m-%dT%H:%M:%SZ", 
    "%Y%m%dT%H%M%S",
    "%a, %d %b %Y %H:%M:%S %z", # rfc2822, httpdate
  ].freeze
end

.type_nameObject



18
19
20
# File 'lib/flat_kit/field_type/timestamp_type.rb', line 18

def self.type_name
  "timestamp"
end