blob: 71f2b1673d07115e54efa6a5157774563a211d58 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001package Spreadsheet::ParseExcel::FmtJapan;
2use utf8;
3
4###############################################################################
5#
6# Spreadsheet::ParseExcel::FmtJapan - A class for Cell formats.
7#
8# Used in conjunction with Spreadsheet::ParseExcel.
9#
10# Copyright (c) 2009 John McNamara
11# Copyright (c) 2006-2008 Gabor Szabo
12# Copyright (c) 2000-2006 Kawai Takanori
13#
14# perltidy with standard settings.
15#
16# Documentation after __END__
17#
18
19use strict;
20use warnings;
21
22use Encode qw(find_encoding decode);
23use base 'Spreadsheet::ParseExcel::FmtDefault';
24our $VERSION = '0.59';
25
26my %FormatTable = (
27 0x00 => '@',
28 0x01 => '0',
29 0x02 => '0.00',
30 0x03 => '#,##0',
31 0x04 => '#,##0.00',
32 0x05 => '(\\#,##0_);(\\#,##0)',
33 0x06 => '(\\#,##0_);[RED](\\#,##0)',
34 0x07 => '(\\#,##0.00_);(\\#,##0.00_)',
35 0x08 => '(\\#,##0.00_);[RED](\\#,##0.00_)',
36 0x09 => '0%',
37 0x0A => '0.00%',
38 0x0B => '0.00E+00',
39 0x0C => '# ?/?',
40 0x0D => '# ??/??',
41
42 # 0x0E => 'm/d/yy',
43 0x0E => 'yyyy/m/d',
44 0x0F => 'd-mmm-yy',
45 0x10 => 'd-mmm',
46 0x11 => 'mmm-yy',
47 0x12 => 'h:mm AM/PM',
48 0x13 => 'h:mm:ss AM/PM',
49 0x14 => 'h:mm',
50 0x15 => 'h:mm:ss',
51
52 # 0x16 => 'm/d/yy h:mm',
53 0x16 => 'yyyy/m/d h:mm',
54
55 #0x17-0x24 -- Differs in Natinal
56 0x1E => 'm/d/yy',
57 0x1F => 'yyyy"年"m"月"d"日"',
58 0x20 => 'h"時"mm"分"',
59 0x21 => 'h"時"mm"分"ss"秒"',
60
61 #0x17-0x24 -- Differs in Natinal
62 0x25 => '(#,##0_);(#,##0)',
63 0x26 => '(#,##0_);[RED](#,##0)',
64 0x27 => '(#,##0.00);(#,##0.00)',
65 0x28 => '(#,##0.00);[RED](#,##0.00)',
66 0x29 => '_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)',
67 0x2A => '_(\\*#,##0_);_(\\*(#,##0);_(*"-"_);_(@_)',
68 0x2B => '_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)',
69 0x2C => '_(\\*#,##0.00_);_(\\*(#,##0.00);_(*"-"??_);_(@_)',
70 0x2D => 'mm:ss',
71 0x2E => '[h]:mm:ss',
72 0x2F => 'mm:ss.0',
73 0x30 => '##0.0E+0',
74 0x31 => '@',
75
76 0x37 => 'yyyy"年"m"月"',
77 0x38 => 'm"月"d"日"',
78 0x39 => 'ge.m.d',
79 0x3A => 'ggge"年"m"月"d"日"',
80);
81
82#------------------------------------------------------------------------------
83# new (for Spreadsheet::ParseExcel::FmtJapan)
84#------------------------------------------------------------------------------
85sub new {
86 my ( $class, %args ) = @_;
87 my $encoding = $args{Code} || $args{encoding};
88 my $self = { Code => $encoding };
89 if($encoding){
90 $self->{encoding} = find_encoding($encoding eq 'sjis' ? 'cp932' : $encoding)
91 or do{
92 require Carp;
93 Carp::croak(qq{Unknown encoding '$encoding'});
94 };
95 }
96 return bless $self, $class;
97}
98
99#------------------------------------------------------------------------------
100# TextFmt (for Spreadsheet::ParseExcel::FmtJapan)
101#------------------------------------------------------------------------------
102sub TextFmt {
103 my ( $self, $text, $input_encoding ) = @_;
104 if(!defined $input_encoding){
105 $input_encoding = 'utf8';
106 }
107 elsif($input_encoding eq '_native_'){
108 $input_encoding = 'cp932'; # Shift_JIS in Microsoft products
109 }
110 $text = decode($input_encoding, $text);
111 return $self->{Code} ? $self->{encoding}->encode($text) : $text;
112}
113#------------------------------------------------------------------------------
114# FmtStringDef (for Spreadsheet::ParseExcel::FmtJapan)
115#------------------------------------------------------------------------------
116sub FmtStringDef {
117 my ( $self, $format_index, $book ) = @_;
118 return $self->SUPER::FmtStringDef( $format_index, $book, \%FormatTable );
119}
120
121#------------------------------------------------------------------------------
122# CnvNengo (for Spreadsheet::ParseExcel::FmtJapan)
123#------------------------------------------------------------------------------
124
125# Convert A.D. into Japanese Nengo (aka Gengo)
126
127my @Nengo = (
128 {
129 name => '平成', # Heisei
130 abbr_name => 'H',
131
132 base => 1988,
133 start => 19890108,
134 },
135 {
136 name => '昭和', # Showa
137 abbr_name => 'S',
138
139 base => 1925,
140 start => 19261225,
141 },
142 {
143 name => '大正', # Taisho
144 abbr_name => 'T',
145
146 base => 1911,
147 start => 19120730,
148 },
149 {
150 name => '明治', # Meiji
151 abbr_name => 'M',
152
153 base => 1867,
154 start => 18680908,
155 },
156);
157
158# Usage: CnvNengo(name => @tm) or CnvNeng(abbr_name => @tm)
159sub CnvNengo {
160 my ( $kind, @tm ) = @_;
161 my $year = $tm[5] + 1900;
162 my $wk = ($year * 10000) + ($tm[4] * 100) + ($tm[3] * 1);
163 #my $wk = sprintf( '%04d%02d%02d', $year, $tm[4], $tm[3] );
164 foreach my $nengo(@Nengo){
165 if( $wk >= $nengo->{start} ){
166 return $nengo->{$kind} . ($year - $nengo->{base});
167 }
168 }
169 return $year;
170}
171
1721;
173
174__END__
175
176=pod
177
178=head1 NAME
179
180Spreadsheet::ParseExcel::FmtJapan - A class for Cell formats.
181
182=head1 SYNOPSIS
183
184See the documentation for Spreadsheet::ParseExcel.
185
186=head1 DESCRIPTION
187
188This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
189
190=head1 AUTHOR
191
192Maintainer 0.40+: John McNamara jmcnamara@cpan.org
193
194Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
195
196Original author: Kawai Takanori kwitknr@cpan.org
197
198=head1 COPYRIGHT
199
200Copyright (c) 2009-2010 John McNamara
201
202Copyright (c) 2006-2008 Gabor Szabo
203
204Copyright (c) 2000-2006 Kawai Takanori
205
206All rights reserved.
207
208You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
209
210=cut