| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MARC::Convert::Wikidata::Object::Utils; |
|
2
|
|
|
|
|
|
|
|
|
3
|
23
|
|
|
23
|
|
698545
|
use base qw(Exporter); |
|
|
23
|
|
|
|
|
75
|
|
|
|
23
|
|
|
|
|
2447
|
|
|
4
|
23
|
|
|
23
|
|
163
|
use strict; |
|
|
23
|
|
|
|
|
46
|
|
|
|
23
|
|
|
|
|
526
|
|
|
5
|
23
|
|
|
23
|
|
133
|
use warnings; |
|
|
23
|
|
|
|
|
251
|
|
|
|
23
|
|
|
|
|
692
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
23
|
|
|
23
|
|
21975
|
use DateTime; |
|
|
23
|
|
|
|
|
12103488
|
|
|
|
23
|
|
|
|
|
1210
|
|
|
8
|
23
|
|
|
23
|
|
6562
|
use Error::Pure qw(err); |
|
|
23
|
|
|
|
|
112033
|
|
|
|
23
|
|
|
|
|
1069
|
|
|
9
|
23
|
|
|
23
|
|
824
|
use Readonly; |
|
|
23
|
|
|
|
|
78
|
|
|
|
23
|
|
|
|
|
11514
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(check_date check_date_order); |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.01; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub check_date { |
|
16
|
73
|
|
|
73
|
1
|
9765
|
my ($self, $key) = @_; |
|
17
|
|
|
|
|
|
|
|
|
18
|
73
|
100
|
|
|
|
300
|
if (! exists $self->{$key}) { |
|
19
|
16
|
|
|
|
|
41
|
return; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
57
|
50
|
|
|
|
176
|
if (! defined $self->{$key}) { |
|
23
|
0
|
|
|
|
|
0
|
return; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Check year format. |
|
27
|
57
|
100
|
|
|
|
409
|
if ($self->{$key} !~ m/^\-?(\d{1,4})\-?\d{0,2}\-?\d{0,2}$/ms) { |
|
28
|
|
|
|
|
|
|
err "Parameter '$key' is in bad format.", |
|
29
|
2
|
|
|
|
|
19
|
'Value', $self->{$key}, |
|
30
|
|
|
|
|
|
|
; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
55
|
|
|
|
|
179
|
my $year = $1; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Check year greater than actual. |
|
35
|
55
|
100
|
|
|
|
276
|
if ($year > DateTime->now->year) { |
|
36
|
1
|
|
|
|
|
258
|
err "Parameter '$key' has year greater than actual year."; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
54
|
|
|
|
|
19517
|
return; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub check_date_order { |
|
43
|
32
|
|
|
32
|
1
|
4661
|
my ($self, $key1, $key2) = @_; |
|
44
|
|
|
|
|
|
|
|
|
45
|
32
|
100
|
100
|
|
|
317
|
if (! exists $self->{$key1} || ! exists $self->{$key2}) { |
|
46
|
10
|
|
|
|
|
26
|
return; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
22
|
50
|
33
|
|
|
183
|
if (! defined $self->{$key1} || ! defined $self->{$key2}) { |
|
50
|
0
|
|
|
|
|
0
|
return; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
22
|
|
|
|
|
101
|
my $dt1 = _construct_dt($self->{$key1}); |
|
54
|
22
|
|
|
|
|
100
|
my $dt2 = _construct_dt($self->{$key2}); |
|
55
|
|
|
|
|
|
|
|
|
56
|
22
|
|
|
|
|
175
|
my $cmp = DateTime->compare($dt1, $dt2); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# dt1 >= dt2 |
|
59
|
22
|
100
|
|
|
|
2137
|
if ($cmp != -1) { |
|
60
|
3
|
|
|
|
|
33
|
err "Parameter '$key1' has date greater or same as parameter '$key2' date."; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
19
|
|
|
|
|
190
|
return; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _construct_dt { |
|
67
|
44
|
|
|
44
|
|
101
|
my $date = shift; |
|
68
|
|
|
|
|
|
|
|
|
69
|
44
|
|
|
|
|
315
|
my ($year, $month, $day) = $date =~ m/^(\-?\d{1,4})\-?(\d{0,2})\-?(\d{0,2})$/ms; |
|
70
|
44
|
100
|
|
|
|
276
|
my $dt = DateTime->new( |
|
|
|
100
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
'year' => $year, |
|
72
|
|
|
|
|
|
|
$month ? ('month' => $month) : (), |
|
73
|
|
|
|
|
|
|
$day ? ('day' => $day) : (), |
|
74
|
|
|
|
|
|
|
); |
|
75
|
|
|
|
|
|
|
|
|
76
|
44
|
|
|
|
|
14288
|
return $dt; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=pod |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=encoding utf8 |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
MARC::Convert::Wikidata::Object::Utils - MARC::Convert::Wikidata::Object utilities. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
use MARC::Convert::Wikidata::Object::Utils qw(check_date); |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
check_date($self, $key); |
|
96
|
|
|
|
|
|
|
check_date_order($self, $key1, $key2); |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Utilities for checking of data values. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 SUBROUTINES |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 C<check_date> |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
check_date($self, $key); |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Check parameter defined by C<$key> which is date and that date isn't greater |
|
109
|
|
|
|
|
|
|
than actual year. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Possible dates: |
|
112
|
|
|
|
|
|
|
- YYYY-MM-DD |
|
113
|
|
|
|
|
|
|
- YYYY-M-D |
|
114
|
|
|
|
|
|
|
- YYYY-MM |
|
115
|
|
|
|
|
|
|
- YYYY-M |
|
116
|
|
|
|
|
|
|
- YYYY |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Put error if check isn't ok. |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Returns undef. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 C<check_date_order> |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
check_date_order($self, $key1, $key2); |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Check if date with C<$key1> is lesser than date with C<$key2>. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Put error if check isn't ok. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Returns undef. |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 ERRORS |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
check_date(): |
|
135
|
|
|
|
|
|
|
Parameter '%s' for date is in bad format. |
|
136
|
|
|
|
|
|
|
Parameter '%s' has year greater than actual year. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
check_date_order(): |
|
139
|
|
|
|
|
|
|
Parameter '%s' has date greater or same as parameter '%s' date. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=for comment filename=check_date_ok.pl |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
use strict; |
|
146
|
|
|
|
|
|
|
use warnings; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
use MARC::Convert::Wikidata::Object::Utils qw(check_date); |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
my $self = { |
|
151
|
|
|
|
|
|
|
'key' => '2022-01-15', |
|
152
|
|
|
|
|
|
|
}; |
|
153
|
|
|
|
|
|
|
check_date($self, 'key'); |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Print out. |
|
156
|
|
|
|
|
|
|
print "ok\n"; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Output: |
|
159
|
|
|
|
|
|
|
# ok |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=for comment filename=check_date_error.pl |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
use strict; |
|
166
|
|
|
|
|
|
|
use warnings; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
use Error::Pure; |
|
169
|
|
|
|
|
|
|
use MARC::Convert::Wikidata::Object::Utils qw(check_date); |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$Error::Pure::TYPE = 'Error'; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
my $self = { |
|
174
|
|
|
|
|
|
|
'key' => 'foo', |
|
175
|
|
|
|
|
|
|
}; |
|
176
|
|
|
|
|
|
|
check_date($self, 'key'); |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Print out. |
|
179
|
|
|
|
|
|
|
print "ok\n"; |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# Output like: |
|
182
|
|
|
|
|
|
|
# #Error [..Utils.pm:?] Parameter 'key' is in bad format. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L<DateTime>, |
|
187
|
|
|
|
|
|
|
L<Exporter>, |
|
188
|
|
|
|
|
|
|
L<Error::Pure>, |
|
189
|
|
|
|
|
|
|
L<Readonly>. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item L<Mo::utils> |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Mo utilities. |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 REPOSITORY |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/MARC-Convert-Wikidata-Object> |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 AUTHOR |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
L<http://skim.cz> |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
© Michal Josef Špaček 2021-2023 |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
BSD 2-Clause License |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 VERSION |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
0.01 |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |