Changeset 1864

Show
Ignore:
Timestamp:
06/15/06 18:08:09
Author:
miyagawa
Message:

refactoring how to handle encodings

Files:

Legend:

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

    r369 r1864  
    11Revision history for Perl extension Date::Japanese::Era. 
     2 
     30.04  Thu Jun 15 10:27:08 JST 2006 
     4        * [BACKWARD INCOMPATIBLE CHANGE] 
     5          Refactored how to handle encodings. Deprecated old codeset() 
     6          APIs and now it just requires Unicode flagged string as method 
     7          input/output. 
    28 
    390.03  Mon Dec 10 16:47:30 JST 2001 
  • Date-Japanese-Era/trunk/MANIFEST

    r369 r1864  
    33Makefile.PL 
    44README 
    5 lib/Date/Japanese/Era.ja.pod 
    65lib/Date/Japanese/Era.pm 
    76lib/Date/Japanese/Era/Table.pm 
  • Date-Japanese-Era/trunk/Makefile.PL

    r230 r1864  
    77    'PREREQ_PM' => { 
    88        Date::Calc => 4.3, 
    9         Jcode      => 0.75, 
    109        Test::More => 0, 
     10        Encode => 2.1, 
    1111    }, 
    1212); 
  • Date-Japanese-Era/trunk/lib/Date/Japanese/Era.pm

    r369 r1864  
    22 
    33use strict; 
    4 use vars qw($VERSION); 
    5 $VERSION = '0.03'; 
     4our $VERSION = '0.04'; 
    65 
    76use Carp; 
     
    98 
    109use vars qw(@ISA @EXPORT %ERA_TABLE %ERA_JA2ASCII %ERA_ASCII2JA); 
    11  
    12 use vars qw($Have_Jcode); 
    13 BEGIN { 
    14     $Have_Jcode = 0; 
    15     eval { require Jcode; $Have_Jcode++; }; 
    16 } 
    17  
    18 { 
    19     my $codeset = 'euc'; 
    20     sub codeset { 
    21         my $proto = shift; 
    22         if (@_) { 
    23             carp "Jcode is required to modify codeset. Ignored." 
    24                 unless $Have_Jcode; 
    25             $codeset = shift; 
    26         } 
    27         $codeset; 
    28     } 
    29 } 
    3010 
    3111sub import { 
     
    5737    } 
    5838    else { 
    59         croak "odd number of arguments: @args"
     39        croak "odd number of arguments: ", scalar(@args)
    6040    } 
    6141    return $self; 
     
    8969sub _from_era { 
    9070    my($self, $era, $year) = @_; 
    91     if ($era =~ /^\w+$/) { 
     71    if ($era =~ /^[a-zA-Z]+$/) { 
    9272        $era = $self->_ascii2ja($era); 
    9373    } 
    94     elsif ($Have_Jcode) { 
    95         $era = Jcode->new($era, $self->codeset)->euc; 
    96     } 
    97  
    98     unless (exists $ERA_TABLE{$era}) { 
    99         croak "Unknown era name: $era"; 
    100     } 
    101     my $data = $ERA_TABLE{$era}; 
     74 
     75    unless (utf8::is_utf8($era)) { 
     76        croak "Era needs to be Unicode string"; 
     77    } 
     78 
     79    my $data = $ERA_TABLE{$era} 
     80        or croak "Unknown era name: $era"; 
     81 
    10282    my $g_year = $data->[1] + $year - 1; 
    10383    if ($g_year > $data->[4]) { 
     
    122102sub name { 
    123103    my $self = shift; 
    124     if ($Have_Jcode) { 
    125         my $encoding = $self->codeset; 
    126         return Jcode->new($self->{name}, 'euc')->$encoding(); 
    127     } 
    128104    return $self->{name}; 
    129105} 
     
    161137 
    162138  # from Japanese Era 
    163   $era = Date::Japanese::Era->new('ŸŒÏÂ', 52); 
    164  
    165   $name      = $era->name;         # 'ŸŒÏÂ' in EUC-jp (default
    166   $gengou    = $era->gengou;       # same 
     139  $era = Date::Japanese::Era->new("\x{662D}\x{548C}", 52); # SHOWA 
     140 
     141  $name      = $era->name;         # \x{662D}\x{548C} (Unicode flagged
     142  $gengou    = $era->gengou;       # Ditto 
    167143 
    168144  $year      = $era->year;         # 52 
     
    181157 
    182158=over 4 
    183  
    184 =item codeset 
    185  
    186   $codeset = Date::Japanese::Era->codeset; 
    187   Date::Japanese::Era->codeset($encoding); 
    188  
    189 sets / gets external encoding of Japanese era names. For example with 
    190 the following code, input and output of era names are encoded in UTF-8. 
    191  
    192   Date::Japanese::Era->codeset('utf8'); 
    193   $era = Date::Japanese::Era->new($name, $year); # $name is UTF-8 
    194   print $era->name;                              # also UTF-8 
    195  
    196 You need Jcode module installed to make use of this 
    197 feature. Otherwise, calls to codeset() are simply ignored (with 
    198 warning). 
    199159 
    200160=item new 
     
    207167construct from Gregorian. 
    208168 
    209 Name of era can be either of Japanese / ASCII. Input encodings can be 
    210 specified via codeset(), suppose you have Jcode module 
    211 installed. Default is EUC-JP. 
     169Name of era can be either of Japanese / ASCII. If you pass Japanese, 
     170the variable should be properly UTF-8 flaged. 
    212171 
    213172Exceptions are thrown when inputs are invalid (e.g: non-existent 
     
    218177  $name = $era->name; 
    219178 
    220 returns era name in Japanese. Encoding can be specified via codeset() 
    221 class method. Default is EUC-JP. 
     179returns era name in Japanese in Unicode. 
    222180 
    223181=item gengou 
     
    254212 
    255213  # to Gregorian 
    256   my $era = Date::Japanese::Era->new('Ê¿À®', 13); 
     214  my $era = Date::Japanese::Era->new("\x{5E73}\x{6210}", 13); # HEISEI 13 
    257215  print $era->gregorian_year;   # 2001 
    258216 
     
    268226cases ("In %d they didn't use gregorius calendar"). 
    269227 
     228To use calendar ealier than that, see 
     229L<DateTime::Calendar::Japanese::Era>, which is based on DateTime 
     230framework and is more comprehensive. 
     231 
    270232=item * 
    271233 
     
    289251=back 
    290252 
    291 =head1 TODO 
    292  
    293 =over 4 
    294  
    295 =item * 
    296  
    297 Date parameters can be in various format. I should replace 
    298 Date::Simple or whatever for that. 
    299  
    300 =item * 
    301  
    302 Support earlier eras and lunar calendar. 
    303  
    304 =back 
    305  
    306253=head1 AUTHOR 
    307254 
     
    313260=head1 SEE ALSO 
    314261 
    315 L<Date::Calc>, L<Jcode>, L<Date::Simple> 
     262L<DateTime::Calendar::Japanese::Era>, L<Date::Calc>, L<Encode> 
    316263 
    317264=cut 
  • Date-Japanese-Era/trunk/lib/Date/Japanese/Era/Table.pm

    r230 r1864  
    33use strict; 
    44use vars qw($VERSION); 
    5 $VERSION = '0.01'; 
     5$VERSION = '0.04'; 
    66 
    77use vars qw(@ISA @EXPORT %ERA_TABLE %ERA_JA2ASCII %ERA_ASCII2JA); 
     
    1212%ERA_TABLE = ( 
    1313    # era => [ $ascii, @begin_ymd, @end_ymd ] 
    14     'ÌÀŒ£' => [ 'meiji', 1868, 9, 8, 1912, 7, 29 ], 
    15     'ÂçÀµ' => [ 'taishou', 1912, 7, 30, 1926, 12, 24 ], 
    16     'ŸŒÏÂ' => [ 'shouwa', 1926, 12, 25, 1989, 1, 7 ], 
    17     'Ê¿À®' => [ 'heisei', 1989, 1, 8, 2999, 12, 31 ], # XXX 
     14    "\x{660E}\x{6CBB}" => [ 'meiji', 1868, 9, 8, 1912, 7, 29 ], 
     15    "\x{5927}\x{6B63}" => [ 'taishou', 1912, 7, 30, 1926, 12, 24 ], 
     16    "\x{662D}\x{548C}" => [ 'shouwa', 1926, 12, 25, 1989, 1, 7 ], 
     17    "\x{5E73}\x{6210}" => [ 'heisei', 1989, 1, 8, 2999, 12, 31 ], # XXX 
    1818); 
    1919 
  • Date-Japanese-Era/trunk/lib/Date/Japanese/Era/Table/JIS_X0301.pm

    r352 r1864  
    1212%ERA_TABLE = ( 
    1313    # era => [ $ascii, @begin_ymd, @end_ymd ] 
    14     'ÌÀŒ£' => [ 'meiji', 1868, 9, 8, 1912, 7, 30 ], 
    15     'ÂçÀµ' => [ 'taishou', 1912, 7, 31, 1926, 12, 25 ], 
    16     'ŸŒÏÂ' => [ 'shouwa', 1926, 12, 26, 1989, 1, 7 ], 
    17     'Ê¿À®' => [ 'heisei', 1989, 1, 8, 2999, 12, 31 ], # XXX 
     14    "\x{660E}\x{6CBB}" => [ 'meiji', 1868, 9, 8, 1912, 7, 30 ], 
     15    "\x{5927}\x{6B63}" => [ 'taishou', 1912, 7, 31, 1926, 12, 25 ], 
     16    "\x{662D}\x{548C}" => [ 'shouwa', 1926, 12, 26, 1989, 1, 7 ], 
     17    "\x{5E73}\x{6210}" => [ 'heisei', 1989, 1, 8, 2999, 12, 31 ], # XXX 
    1818); 
    1919 
  • Date-Japanese-Era/trunk/t/00_Era.t

    r352 r1864  
    44BEGIN { use_ok('Date::Japanese::Era'); } 
    55 
     6use encoding "utf-8"; 
     7 
    68my @tests = ( 
    7     [ 2001, 9, 1, 'Ê¿À®', 13 ], 
    8     [ 1989, 1, 8, 'Ê¿À®', 1 ], 
    9     [ 1989, 1, 7, 'ŸŒÏÂ', 64 ], 
    10     [ 1977, 9, 12, 'ŸŒÏÂ', 52 ], 
    11     [ 1926, 12, 25, 'ŸŒÏÂ', 1 ], 
     9    [ 2001, 9, 1, '平成', 13 ], 
     10    [ 1989, 1, 8, '平成', 1 ], 
     11    [ 1989, 1, 7, '昭和', 64 ], 
     12    [ 1977, 9, 12, '昭和', 52 ], 
     13    [ 1926, 12, 25, '昭和', 1 ], 
    1214    [ 1926, 12, 24, 'taishou', 15 ], 
    1315    [ 1912, 7, 30, 'taishou', 1 ], 
     
    1921    my($year, $month, $day, $name, $era_year) = @$test; 
    2022    my $e1 = Date::Japanese::Era->new($year, $month, $day); 
    21     if ($name =~ /^\w+$/) { 
     23    if ($name =~ /^[a-zA-Z]+$/) { 
    2224        is($e1->name_ascii, $name, 'Gregorian to Japanese era (ASCII)'); 
    2325    } 
    2426    else { 
    25         is($e1->name('euc'), $name, 'Gregorian to Japanese era'); 
     27        is($e1->name, $name, 'Gregorian to Japanese era'); 
    2628    } 
    2729    is($e1->year, $era_year); 
     
    3638    [ [],  'odd number of arguments: ' ], 
    3739    [ [ 'xxx', 1 ], 'Unknown era name: ' ], 
    38     [ [ '·Ä±þ', 12 ], 'Unknown era name: ' ], 
    39     [ [ 'ŸŒÏÂ', 65 ], 'Invalid combination of era and year: ' ], 
     40    [ [ '慶応', 12 ], 'Unknown era name: ' ], 
     41    [ [ '昭和', 65 ], 'Invalid combination of era and year: ' ], 
    4042    [ [ 1868, 9, 7 ], 'Unsupported date: ' ], 
    4143    [ [ 2000, -1, -1 ], 'not a valid date' ], # XXX depends on D::Calc 
     
    5052} 
    5153 
    52 SKIP: { 
    53     skip 'Jcode not installed', 1 unless $Date::Japanese::Era::Have_Jcode; 
    54     my $utf8 = "\xe6\x98\xad\xe5\x92\x8c";      # ŸŒÏ 
    55     Date::Japanese::Era->codeset('utf8'); 
    56     my $era = Date::Japanese::Era->new($utf8, 52); 
    57     is($era->name, $utf8, 'input / output UTF-8'); 
    58 }; 
     54my $utf8 = "\xe6\x98\xad\xe5\x92\x8c";  # 昭和 
     55my $era = Date::Japanese::Era->new($utf8, 52); 
     56is($era->name, $utf8, 'input / output UTF-8'); 
    5957 
    6058 
    6159 
    62  
    63  
  • Date-Japanese-Era/trunk/t/01_Jis.t

    r352 r1864  
    22use Test::More tests => 27; 
    33 
     4use encoding "utf-8"; 
    45use Date::Japanese::Era 'JIS_X0301'; 
    56 
    67my @tests = ( 
    7     [ 2001, 9, 1, 'Ê¿À®', 13 ], 
    8     [ 1989, 1, 8, 'Ê¿À®', 1 ], 
    9     [ 1989, 1, 7, 'ŸŒÏÂ', 64 ], 
    10     [ 1977, 9, 12, 'ŸŒÏÂ', 52 ], 
    11     [ 1926, 12, 26, 'ŸŒÏÂ', 1 ], 
     8    [ 2001, 9, 1, '平成', 13 ], 
     9    [ 1989, 1, 8, '平成', 1 ], 
     10    [ 1989, 1, 7, '昭和', 64 ], 
     11    [ 1977, 9, 12, '昭和', 52 ], 
     12    [ 1926, 12, 26, '昭和', 1 ], 
    1213    [ 1926, 12, 25, 'taishou', 15 ], 
    1314    [ 1912, 7, 31, 'taishou', 1 ], 
     
    1920    my($year, $month, $day, $name, $era_year) = @$test; 
    2021    my $e1 = Date::Japanese::Era->new($year, $month, $day); 
    21     if ($name =~ /^\w+$/) { 
     22    if ($name =~ /^[a-z]+$/) { 
    2223        is($e1->name_ascii, $name, 'Gregorian to Japanese era (ASCII)'); 
    2324    } 
    2425    else { 
    25         is($e1->name('euc'), $name, 'Gregorian to Japanese era'); 
     26        is($e1->name, $name, 'Gregorian to Japanese era'); 
    2627    } 
    2728    is($e1->year, $era_year);