line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Date::Japanese::Era; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
70650
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
129
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
219
|
|
7
|
2
|
|
|
2
|
|
12
|
use constant END_OF_LUNAR => 1872; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
198
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
28
|
use vars qw(@ISA @EXPORT %ERA_TABLE %ERA_JA2ASCII %ERA_ASCII2JA); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
3041
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub import { |
12
|
2
|
|
|
2
|
|
17
|
my $self = shift; |
13
|
2
|
100
|
|
|
|
13
|
if (@_) { |
14
|
1
|
|
|
|
|
4
|
my $table = shift; |
15
|
1
|
|
|
1
|
|
23
|
eval qq{use Date::Japanese::Era::Table::$table}; |
|
1
|
|
|
|
|
1404
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
162
|
|
16
|
1
|
50
|
|
|
|
31
|
die $@ if $@; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
else { |
19
|
1
|
|
|
|
|
465
|
require Date::Japanese::Era::Table; |
20
|
1
|
|
|
|
|
89
|
import Date::Japanese::Era::Table; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub new { |
25
|
49
|
|
|
49
|
1
|
14534
|
my($class, @args) = @_; |
26
|
49
|
|
|
|
|
267
|
my $self = bless { |
27
|
|
|
|
|
|
|
name => undef, |
28
|
|
|
|
|
|
|
year => undef, |
29
|
|
|
|
|
|
|
gregorian_year => undef, |
30
|
|
|
|
|
|
|
}, $class; |
31
|
|
|
|
|
|
|
|
32
|
49
|
100
|
|
|
|
156
|
if (@args == 3) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
33
|
20
|
|
|
|
|
51
|
$self->_from_ymd(@args); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
elsif (@args == 2) { |
36
|
22
|
|
|
|
|
57
|
$self->_from_era(@args); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif (@args == 1) { |
39
|
6
|
|
|
|
|
16
|
$self->_dwim(@args); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
1
|
|
|
|
|
192
|
croak "odd number of arguments: ", scalar(@args); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
43
|
|
|
|
|
154
|
return $self; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _from_ymd { |
49
|
20
|
|
|
20
|
|
41
|
my($self, @ymd) = @_; |
50
|
|
|
|
|
|
|
|
51
|
20
|
100
|
|
|
|
54
|
if ($ymd[0] <= END_OF_LUNAR) { |
52
|
1
|
|
|
|
|
187
|
Carp::carp("In $ymd[0] they didn't use gregorious date."); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
20
|
|
|
|
|
2161
|
require Date::Calc; # not 'use' |
56
|
20
|
|
|
|
|
74763
|
*Delta_Days = \&Date::Calc::Delta_Days; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# XXX can be more efficient |
59
|
20
|
|
|
|
|
66
|
for my $era (keys %ERA_TABLE) { |
60
|
49
|
|
|
|
|
1567
|
my $data = $ERA_TABLE{$era}; |
61
|
49
|
100
|
100
|
|
|
70
|
if (Delta_Days(@{$data}[1..3], @ymd) >= 0 && |
|
49
|
|
|
|
|
165
|
|
|
27
|
|
|
|
|
1504
|
|
62
|
|
|
|
|
|
|
Delta_Days(@ymd, @{$data}[4..6]) >= 0) { |
63
|
18
|
|
|
|
|
920
|
$self->{name} = $era; |
64
|
18
|
|
|
|
|
39
|
$self->{year} = $ymd[0] - $data->[1] + 1; |
65
|
18
|
|
|
|
|
28
|
$self->{gregorian_year} = $ymd[0]; |
66
|
18
|
|
|
|
|
42
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
203
|
croak "Unsupported date: ", join('-', @ymd); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _from_era { |
74
|
28
|
|
|
28
|
|
49
|
my($self, $era, $year) = @_; |
75
|
28
|
100
|
|
|
|
177
|
if ($era =~ /^[a-zA-Z]+$/) { |
76
|
9
|
|
|
|
|
26
|
$era = $self->_ascii2ja($era); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
27
|
50
|
|
|
|
106
|
unless (utf8::is_utf8($era)) { |
80
|
0
|
|
|
|
|
0
|
croak "Era needs to be Unicode string"; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
27
|
100
|
|
|
|
85
|
my $data = $ERA_TABLE{$era} |
84
|
|
|
|
|
|
|
or croak "Unknown era name: $era"; |
85
|
|
|
|
|
|
|
|
86
|
26
|
|
|
|
|
53
|
my $g_year = $data->[1] + $year - 1; |
87
|
26
|
100
|
|
|
|
66
|
if ($g_year > $data->[4]) { |
88
|
1
|
|
|
|
|
8
|
croak "Invalid combination of era and year: $era-$year"; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
25
|
|
|
|
|
41
|
$self->{name} = $era; |
92
|
25
|
|
|
|
|
40
|
$self->{year} = $year; |
93
|
25
|
|
|
|
|
57
|
$self->{gregorian_year} = $g_year; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _dwim { |
97
|
6
|
|
|
6
|
|
9
|
my($self, $str) = @_; |
98
|
|
|
|
|
|
|
|
99
|
6
|
50
|
|
|
|
17
|
unless (utf8::is_utf8($str)) { |
100
|
0
|
|
|
|
|
0
|
croak "Era should be Unicode flagged"; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
231
|
my $gengou_re = join "|", keys %ERA_JA2ASCII; |
104
|
|
|
|
|
|
|
|
105
|
6
|
50
|
|
|
|
155
|
$str =~ s/^($gengou_re)// |
106
|
|
|
|
|
|
|
or croak "Can't extract Era from $str"; |
107
|
|
|
|
|
|
|
|
108
|
6
|
|
|
|
|
62
|
my $era = $1; |
109
|
|
|
|
|
|
|
|
110
|
6
|
|
|
|
|
26
|
$str =~ s/\x{5E74}$//; # nen |
111
|
6
|
|
|
|
|
27
|
my $year = _number($str); |
112
|
|
|
|
|
|
|
|
113
|
6
|
50
|
|
|
|
92996
|
unless (defined $year) { |
114
|
0
|
|
|
|
|
0
|
croak "Can't parse year from $str"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
6
|
|
|
|
|
18
|
$self->_from_era($era, $year); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _number { |
121
|
6
|
|
|
6
|
|
7
|
my $str = shift; |
122
|
|
|
|
|
|
|
|
123
|
6
|
|
|
|
|
25
|
$str =~ s/([\x{FF10}-\x{FF19}])/;ord($1)-0xff10/eg; |
|
4
|
|
|
|
|
13
|
|
124
|
|
|
|
|
|
|
|
125
|
6
|
100
|
|
|
|
28
|
if ($str =~ /^\d+$/) { |
126
|
4
|
|
|
|
|
21
|
return $str; |
127
|
|
|
|
|
|
|
} else { |
128
|
2
|
|
|
|
|
3
|
eval { require Lingua::JA::Numbers }; |
|
2
|
|
|
|
|
1190
|
|
129
|
2
|
50
|
|
|
|
47198
|
if ($@) { |
130
|
0
|
|
|
|
|
0
|
croak "require Lingua::JA::Numbers to read Japanized numbers"; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
2
|
|
|
|
|
11
|
return Lingua::JA::Numbers::ja2num($str); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub _ascii2ja { |
138
|
9
|
|
|
9
|
|
15
|
my($self, $ascii) = @_; |
139
|
9
|
|
66
|
|
|
51
|
return $ERA_ASCII2JA{$ascii} || croak "Unknown era name: $ascii"; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _ja2ascii { |
143
|
8
|
|
|
8
|
|
12
|
my($self, $ja) = @_; |
144
|
8
|
|
33
|
|
|
47
|
return $ERA_JA2ASCII{$ja} || croak "Unknown era name: $ja"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub name { |
148
|
25
|
|
|
25
|
1
|
2074
|
my $self = shift; |
149
|
25
|
|
|
|
|
96
|
return $self->{name}; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
*gengou = \&name; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub name_ascii { |
155
|
8
|
|
|
8
|
1
|
67
|
my $self = shift; |
156
|
8
|
|
|
|
|
18
|
return $self->_ja2ascii($self->name); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub year { |
160
|
24
|
|
|
24
|
1
|
10574
|
my $self = shift; |
161
|
24
|
|
|
|
|
86
|
return $self->{year}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub gregorian_year { |
165
|
18
|
|
|
18
|
1
|
88
|
my $self = shift; |
166
|
18
|
|
|
|
|
61
|
return $self->{gregorian_year}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
__END__ |