jadtfmt で 西暦 -> 和暦

jadtfmtというgemのライブラリを発見。 http://jadtfmt.rubyforge.org/ でもドキュメントすくねー!

とにかくインストールしてみると、こんなエラーが。

$ sudo gem install jadtfmt
Password:
Building native extensions.  This could take a while...
ERROR:  Error installing jadtfmt:
	ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb install jadtfmt
checking for unicode/calendar.h... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/opt/local/bin/ruby


Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/jadtfmt-0.0.4 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/jadtfmt-0.0.4/ext/gem_make.out


http://rubyforge.org/projects/jadtfmt/ によると ICUを使っているらしいので、icuをportでインストール。

$ port search icu
icu                            devel/icu      4.0          International Components for Unicode
gnomeicu                       gnome/gnomeicu 0.99         GnomeICU is one of the most popular ICQ programs in the world.
gnopernicus                    gnome/gnopernicus 1.1.2        Collection of accessibility apps for GNOME 2.
ploticus                       math/ploticus  2.40         plot generation program
$ 
$ 
$ 
$ sudo port variants icu
icu has the variants:
	universal
	doc: Install extra documentation
	freebsd
$ sudo port install icu +doc
--->  Fetching icu
--->  Attempting to fetch icu4c-4_0-src.tgz from http://download.icu-project.org/files/icu4c/4.0/
--->  Attempting to fetch icu4c-4_0-docs.zip from http://download.icu-project.org/files/icu4c/4.0/
--->  Verifying checksum(s) for icu
--->  Extracting icu
--->  Configuring icu
--->  Building icu with target all
--->  Staging icu into destroot
--->  Installing icu 4.0_0+doc
--->  Activating icu 4.0_0+doc
--->  Cleaning icu

できたっぽいので、改めてjadtfmtをインストール。

$ sudo gem install jadtfmt
Password:
Building native extensions.  This could take a while...
Successfully installed jadtfmt-0.0.4
1 gem installed
Installing ri documentation for jadtfmt-0.0.4...
Installing RDoc documentation for jadtfmt-0.0.4...

今度はさくっと入りました。


で、使い方なんだけど、 http://jadtfmt.rubyforge.org/ には クラスとメソッドの名前しか載ってない。泣。
仕方ないので、ググってみると、どうもTime.jadtfmtには 'Gyy.MM.dd' とか渡せばいいらしい
http://d.hatena.ne.jp/aufheben/20080705/1215261824

他にはどんなことができるのかソースをみてみると

  pph_G = rb_str_new2("%G");
  pph_N = rb_str_new2("%N");
  pph_n = rb_str_new2("%n");
  pph_E = rb_str_new2("%E");
  pph_G_pattern = rb_str_new2("G");
  pph_N_pattern = rb_str_new2("yy");
  pph_n_pattern = rb_str_new2("y");
  pph_E_pattern = rb_str_new2("E");

とある。%がついている方が jastrftimeで、それ以外がjadtfmtで使えるものらしいっす。

こんな感じで使えますー

$ irb
irb(main):001:0> require "rubygems"
=> true
irb(main):002:0> require "jadtfmt"
=> true
irb(main):003:0> $KCODE="u"
=> "u"
irb(main):004:0> Time.local(1989,1,7).jadtfmt("Gyy.MM.dd")
=> "昭和64.01.07"
irb(main):005:0> Time.local(1989,1,8).jadtfmt("Gyy.MM.dd")
=> "平成01.01.08"
irb(main):006:0> Time.local(1989,1,7).jastrftime("%G%N/%m/%d")
=> "昭和64/01/07"
irb(main):007:0> Time.local(1989,1,8).jastrftime("%G%N/%m/%d")
=> "平成01/01/08"

和暦 -> 西暦

ついでに 和暦からDateオブジェクトを得る方法を書いておきます。

$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.parse("H21.1.24")
=> #<Date: 4909711/2,0,2299161>
irb(main):003:0> d.year
=> 2009
irb(main):004:0> d.strftime("%Y/%m/%d")
=> "2009/01/24"

年月日の区切りがドットなら和暦としてパースしてくれますー