Skip to content

Commit

Permalink
Support number format option. (sugi#11)
Browse files Browse the repository at this point in the history
Fix standard Date#strftime compatibility
  • Loading branch information
masa-kunikata committed Jun 16, 2019
1 parent 25e2783 commit b19d0d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
23 changes: 17 additions & 6 deletions lib/wareki/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,29 @@ def to_time
end

def strftime(format_str = '%JF')
ret = format_str.to_str.gsub(/%J(-|[_0]?[0-9]+|)([fFyYegGoOiImMsSlLdD][kK]?)/) { format($2, $1) || $& }
ret = format_str.to_str.gsub(/%J(-|[_0]{0,2}[0-9]*|)([fFyYegGoOiImMsSlLdD][kK]?)/) { format($2, $1) || $& }
ret.index('%') or return ret
d = to_date
d.respond_to?(:_wareki_strftime_orig) ? d._wareki_strftime_orig(ret) : d.strftime(ret)
end

def format(key, opt = "")
sprintf_format = (opt.empty? ? "%02d" : "%#{opt.sub('_', '')}d")
def _number_format(opt)
case opt
when '' then '%02d'
when '-' then '%d'
when '0' then '%02d'
when '_0' then '%02d'
when /_\Z/ then '%2d'
when /0?_/ then "\%#{opt.sub(/0?_/, '')}d"
when /_?0/ then "\%#{opt.sub(/_?0/, '0')}d"
else "\%0#{opt}d"
end
end

def format(key, opt = '')
case key.to_sym
when :e then era_name
when :g then era_name.to_s == '' ? '' : (sprintf_format % era_year)
when :g then era_name.to_s == '' ? '' : Kernel.format(_number_format(opt), era_year)
when :G then era_name.to_s == '' ? '' : Utils.i2z(era_year)
when :Gk then era_name.to_s == '' ? '' : YaKansuji.to_kan(era_year, :simple)
when :GK
Expand All @@ -187,14 +198,14 @@ def format(key, opt = "")
when :i then imperial_year
when :I then Utils.i2z(imperial_year)
when :Ik then YaKansuji.to_kan(imperial_year, :simple)
when :s then (sprintf_format % month)
when :s then Kernel.format(_number_format(opt), month)
when :S then Utils.i2z(month)
when :Sk then YaKansuji.to_kan(month, :simple)
when :SK then Utils.alt_month_name(month)
when :l then leap_month? ? "'" : ''
when :L then leap_month? ? '’' : ''
when :Lk then leap_month? ? '閏' : ''
when :d then (sprintf_format % day)
when :d then Kernel.format(_number_format(opt), day)
when :D then Utils.i2z(day)
when :Dk then YaKansuji.to_kan(day, :simple)
when :DK
Expand Down
29 changes: 26 additions & 3 deletions spec/date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,18 @@ def self.days(v); new; end
expect(d.strftime("%Jm %JM %JMk")).to eq "05' 閏5 閏五"
expect(d.strftime("%Jy %JY %JYk")).to eq "天和03 天和3 天和三"

expect(d.strftime("1桁: %J01f")).to eq "1桁: 天和3年5'月4日"
expect(d.strftime("1桁: %J01y %J01m %J01d")).to eq "1桁: 天和3 5' 4"
expect(d.strftime("1桁: %J01g %J01s")).to eq "1桁: 3 5"
expect(d.strftime("1桁: %J1f")).to eq "1桁: 天和3年5'月4日"
expect(d.strftime("1桁: %J1y %J1m %J1d")).to eq "1桁: 天和3 5' 4"
expect(d.strftime("1桁: %J1g %J1s")).to eq "1桁: 3 5"
expect(d.strftime("空白2桁: %J_2f")).to eq "空白2桁: 天和 3年 5'月 4日"
expect(d.strftime("空白2桁: %J_2y %J_2m %J_2d")).to eq "空白2桁: 天和 3 5' 4"
expect(d.strftime("空白2桁: %J_2g %J_2s")).to eq "空白2桁: 3 5"
expect(d.strftime("0埋3桁: %J03f")).to eq "0埋3桁: 天和003年005'月004日"
expect(d.strftime("0埋3桁: %J03y %J03m %J03d")).to eq "0埋3桁: 天和003 005' 004"
expect(d.strftime("0埋3桁: %J03g %J03s")).to eq "0埋3桁: 003 005"
expect(d.strftime("0埋4桁: %J4f")).to eq "0埋4桁: 天和0003年0005'月0004日"
expect(d.strftime("0埋4桁: %J4y %J4m %J4d")).to eq "0埋4桁: 天和0003 0005' 0004"
expect(d.strftime("0埋4桁: %J4g %J4s")).to eq "0埋4桁: 0003 0005"

expect(d.strftime("皇紀で%Ji年%Jm月%Jd日")).to eq "皇紀で2343年05'月04日"
expect(d.strftime("%JYk年 %JSK")).to eq "天和三年 皐月"
Expand All @@ -212,6 +215,26 @@ def self.days(v); new; end
expect(Wareki::Date.new("寿永", 1, 1, 1).strftime("%JYK年%JM%JL月%JDK日")).to eq "寿永元年1月元日"
end

it "can be formatted having compatibility with standard Date#strftime" do
wareki_date = Wareki::Date.new("令和", 1, 5, 4)
date = ::Date.new(2019, 5, 4)

expect(wareki_date.strftime("%Jm %Jd")).to eq date.strftime("%m %d")
expect(wareki_date.strftime("%J-m %J-d")).to eq date.strftime("%-m %-d")

expect(wareki_date.strftime("%J_m %J_d")).to eq date.strftime("%_m %_d")
expect(wareki_date.strftime("%J_2m %J_2d")).to eq date.strftime("%_2m %_2d")
expect(wareki_date.strftime("%J03m %J03d")).to eq date.strftime("%03m %03d")
expect(wareki_date.strftime("%J4m %J4d")).to eq date.strftime("%4m %4d")

expect(wareki_date.strftime("%J0_5m %J0_5d")).to eq date.strftime("%0_5m %0_5d")
expect(wareki_date.strftime("%J_06m %J_06d")).to eq date.strftime("%_06m %_06d")

expect(wareki_date.strftime("%J0m %J0d")).to eq date.strftime("%0m %0d")
expect(wareki_date.strftime("%J0_m %J0_d")).to eq date.strftime("%0_m %0_d")
expect(wareki_date.strftime("%J_0m %J_0d")).to eq date.strftime("%_0m %_0d")
end

it "can handle last days of era" do
expect(Date.parse("1989/1/7").strftime('%JF')).to eq "昭和六十四年一月七日"
expect(Date.parse("1912/7/29").strftime('%JF')).to eq "明治四十五年七月二十九日"
Expand Down

0 comments on commit b19d0d9

Please sign in to comment.