Skip to content

Commit

Permalink
Merge 35aa004 into 01514fc
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Nov 10, 2019
2 parents 01514fc + 35aa004 commit 461cce7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/wareki/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ module Wareki
)
NUM_CHARS = '零壱壹弌弐貳貮参參弎肆伍陸漆質柒捌玖〇一二三四五六七八九十拾什卄廿卅丗卌百陌佰皕阡仟千万萬億兆京垓01234567890123456789'.freeze
ALT_MONTH_NAME = %w(睦月 如月 弥生 卯月 皐月 水無月 文月 葉月 長月 神無月 霜月 師走).freeze
REGEX = %r{^
REGEX = %r{
(?:(?<era_name>紀元前|#{ERA_REGEX})?
(?:(?<year>[元#{NUM_CHARS}]+)年))?
(?:(?<is_leap>閏|潤|うるう)?
(?:(?<month>[正#{NUM_CHARS}]+)月 |
(?<alt_month>#{ALT_MONTH_NAME.join('|')})))?
(?:(?<day>[元朔晦#{NUM_CHARS}]+)日|元旦)?
$}x.freeze
}x.freeze

class UnsupportedDateRange < StandardError; end

Expand Down
3 changes: 2 additions & 1 deletion lib/wareki/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def self._check_invalid_date(era, year, month, day)

def self._parse(str)
str = str.to_s.gsub(/[[:space:]]/, '')
match = (!str.empty? && REGEX.match(str)) or
match = REGEX.match(str)
match && !match[0].empty? or
raise ArgumentError, "Invaild Date: #{str}"
era = match[:era_name]
if (era.nil? || era == '') && match[:year].nil?
Expand Down
10 changes: 10 additions & 0 deletions lib/wareki/std_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ def parse(str, comp = true, start = ::Date::ITALY)
rescue ArgumentError, Wareki::UnsupportedDateRange
::Date._wareki_parse_orig(str, comp, start)
end

alias _wareki__parse_orig _parse
def _parse(str, comp = true)
di = Wareki::Date._parse(str)
wdate = Wareki::Date.new(di[:era], di[:year], di[:month], di[:day], di[:is_leap])
rescue ArgumentError, Wareki::UnsupportedDateRange
::Date._wareki__parse_orig(str, comp)
else
::Date._wareki__parse_orig(str.sub(Wareki::REGEX, wdate.strftime('%F ')), comp)
end
end
end
5 changes: 5 additions & 0 deletions spec/std_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
}.to raise_error(ArgumentError)
end

it "overrides _parse" do
expect(Date._parse("平成元年5月4日")).to eq({ year: 1989, mon: 5, mday: 4 })
expect(Date._parse("平成元年5月4日12:34:56")).to eq({ year: 1989, mon: 5, mday: 4, hour: 12, min: 34, sec: 56 })
end

it "have Date::JAPAN" do
expect(Date::JAPAN).to eq Wareki::GREGORIAN_START
end
Expand Down

0 comments on commit 461cce7

Please sign in to comment.