Changeset 1865

Show
Ignore:
Timestamp:
06/15/06 18:47:41
Author:
miyagawa
Message:

support DWIMy extraction and Lingua::JA::Numbers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Date-Japanese-Era/trunk/Changes

    r1864 r1865  
    11Revision history for Perl extension Date::Japanese::Era. 
     2 
     30.06  Thu Jun 15 11:07:39 JST 2006 
     4        - Oops, forgot to update Changes 
     5 
     60.05  Thu Jun 15 11:07:02 JST 2006 
     7        * Support single argument like "昭和五十二年". Uses 
     8          Lingua::JA::Numbers for conversion. 
    29 
    3100.04  Thu Jun 15 10:27:08 JST 2006 
  • Date-Japanese-Era/trunk/Makefile.PL

    r1864 r1865  
    99        Test::More => 0, 
    1010        Encode => 2.1, 
     11        Lingua::JA::Numbers => 0.04, 
    1112    }, 
    1213); 
  • Date-Japanese-Era/trunk/lib/Date/Japanese/Era.pm

    r1864 r1865  
    22 
    33use strict; 
    4 our $VERSION = '0.04'; 
     4our $VERSION = '0.06'; 
    55 
    66use Carp; 
     
    3636        $self->_from_era(@args); 
    3737    } 
     38    elsif (@args == 1) { 
     39        $self->_dwim(@args); 
     40    } 
    3841    else { 
    39         croak "odd number of arguments: ", scalar(@args); 
    40     } 
     42        croak "odd number of arguments: ", scalar(@args); 
     43    } 
     44 
    4145    return $self; 
    4246} 
     
    9094} 
    9195 
     96sub _dwim { 
     97    my($self, $str) = @_; 
     98 
     99    unless (utf8::is_utf8($str)) { 
     100        croak "Era should be Unicode flagged"; 
     101    } 
     102 
     103    my $gengou_re = join "|", keys %ERA_JA2ASCII; 
     104 
     105    $str =~ s/^($gengou_re)// 
     106        or croak "Can't extract Era from $str"; 
     107 
     108    my $era = $1; 
     109 
     110    $str =~ s/\x{5E74}$//; # nen 
     111    my $year = _number($str); 
     112 
     113    unless (defined $year) { 
     114        croak "Can't parse year from $str"; 
     115    } 
     116 
     117    $self->_from_era($era, $year); 
     118} 
     119 
     120sub _number { 
     121    my $str = shift; 
     122 
     123    $str =~ s/([\x{FF10}-\x{FF19}])/;ord($1)-0xff10/eg; 
     124 
     125    if ($str =~ /^\d+$/) { 
     126        return $str; 
     127    } else { 
     128        eval { require Lingua::JA::Numbers }; 
     129        if ($@) { 
     130            croak "require Lingua::JA::Numbers to read Japanized numbers"; 
     131        } 
     132 
     133        return Lingua::JA::Numbers::ja2num($str); 
     134    } 
     135} 
     136 
    92137sub _ascii2ja { 
    93138    my($self, $ascii) = @_; 
     
    125170__END__ 
    126171 
     172=encoding utf-8 
     173 
    127174=head1 NAME 
    128175 
     
    148195  use Date::Japanese::Era 'JIS_X0301'; 
    149196 
     197  # more DWIMmy 
     198  use encoding 'utf-8'; 
     199  $era = Date::Japanese::Era->new("昭和五十二年"); 
     200  $era = Date::Japanese::Era->new("昭和52年"); 
    150201 
    151202=head1 DESCRIPTION 
     
    162213  $era = Date::Japanese::Era->new($year, $month, $day); 
    163214  $era = Date::Japanese::Era->new($era_name, $year); 
     215  $era = Date::Japanese::Era->new($era_year_string); 
    164216 
    165217Constructs new Date::Japanese::Era instance. When constructed from 
  • Date-Japanese-Era/trunk/t/00_Era.t

    r1864 r1865  
    11use strict; 
    2 use Test::More tests => 35
     2use Test::More tests => 47
    33 
    44BEGIN { use_ok('Date::Japanese::Era'); } 
     
    5656is($era->name, $utf8, 'input / output UTF-8'); 
    5757 
     58{ 
     59    my @era; 
     60    push @era, Date::Japanese::Era->new('昭和52年'); 
     61    push @era, Date::Japanese::Era->new('昭和52'); 
     62    push @era, Date::Japanese::Era->new('昭和52年'); 
     63    push @era, Date::Japanese::Era->new('昭和52'); 
     64    push @era, Date::Japanese::Era->new('昭和五十二年'); 
     65    push @era, Date::Japanese::Era->new('昭和五十二'); 
     66 
     67    for my $e (@era) { 
     68        is $e->name, '昭和'; 
     69        is $e->year, 52; 
     70    } 
     71} 
    5872 
    5973