From f147e9b7a4a0c8ed8be226cceb3b607bd3e65d1b Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 21 Oct 2012 16:56:42 +0900 Subject: [PATCH 1/3] Implemented first stage of gregorian to imperial date conversion --- nao-randym/date_converter.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 nao-randym/date_converter.rb diff --git a/nao-randym/date_converter.rb b/nao-randym/date_converter.rb new file mode 100644 index 0000000..4215604 --- /dev/null +++ b/nao-randym/date_converter.rb @@ -0,0 +1,33 @@ +# encoding: UTF-8 +require 'date' +a = %w[18680908 19120730 19261225 19890108 20300101] +$eras = %w[明治 大正 昭和 平成] +$date_ranges= {} +(a.size-1).times do |i| + $date_ranges[(Date.parse(a[i])...Date.parse(a[i+1]))] = $eras[i] +end + +def convert(input_date) + output = 'WTF' + date = Date.parse(input_date.gsub!('-', '')) + $date_ranges.each do |range, era| + if range.include? date + nengo = era + nengo_year = date.year - range.first.year + 1 + output = date.strftime("#{nengo}#{nengo_year}年%m月%d日") + end + end + output +end + +require 'test/unit' + +class TestFoo < Test::Unit::TestCase + + def test_simple_one + ret = convert('2012-10-20') + assert_equal('平成24年10月20日', ret) + end + + +end From 1dcfd9ffe4c0469f13f9ab5cd9b9176b14fe507d Mon Sep 17 00:00:00 2001 From: Naoyoshi Aikawa Date: Sun, 21 Oct 2012 17:08:53 +0900 Subject: [PATCH 2/3] Add test for Showa/Heisei boundary --- nao-randym/date_converter.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/nao-randym/date_converter.rb b/nao-randym/date_converter.rb index 4215604..656faef 100644 --- a/nao-randym/date_converter.rb +++ b/nao-randym/date_converter.rb @@ -14,7 +14,7 @@ def convert(input_date) if range.include? date nengo = era nengo_year = date.year - range.first.year + 1 - output = date.strftime("#{nengo}#{nengo_year}年%m月%d日") + output = date.strftime("#{nengo}#{nengo_year}年%-m月%-d日") end end output @@ -29,5 +29,13 @@ def test_simple_one assert_equal('平成24年10月20日', ret) end + def test_showa_end + ret = convert('1989-01-07') + assert_equal('昭和64年1月7日', ret) + end + def test_heisei_start + ret = convert('1989-01-08') + assert_equal('平成1年1月8日', ret) + end end From a2d7e58665c0e8a4c89bd0311301e25fb3f19061 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 21 Oct 2012 17:27:57 +0900 Subject: [PATCH 3/3] added more tests and excluded invalid (or uncalculable) dates in the old, pre meiji 6 calendar. --- nao-randym/date_converter.rb | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/nao-randym/date_converter.rb b/nao-randym/date_converter.rb index 656faef..d594946 100644 --- a/nao-randym/date_converter.rb +++ b/nao-randym/date_converter.rb @@ -1,19 +1,22 @@ # encoding: UTF-8 require 'date' -a = %w[18680908 19120730 19261225 19890108 20300101] -$eras = %w[明治 大正 昭和 平成] +a = %w[18730101 19120730 19261225 19890108] +a << (Time.now+(60*60*24)).strftime('%Y%m%d') +eras = %w[明治 大正 昭和 平成] $date_ranges= {} (a.size-1).times do |i| - $date_ranges[(Date.parse(a[i])...Date.parse(a[i+1]))] = $eras[i] + $date_ranges[(Date.parse(a[i])...Date.parse(a[i+1]))] = eras[i] end def convert(input_date) - output = 'WTF' + + output = 'Your date sucks.' date = Date.parse(input_date.gsub!('-', '')) $date_ranges.each do |range, era| if range.include? date nengo = era nengo_year = date.year - range.first.year + 1 + nengo_year += 5 if era == '明治' output = date.strftime("#{nengo}#{nengo_year}年%-m月%-d日") end end @@ -38,4 +41,29 @@ def test_heisei_start ret = convert('1989-01-08') assert_equal('平成1年1月8日', ret) end + + def test_out_of_bounds_max + ret = convert((Time.now+(60*60*24)).strftime('%Y-%m-%d')) + assert_equal('Your date sucks.', ret) + end + + def test_just_in_bounds_max + ret = convert(Time.now.strftime('%Y-%m-%d')) + assert_not_equal('Your date sucks.', ret) + end + + def test_out_of_bounds_min + ret = convert('1872-12-31') + assert_equal('Your date sucks.', ret) + end + + def test_just_in_bounds_min + ret = convert('1873-01-01') + assert_equal('明治6年1月1日', ret) + end + + def test_out_of_bounds_max + ret = convert('2030-01-01') + assert_equal('Your date sucks.', ret) + end end