4
5
6
7
8
9
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
|
# File 'lib/pseudo_date/parser.rb', line 4
def self.parse(input)
date_hash = {}
input.strip!; input.gsub!('~','')
date = input.split(/\/|-/).length < 3 ? nil : Date.parse(input) rescue nil
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
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
unless date_hash.empty?
date_hash.each do |key,value|
date_hash[key] = "0#{value}" if value.to_s.length == 1
end
end
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
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
|