Class: FlatKit::FieldType::DateType

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

Overview

Internal: 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, weights

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Class Method Details

.coerce(data) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/flat_kit/field_type/date_type.rb', line 153

def self.coerce(data)
  case data
  when DateTime
    CoerceFailure
  when Date
    data
  when String
    try_parse(data)
  else
    CoerceFailure
  end
end

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.parse_formatsObject

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



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
56
# File 'lib/flat_kit/field_type/date_type.rb', line 23

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",
  ].freeze
end

.try_parse(data) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/flat_kit/field_type/date_type.rb', line 166

def self.try_parse(data)
  parse_formats.each do |format|
    coerced_data = Date.strptime(data, format)
    return coerced_data
  rescue StandardError => _e
    false
  end
  CoerceFailure
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



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

def self.type_name
  "date"
end