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
|
# File 'lib/datasets/japanese-date-parser.rb', line 10
def parse(string)
case string
when nil
nil
when /\A(平成|令和|..)\s*(\d{1,2}|元)年\s*(\d{1,2})月\s*(\d{1,2})日\z/
match_data = Regexp.last_match
era_initial = ERA_INITIALS[match_data[1]]
if era_initial.nil?
message = +"era must be one of ["
message << ERA_INITIALS.keys.join(", ")
message << "]: #{match_data[1]}"
raise UnsupportedEraInitialRange, message
end
year = match_data[2]
if year == "元"
year = "01"
else
year = year.rjust(2, "0")
end
month = match_data[3].rjust(2, "0")
day = match_data[4].rjust(2, "0")
Date.jisx0301("#{era_initial}#{year}.#{month}.#{day}")
else
string
end
end
|