From 560795b6ee274471bf18e800baba3cbf15956826 Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 16:27:50 +0900 Subject: [PATCH 1/7] Spec and main class setup. --- calendar.rb | 4 ++++ calendar_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 calendar.rb create mode 100644 calendar_spec.rb diff --git a/calendar.rb b/calendar.rb new file mode 100644 index 0000000..9d5b1b3 --- /dev/null +++ b/calendar.rb @@ -0,0 +1,4 @@ +module Calendar + def self.convert(date) + end +end \ No newline at end of file diff --git a/calendar_spec.rb b/calendar_spec.rb new file mode 100644 index 0000000..f337a88 --- /dev/null +++ b/calendar_spec.rb @@ -0,0 +1,12 @@ +# encoding: utf-8 + +require 'rubygems' +require 'rspec' +require "#{File.dirname(__FILE__)}/calendar" + +describe Calendar do + it "should return a blank string for nil input" do + Calendar.convert(nil).should == nil + end +end + From 120efcdbe742c48e812163ba0cf1c04752ca062d Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 16:39:43 +0900 Subject: [PATCH 2/7] Heisei support Heisei jump! --- calendar.rb | 5 +++++ calendar_spec.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/calendar.rb b/calendar.rb index 9d5b1b3..015603c 100644 --- a/calendar.rb +++ b/calendar.rb @@ -1,4 +1,9 @@ +#encoding: utf-8 module Calendar def self.convert(date) + return nil unless date + year, month, day = date.split('-') + japanese_year = year.to_i - 1989 + 1 + "平成#{japanese_year}年#{month}月#{day}日" end end \ No newline at end of file diff --git a/calendar_spec.rb b/calendar_spec.rb index f337a88..f0b07c8 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -8,5 +8,9 @@ it "should return a blank string for nil input" do Calendar.convert(nil).should == nil end + + it "should work for today" do + Calendar.convert("2012-10-20").should == "平成24年10月20日" + end end From 5d433c7397abe2d7905ad168a9d7483a025165cc Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 16:47:29 +0900 Subject: [PATCH 3/7] Showa date support Natsukashii na~ --- calendar.rb | 8 ++++++-- calendar_spec.rb | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/calendar.rb b/calendar.rb index 015603c..162457e 100644 --- a/calendar.rb +++ b/calendar.rb @@ -3,7 +3,11 @@ module Calendar def self.convert(date) return nil unless date year, month, day = date.split('-') - japanese_year = year.to_i - 1989 + 1 - "平成#{japanese_year}年#{month}月#{day}日" + if year.to_i >= 1989 + japanese_year = "平成#{year.to_i - 1989 + 1}" + else + japanese_year = "昭和#{year.to_i - 1926 + 1}" + end + "#{japanese_year}年#{month}月#{day}日" end end \ No newline at end of file diff --git a/calendar_spec.rb b/calendar_spec.rb index f0b07c8..691cbd5 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -12,5 +12,9 @@ it "should work for today" do Calendar.convert("2012-10-20").should == "平成24年10月20日" end + +it "should work for Showa dates (natsukashii)" do + Calendar.convert("1979-08-08").should == "昭和54年08月08日" +end end From 8b66432122d2f30c30ec89f135d04e78e8933fca Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 17:06:49 +0900 Subject: [PATCH 4/7] Support for start of Heisei on 1989-01-08 --- calendar.rb | 10 +++++----- calendar_spec.rb | 12 +++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/calendar.rb b/calendar.rb index 162457e..5586048 100644 --- a/calendar.rb +++ b/calendar.rb @@ -2,12 +2,12 @@ module Calendar def self.convert(date) return nil unless date - year, month, day = date.split('-') - if year.to_i >= 1989 - japanese_year = "平成#{year.to_i - 1989 + 1}" + year, month, day = date.split('-').map(&:to_i) + if Date.parse(date) >= Date.parse("1989-01-08") + japanese_year = "平成#{(year - 1989 + 1).to_s.rjust(2, '0')}" else - japanese_year = "昭和#{year.to_i - 1926 + 1}" + japanese_year = "昭和#{(year - 1926 + 1).to_s.rjust(2, '0')}" end - "#{japanese_year}年#{month}月#{day}日" + "#{japanese_year}年#{month.to_s.rjust(2, '0')}月#{day.to_s.rjust(2, '0')}日" end end \ No newline at end of file diff --git a/calendar_spec.rb b/calendar_spec.rb index 691cbd5..3d79e20 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -13,8 +13,14 @@ Calendar.convert("2012-10-20").should == "平成24年10月20日" end -it "should work for Showa dates (natsukashii)" do - Calendar.convert("1979-08-08").should == "昭和54年08月08日" -end + it "should work for border of era" do + Calendar.convert("1989-01-07").should == "昭和64年01月07日" + Calendar.convert("1989-01-08").should == "平成01年01月08日" + Calendar.convert("1989-01-09").should == "平成01年01月09日" + end + + it "should work for Showa dates (natsukashii)" do + Calendar.convert("1979-08-08").should == "昭和54年08月08日" + end end From 769d719e44719d195fdf6ed1f780c5f87dbb357d Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 17:15:17 +0900 Subject: [PATCH 5/7] Taisho support --- calendar.rb | 4 +++- calendar_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/calendar.rb b/calendar.rb index 5586048..ef6e7da 100644 --- a/calendar.rb +++ b/calendar.rb @@ -5,8 +5,10 @@ def self.convert(date) year, month, day = date.split('-').map(&:to_i) if Date.parse(date) >= Date.parse("1989-01-08") japanese_year = "平成#{(year - 1989 + 1).to_s.rjust(2, '0')}" - else + elsif Date.parse(date) >= Date.parse("1926-12-25") japanese_year = "昭和#{(year - 1926 + 1).to_s.rjust(2, '0')}" + else + japanese_year = "大正#{(year - 1912 + 1).to_s.rjust(2, '0')}" end "#{japanese_year}年#{month.to_s.rjust(2, '0')}月#{day.to_s.rjust(2, '0')}日" end diff --git a/calendar_spec.rb b/calendar_spec.rb index 3d79e20..c25a276 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -22,5 +22,9 @@ it "should work for Showa dates (natsukashii)" do Calendar.convert("1979-08-08").should == "昭和54年08月08日" end + +it "should work for Taisho dates (furui)" do + Calendar.convert("1912-07-30").should == "大正01年07月30日" +end end From a6929591b2d3557c771b406a6bb1712c08593aad Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 17:18:59 +0900 Subject: [PATCH 6/7] Meiji date support --- calendar.rb | 4 +++- calendar_spec.rb | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/calendar.rb b/calendar.rb index ef6e7da..2521e4d 100644 --- a/calendar.rb +++ b/calendar.rb @@ -7,8 +7,10 @@ def self.convert(date) japanese_year = "平成#{(year - 1989 + 1).to_s.rjust(2, '0')}" elsif Date.parse(date) >= Date.parse("1926-12-25") japanese_year = "昭和#{(year - 1926 + 1).to_s.rjust(2, '0')}" - else + elsif Date.parse(date) >= Date.parse("1912-07-30") japanese_year = "大正#{(year - 1912 + 1).to_s.rjust(2, '0')}" + elsif Date.parse(date) >= Date.parse("1868-09-08") + japanese_year = "明治#{(year - 1868 + 1).to_s.rjust(2, '0')}" end "#{japanese_year}年#{month.to_s.rjust(2, '0')}月#{day.to_s.rjust(2, '0')}日" end diff --git a/calendar_spec.rb b/calendar_spec.rb index c25a276..8c8f97a 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -23,8 +23,12 @@ Calendar.convert("1979-08-08").should == "昭和54年08月08日" end -it "should work for Taisho dates (furui)" do - Calendar.convert("1912-07-30").should == "大正01年07月30日" -end + it "should work for Taisho dates (furui)" do + Calendar.convert("1912-07-30").should == "大正01年07月30日" + end + + it "should work for Meiji dates (motto furui)" do + Calendar.convert("1868-09-08").should == "明治01年09月08日" + end end From a3452eb25d189d265a476cac335c5eca71d40918 Mon Sep 17 00:00:00 2001 From: mayuko hirono Date: Sun, 21 Oct 2012 17:22:18 +0900 Subject: [PATCH 7/7] Throws error for dates pre Meiji era Calculating dates reliably before this is hard :( --- calendar.rb | 2 ++ calendar_spec.rb | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/calendar.rb b/calendar.rb index 2521e4d..3f4cf37 100644 --- a/calendar.rb +++ b/calendar.rb @@ -11,6 +11,8 @@ def self.convert(date) japanese_year = "大正#{(year - 1912 + 1).to_s.rjust(2, '0')}" elsif Date.parse(date) >= Date.parse("1868-09-08") japanese_year = "明治#{(year - 1868 + 1).to_s.rjust(2, '0')}" + else + raise ArgumentError, "Dates earlier than the Meiji era are unsupported" end "#{japanese_year}年#{month.to_s.rjust(2, '0')}月#{day.to_s.rjust(2, '0')}日" end diff --git a/calendar_spec.rb b/calendar_spec.rb index 8c8f97a..ec493b7 100644 --- a/calendar_spec.rb +++ b/calendar_spec.rb @@ -30,5 +30,9 @@ it "should work for Meiji dates (motto furui)" do Calendar.convert("1868-09-08").should == "明治01年09月08日" end + + it "should throw error for pre-Meiji dates" do + expect{Calendar.convert("1868-09-01")}.to raise_error(ArgumentError) + end end