|
NAMEDate::Japanese::Era - Conversion between Japanese Era / Gregorian calendar SYNOPSIS use utf8;
use Date::Japanese::Era;
# from Gregorian (month + day required)
$era = Date::Japanese::Era->new(1970, 1, 1);
# from Japanese Era
$era = Date::Japanese::Era->new("昭和", 52); # SHOWA
$name = $era->name; # 昭和 (in Unicode)
$gengou = $era->gengou; # Ditto
$year = $era->year; # 52
$gregorian = $era->gregorian_year; # 1977
# use JIS X0301 table for conversion
use Date::Japanese::Era 'JIS_X0301';
# more DWIMmy
$era = Date::Japanese::Era->new("昭和五十二年");
$era = Date::Japanese::Era->new("昭和52年");
DESCRIPTIONDate::Japanese::Era handles conversion between Japanese Era and Gregorian calendar. METHODS
EXAMPLES use utf8;
use Date::Japanese::Era;
# 2001 is H-13
my $era = Date::Japanese::Era->new(2001, 8, 31);
printf "%s-%s", uc(substr($era->name_ascii, 0, 1)), $era->year;
# to Gregorian
my $era = Date::Japanese::Era->new("平成", 13); # HEISEI 13
print $era->gregorian_year; # 2001
ERA NAME VALIDATION AND CONVERSIONWhen you construct a new object from Japanese Era and year, this module does not handle if the year does not exist for the given era, such as 平成32, since the era ended in 31. This might be problematic if you want to allow the year number to exceed its end and automatically convert to the correct era i.e. 令和2. To do this, you can use an offset-based calculation first to get the Gregorian year, and then construct a Date::Japanese::Era object from Gregorian year, month and day, such as: my %offset = (
"昭和" => 1925,
"平成" => 1988,
"令和" => 2018,
);
my $name = "平成";
my $year = 33;
my $month = 4;
my $day = 1;
my $gregorian_year = $offset{$name} + $year;
my $era = Date::Japanese::Era->new( $gregorian_year, $month, $day );
# $era is now Reiwa 3, since Heisei 33 doesn't exist.
Similarly, to validate if the given Japanese era is valid for the given date, you can compare the era after round-tripping with Gregorian year: sub is_valid_era {
my( $name, $year, $month, $day ) = @_;
my $ok;
eval {
my $era1 = Date::Japanese::Era->new($name, $year);
my $era2 = Date::Japanese::Era->new($era1->gregorian_year, $month, $day);
$ok = $era1->name eq $era2->name;
};
return $ok;
}
CAVEATS
AUTHORTatsuhiko Miyagawa <miyagawa@bulknews.net> COPYRIGHTTatsuhiko Miyagawa, 2001- LICENSEThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSODateTime::Calendar::Japanese::Era, Date::Calc, Encode
|