DateTimeで和暦文字列をW3CDTF文字列に変換するサンプル
和暦の文字列を、RSS の dc:date に最適な W3CDTF文字列に変換します。
#!/usr/bin/perl
# 法令データ提供システム提供ページのURL指定して、
# 最終改正の箇所の日付を取得して、
# W3CDTF(RSS の dc:date などに最適)に変換して表示
use strict;
use warnings;
use utf8;
use Encode;
use LWP::Simple;
use DateTime::Format::W3CDTF;
use DateTime::Format::Japanese;
my $url = 'http://law.e-gov.go.jp/htmldata/H05/H05HO091.html';
my $html = decode('cp932', get($url) );
my $day_kansu = $1 if $html =~m/最終改正:(.+日)/;
#my $day_kansu = '平成十八年一〇月三十一日';
#テスト用。「十」と「一〇」は区別つくかな?
return unless $day_kansu;
print encode('utf8', "入力データ: $day_kansu\n");
# 和暦文字列からDateTimeオブジェクトに変換
my $fmt = DateTime::Format::Japanese->new();
my $dt = $fmt->parse_datetime(encode('utf8',$day_kansu ) );
$dt->set_time_zone('local'); #タイムゾーン忘れずに
# DateTimeオブジェクトをW3CDTFフォーマット文字列へ変換
my $str = DateTime::Format::W3CDTF->format_datetime($dt);
print "W3CDTF->format_datetime: $str\n";
$str = DateTime::Format::W3CDTF->format_date($dt);
print "W3CDTF->format_date : $str\n";
参考:
[[Perlで日付・時間を操作 - DateTime モジュールの使い方 (iandeth.)
AnnoCPAN - DateTime::Format::Japanese
DateTime::Format::W3CDTF - Parse and format W3CDTF datetime strings - search.cpan.org