Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Japanese Era Calendar Scheme

## 日本語暦変換プログラム

If you've ever had to fill out paperwork in Japan, you know that the country uses two systems for counting dates: the western [Gregorian calendar](http://en.wikipedia.org/wiki/Gregorian_calendar), and the [Japanese era calendar scheme](http://en.wikipedia.org/wiki/Japanese_era_name), which uses the combination of the reigning Emperor's name and year since his birth.

Write a program that takes a Gregorian date as an input, give the Japanese era date as an output. For example, with 2012-10-20 as an input, the output would be 平成24年10月20日.
Expand Down
10 changes: 10 additions & 0 deletions yohei-cjs/Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

for date in \
1920-05-07 \
1912-07-29 \
1912-07-30 \
2010-12-31 \
; do
./translate $date
done | diff -u expected1 -
4 changes: 4 additions & 0 deletions yohei-cjs/expected1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
大正九年五月七日
明治四十五年七月二十九日
大正元年七月三十日
平成二十二年十二月三十一日
32 changes: 32 additions & 0 deletions yohei-cjs/translate
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# Convert number represented as a one- or two-digit string to kanji.
def to_kanji(dd)
d1, d2 = dd.length == 1 ? ['0', dd[0]-48] : [dd[0]-48, dd[1]-48]
case d1 when 0 then ''
when 1 then 'JUU'
else to_kanji_single(d1.to_i) ;end \
+ to_kanji_single(d2.to_s)
end

def to_kanji_single(n)
n.to_s
end

date = ARGV[0]
match = date.match(/^(\d{4})-(\d\d)-(\d\d)$/)
raise "Unparsable date: #{ARGV[0]}" unless match
year, month, day = match[1].to_i, match[2], match[3]

eras = [
['1989-01-08', '平成'],
['1926-12-25', '昭和'],
['1912-07-30', '大正'],
['1868-09-08', '明治'],
]
era_start, era = eras.drop_while { |start, era| start > date }[0]
era_start_year = Integer(era_start.match(/^\d{4}/)[0])

printf("%s%s年%s月%s日\n", era, to_kanji((year-era_start_year+1).to_s),
to_kanji(month), to_kanji(day))