Class: PseudoDate::Parser

Inherits:
Object show all
Defined in:
lib/pseudo_date/parser.rb

Constant Summary collapse

AMERICAN_DATE_FORMAT =
'%m/%d/%Y'
EUROPEAN_DATE_FORMAT =
'%Y-%m-%d'

Class Method Summary collapse

Class Method Details

.parse(input) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/pseudo_date/parser.rb', line 10

def self.parse(input)
  date_hash = {}
  # Minor Pre Cleanup
  input.strip!; input.gsub!('~','')
  
  date = parse_with_poro_date(input)
  
  if date
    date_hash = { :year => date.year.to_s, :month => date.month.to_s, :day => date.day.to_s }
  else
    year, month, day = parse_string(input)
    date_hash = { :year => year, :month => month, :day => day }
  end
  
  # Post parsing cleanup
  date_hash.each do |key, value|
    date_hash[key] = if value.nil?
      key.to_s == 'year' ? '0000' : '00'
    else
      date_hash[key] = value.to_s.strip
    end
  end
  
  # Cleanup the single digit values
  unless date_hash.empty?
    date_hash.each do |key,value|
      date_hash[key] = "0#{value}" if value.to_s.length == 1
    end
  end
  
  # Two character years
  if date_hash[:year].length == 2
    date_hash[:year] = date_hash[:year].to_i > Date.today.year.to_s.slice(2..4).to_i ? "19#{date_hash[:year]}" : "20#{date_hash[:year]}"
  end
  
  # Attempt to correct some known OCR issues
  if date_hash[:year].to_s.match('00') && date_hash[:year] != '0000'
    date_hash[:year] = "2#{date_hash[:year].slice(1..3)}"
  end
  
  return date_hash.empty? ? nil : date_hash
end