From 400aa4a9e090b1c4c62a0c0b23a4cd45555f8b3e Mon Sep 17 00:00:00 2001 From: Truls Tangstad Date: Sun, 21 Oct 2012 16:55:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Parse=20=E5=B9=B3=E6=88=90,=20=E6=98=AD?= =?UTF-8?q?=E5=92=8C,=20=E5=A4=A7=E6=AD=A3,=20=E6=98=8E=E6=B2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kim_truls/japanese_era.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 kim_truls/japanese_era.rb diff --git a/kim_truls/japanese_era.rb b/kim_truls/japanese_era.rb new file mode 100644 index 0000000..53be466 --- /dev/null +++ b/kim_truls/japanese_era.rb @@ -0,0 +1,39 @@ +# encoding: utf-8 +require 'rspec' +require 'date' + +module JapaneseEra + def self.convert date_str + date = Date.parse(date_str) + date_str_jp = "" + if date >= Date.new(1989,1,8) + date_str_jp = "平成#{date.year - 1988}年#{date.month}月#{date.day}日" + elsif date >= Date.new(1926, 12, 25) + date_str_jp = "昭和#{date.year - 1925}年#{date.month}月#{date.day}日" + elsif date >= Date.new(1912, 07, 30) + date_str_jp = "大正#{date.year - 1911}年#{date.month}月#{date.day}日" + else + date_str_jp = "明治#{date.year - 1867}年#{date.month}月#{date.day}日" + end + return date_str_jp + end +end + +describe JapaneseEra do + describe "#convert" do + it "can convert year to 平成" do + JapaneseEra.convert("1989-01-08").should eq "平成1年1月8日" + end + it "can convert year to 昭和" do + JapaneseEra.convert("1989-01-07").should eq "昭和64年1月7日" + JapaneseEra.convert("1926-12-25").should eq "昭和1年12月25日" + end + it "can convert year to 大正" do + JapaneseEra.convert("1912-07-30").should eq "大正1年7月30日" + JapaneseEra.convert("1926-12-24").should eq "大正15年12月24日" + end + it "can convert year to 明治" do + JapaneseEra.convert("1912-07-29").should eq "明治45年7月29日" + end + end +end From 4f500d40d3c9bdec0c9b521335bebc317b31ccb7 Mon Sep 17 00:00:00 2001 From: Truls Tangstad Date: Sun, 21 Oct 2012 17:23:13 +0900 Subject: [PATCH 2/2] Cleanup using lookup table --- kim_truls/japanese_era.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/kim_truls/japanese_era.rb b/kim_truls/japanese_era.rb index 53be466..c92be59 100644 --- a/kim_truls/japanese_era.rb +++ b/kim_truls/japanese_era.rb @@ -3,19 +3,18 @@ require 'date' module JapaneseEra + Eras = {"平成" => Date.new(1989, 1, 8), + "昭和" => Date.new(1926, 12, 25), + "大正" => Date.new(1912, 07, 30), + "明治" => Date.new(1868, 9, 8), + } + def self.convert date_str date = Date.parse(date_str) date_str_jp = "" - if date >= Date.new(1989,1,8) - date_str_jp = "平成#{date.year - 1988}年#{date.month}月#{date.day}日" - elsif date >= Date.new(1926, 12, 25) - date_str_jp = "昭和#{date.year - 1925}年#{date.month}月#{date.day}日" - elsif date >= Date.new(1912, 07, 30) - date_str_jp = "大正#{date.year - 1911}年#{date.month}月#{date.day}日" - else - date_str_jp = "明治#{date.year - 1867}年#{date.month}月#{date.day}日" + Eras.each do |era, start| + return "#{era}#{date.year - start.year + 1}年#{date.month}月#{date.day}日" if date >= start end - return date_str_jp end end