Class: FlatKit::FieldType::DateType

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

Overview

Representing the type of data which only includes data up to the day resolution

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/flat_kit/field_type/date_type.rb', line 152

def self.coerce(data)
  case data
  when DateTime
    CoerceFailure
  when Date
    data
  when String
    coerced_data = CoerceFailure
    parse_formats.each do |format|
      begin
        coerced_data = Date.strptime(data, format)
        break
      rescue => _
        false
      end
    end
    coerced_data
  else
    CoerceFailure
  end
end

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/flat_kit/field_type/date_type.rb', line 147

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

.parse_formatsObject

parse formats are not the same as print formats as parsing does not deal with flags and widths



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/flat_kit/field_type/date_type.rb', line 21

def self.parse_formats
  @parse_formats ||= [
    # YMD formats
    "%Y-%m-%d",
    "%Y%m%d",
    "%Y/%m/%d",
    "%Y %m %d.",

    # DMY formats
    "%d %B %Y",
    "%d %b %Y",
    "%d-%b-%Y",
    "%d/%b/%Y",
    "%d-%m-%Y",
    "%d-%m-%y",
    "%d %b, %Y",
    "%d %b,%Y",
    "%d %B, %Y",
    "%d %B,%Y",

    # MDY formats
    "%m/%d/%Y",
    "%m-%d-%Y",
    "%m/%d/%y",
    "%m-%d-%y",

    "%B %d, %Y",
    "%b %d, %Y",

    # other formats
    "%Y-%j",
    "%a %b %d %Y"
 ]

end

.type_nameObject

en.wikipedia.org/wiki/Date_format_by_country List of formats culled from the above - not using all as it is definitely a performance issue at the moment def self.known_formats

@known_formats ||= [
  # YMD formats
  "%Y-%m-%d",
  "%Y%m%d",
  "%Y/%m/%d",
  "%Y.%m.%d",
  "%Y.%m.%d.",
  "%Y %m %d.",
  "%Y %b %d",
  "%Y %b %-d",
  "%Y %B %-d",
  "%Y %B %d",
  "%Y-%m%d",
  "%Y. %m. %-d.",
  "%Y. %m. %d.",
  "%Y.%-m.%-d.",
  "%Y.%-m.%-d",
  "%Y, %d %B",
  "%Y, %d %b",

  "%y.%-m.%-d",
  "%y.%-m.%-d.",
  "%y.%m.%d.",
  "%y.%m.%d",
  "%y/%m/%d",

  # DMY formats
  "%-d %b %Y",
  "%-d %B %Y",
  "%-d-%-m-%Y",
  "%-d. %-m. %Y",
  "%-d. %-m. %Y.",
  "%-d. %B %Y",
  "%-d. %B %Y.",
  "%-d.%-m.%Y",
  "%-d.%-m.%Y.",
  "%-d.%m.%Y.",
  "%-d.%m.%Y",
  "%-d.%b.%Y",
  "%-d.%B.%Y",
  "%-d/%-m %Y",
  "%-d/%-m/%Y",
  "%d %B %Y",
  "%d %b %Y",
  "%d-%m-%Y",
  "%d-%b-%Y",
  "%d-%B-%Y",
  "%d.%m.%Y",
  "%d/%m %Y",
  "%d/%m/%Y",

  "%-d.%b.%y",
  "%-d.%B.%y",
  "%-d.%-m.%y",
  "%-d/%-m-%y",
  "%-d/%-m/%y",
  "%d/%m/%y",
  "%d-%m-%y",
  "%d.%m.%y",
  "%d%m%y",

  # MDY formats
  "%-m/%-d/%Y",
  "%m/%d/%Y",
  "%m-%d-%Y",
  "%b-%d-%Y",
  "%B %-d, %Y",
  "%B %-d. %Y",
  "%B %d, %Y",
  "%B-%d-%Y",
  "%B/%d/%Y",

  "%-m/%-d/%y",

  # other formats
  "%Y-%j",
  "%Y%m",
  "%Y-%m",
  "%Y %m",
]

end



143
144
145
# File 'lib/flat_kit/field_type/date_type.rb', line 143

def self.type_name
  "date"
end