Strptime.pm

filter: Statementstmt 96.5% 333 / 345 Branchbran 91.7% 156 / 170 Conditioncond 81.1% 56 / 69 MC/DCmcdc 60.4% 26 / 43 Subroutinesub 92.5% 37 / 40 Podpod 81.8% 9 / 11 totaltotal 91.0% 617 / 678 CC 117 SCAR 31.1
CC
117
Cov
93%
_check_dt
45.0
_munge_args
32.0
parse_datetime
13.9
CRAP 22.4
↑ Summary Strptime/Types.pm »
1 package DateTime::Format::Strptime;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 our $VERSION = '1.80';
7  
8 12 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use DateTime 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use DateTime::Locale 1.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use DateTime::Format::Strptime::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use DateTime::TimeZone 2.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 our @EXPORT_OK = qw( strftime strptime );
18  
19 ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
20 12 ▶use constant PERL_58 => $] < 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # We previously used Package::DeprecationManager which allowed passing of
23 # "-api_version => X" on import. We don't want any such imports to blow up but
24 # we no longer have anything to deprecate.
25 14 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
26 14 my $class = shift;
27 14 my @args;
28 ## no critic (ControlStructures::ProhibitCStyleForLoops)
29 14 ▶ for ( my $i = 0; $i < @_; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 3 14 17
30 3 ▶ if ( $_[$i] eq '-api_version' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq '-api_version') { } 1 2 3
31 1 $i++;
32 }
33 else {
34 2 push @args, $_[$i];
35 }
36 }
37 14 @_ = ( $class, @args );
38 14 goto &Exporter::import;
39 }
40  
41 {
42 12 my $en_locale = DateTime::Locale->load('en');
43  
44 my $validator = validation_for(
45 params => {
46 pattern => { type => t('NonEmptyStr') },
47 time_zone => {
48 type => t('TimeZone'),
49 optional => 1,
50 },
51 zone_map => {
52 type => t('HashRef'),
53 293 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 },
55 locale => {
56 type => t('Locale'),
57 157 ▶ default => sub {$en_locale},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 },
59 on_error => {
60 type => t('OnError'),
61 default => 'undef',
62 },
63 strict => {
64 type => t('Bool'),
65 default => 0,
66 },
67 debug => {
68 type => t('Bool'),
69 default => $ENV{DATETIME_FORMAT_STRPTIME_DEBUG},
70 },
71 },
72 12 );
73  
74 294 ▶ sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
75 294 my $class = shift;
76 294 my %args = $validator->(@_);
77  
78 my $self = bless {
79 %args,
80 294 zone_map => $class->_build_zone_map( $args{zone_map} ),
81 }, $class;
82  
83 # Forces a check that the pattern is valid
84 294 $self->_parser;
85  
86 293 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 293 293
87 0 ▶ binmode STDERR, ':encoding(UTF-8)' or die $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode STDERR, ":encoding(UTF-8)" or die $! 0 0 0
88 }
89  
90 293 return $self;
91 }
92 }
93  
94 {
95 12 my %zone_map = (
96 'A' => '+0100', 'ACDT' => '+1030', 'ACST' => '+0930',
97 'ADT' => undef, 'AEDT' => '+1100', 'AES' => '+1000',
98 'AEST' => '+1000', 'AFT' => '+0430', 'AHDT' => '-0900',
99 'AHST' => '-1000', 'AKDT' => '-0800', 'AKST' => '-0900',
100 'AMST' => '+0400', 'AMT' => '+0400', 'ANAST' => '+1300',
101 'ANAT' => '+1200', 'ART' => '-0300', 'AST' => undef,
102 'AT' => '-0100', 'AWST' => '+0800', 'AZOST' => '+0000',
103 'AZOT' => '-0100', 'AZST' => '+0500', 'AZT' => '+0400',
104 'B' => '+0200', 'BADT' => '+0400', 'BAT' => '+0600',
105 'BDST' => '+0200', 'BDT' => '+0600', 'BET' => '-1100',
106 'BNT' => '+0800', 'BORT' => '+0800', 'BOT' => '-0400',
107 'BRA' => '-0300', 'BST' => undef, 'BT' => undef,
108 'BTT' => '+0600', 'C' => '+0300', 'CAST' => '+0930',
109 'CAT' => undef, 'CCT' => undef, 'CDT' => undef,
110 'CEST' => '+0200', 'CET' => '+0100', 'CETDST' => '+0200',
111 'CHADT' => '+1345', 'CHAST' => '+1245', 'CKT' => '-1000',
112 'CLST' => '-0300', 'CLT' => '-0400', 'COT' => '-0500',
113 'CST' => undef, 'CSuT' => '+1030', 'CUT' => '+0000',
114 'CVT' => '-0100', 'CXT' => '+0700', 'ChST' => '+1000',
115 'D' => '+0400', 'DAVT' => '+0700', 'DDUT' => '+1000',
116 'DNT' => '+0100', 'DST' => '+0200', 'E' => '+0500',
117 'EASST' => '-0500', 'EAST' => undef, 'EAT' => '+0300',
118 'ECT' => undef, 'EDT' => undef, 'EEST' => '+0300',
119 'EET' => '+0200', 'EETDST' => '+0300', 'EGST' => '+0000',
120 'EGT' => '-0100', 'EMT' => '+0100', 'EST' => undef,
121 'ESuT' => '+1100', 'F' => '+0600', 'FDT' => undef,
122 'FJST' => '+1300', 'FJT' => '+1200', 'FKST' => '-0300',
123 'FKT' => '-0400', 'FST' => undef, 'FWT' => '+0100',
124 'G' => '+0700', 'GALT' => '-0600', 'GAMT' => '-0900',
125 'GEST' => '+0500', 'GET' => '+0400', 'GFT' => '-0300',
126 'GILT' => '+1200', 'GMT' => '+0000', 'GST' => undef,
127 'GT' => '+0000', 'GYT' => '-0400', 'GZ' => '+0000',
128 'H' => '+0800', 'HAA' => '-0300', 'HAC' => '-0500',
129 'HAE' => '-0400', 'HAP' => '-0700', 'HAR' => '-0600',
130 'HAT' => '-0230', 'HAY' => '-0800', 'HDT' => '-0930',
131 'HFE' => '+0200', 'HFH' => '+0100', 'HG' => '+0000',
132 'HKT' => '+0800', 'HL' => 'local', 'HNA' => '-0400',
133 'HNC' => '-0600', 'HNE' => '-0500', 'HNP' => '-0800',
134 'HNR' => '-0700', 'HNT' => '-0330', 'HNY' => '-0900',
135 'HOE' => '+0100', 'HST' => '-1000', 'I' => '+0900',
136 'ICT' => '+0700', 'IDLE' => '+1200', 'IDLW' => '-1200',
137 'IDT' => undef, 'IOT' => '+0500', 'IRDT' => '+0430',
138 'IRKST' => '+0900', 'IRKT' => '+0800', 'IRST' => '+0430',
139 'IRT' => '+0330', 'IST' => undef, 'IT' => '+0330',
140 'ITA' => '+0100', 'JAVT' => '+0700', 'JAYT' => '+0900',
141 'JST' => '+0900', 'JT' => '+0700', 'K' => '+1000',
142 'KDT' => '+1000', 'KGST' => '+0600', 'KGT' => '+0500',
143 'KOST' => '+1200', 'KRAST' => '+0800', 'KRAT' => '+0700',
144 'KST' => '+0900', 'L' => '+1100', 'LHDT' => '+1100',
145 'LHST' => '+1030', 'LIGT' => '+1000', 'LINT' => '+1400',
146 'LKT' => '+0600', 'LST' => 'local', 'LT' => 'local',
147 'M' => '+1200', 'MAGST' => '+1200', 'MAGT' => '+1100',
148 'MAL' => '+0800', 'MART' => '-0930', 'MAT' => '+0300',
149 'MAWT' => '+0600', 'MDT' => '-0600', 'MED' => '+0200',
150 'MEDST' => '+0200', 'MEST' => '+0200', 'MESZ' => '+0200',
151 'MET' => undef, 'MEWT' => '+0100', 'MEX' => '-0600',
152 'MEZ' => '+0100', 'MHT' => '+1200', 'MMT' => '+0630',
153 'MPT' => '+1000', 'MSD' => '+0400', 'MSK' => '+0300',
154 'MSKS' => '+0400', 'MST' => '-0700', 'MT' => '+0830',
155 'MUT' => '+0400', 'MVT' => '+0500', 'MYT' => '+0800',
156 'N' => '-0100', 'NCT' => '+1100', 'NDT' => '-0230',
157 'NFT' => undef, 'NOR' => '+0100', 'NOVST' => '+0700',
158 'NOVT' => '+0600', 'NPT' => '+0545', 'NRT' => '+1200',
159 'NST' => undef, 'NSUT' => '+0630', 'NT' => '-1100',
160 'NUT' => '-1100', 'NZDT' => '+1300', 'NZST' => '+1200',
161 'NZT' => '+1200', 'O' => '-0200', 'OESZ' => '+0300',
162 'OEZ' => '+0200', 'OMSST' => '+0700', 'OMST' => '+0600',
163 'OZ' => 'local', 'P' => '-0300', 'PDT' => '-0700',
164 'PET' => '-0500', 'PETST' => '+1300', 'PETT' => '+1200',
165 'PGT' => '+1000', 'PHOT' => '+1300', 'PHT' => '+0800',
166 'PKT' => '+0500', 'PMDT' => '-0200', 'PMT' => '-0300',
167 'PNT' => '-0830', 'PONT' => '+1100', 'PST' => undef,
168 'PWT' => '+0900', 'PYST' => '-0300', 'PYT' => '-0400',
169 'Q' => '-0400', 'R' => '-0500', 'R1T' => '+0200',
170 'R2T' => '+0300', 'RET' => '+0400', 'ROK' => '+0900',
171 'S' => '-0600', 'SADT' => '+1030', 'SAST' => undef,
172 'SBT' => '+1100', 'SCT' => '+0400', 'SET' => '+0100',
173 'SGT' => '+0800', 'SRT' => '-0300', 'SST' => undef,
174 'SWT' => '+0100', 'T' => '-0700', 'TFT' => '+0500',
175 'THA' => '+0700', 'THAT' => '-1000', 'TJT' => '+0500',
176 'TKT' => '-1000', 'TMT' => '+0500', 'TOT' => '+1300',
177 'TRUT' => '+1000', 'TST' => '+0300', 'TUC ' => '+0000',
178 'TVT' => '+1200', 'U' => '-0800', 'ULAST' => '+0900',
179 'ULAT' => '+0800', 'USZ1' => '+0200', 'USZ1S' => '+0300',
180 'USZ3' => '+0400', 'USZ3S' => '+0500', 'USZ4' => '+0500',
181 'USZ4S' => '+0600', 'USZ5' => '+0600', 'USZ5S' => '+0700',
182 'USZ6' => '+0700', 'USZ6S' => '+0800', 'USZ7' => '+0800',
183 'USZ7S' => '+0900', 'USZ8' => '+0900', 'USZ8S' => '+1000',
184 'USZ9' => '+1000', 'USZ9S' => '+1100', 'UTZ' => '-0300',
185 'UYT' => '-0300', 'UZ10' => '+1100', 'UZ10S' => '+1200',
186 'UZ11' => '+1200', 'UZ11S' => '+1300', 'UZ12' => '+1200',
187 'UZ12S' => '+1300', 'UZT' => '+0500', 'V' => '-0900',
188 'VET' => '-0400', 'VLAST' => '+1100', 'VLAT' => '+1000',
189 'VTZ' => '-0200', 'VUT' => '+1100', 'W' => '-1000',
190 'WAKT' => '+1200', 'WAST' => undef, 'WAT' => '+0100',
191 'WEST' => '+0100', 'WESZ' => '+0100', 'WET' => '+0000',
192 'WETDST' => '+0100', 'WEZ' => '+0000', 'WFT' => '+1200',
193 'WGST' => '-0200', 'WGT' => '-0300', 'WIB' => '+0700',
194 'WIT' => '+0900', 'WITA' => '+0800', 'WST' => undef,
195 'WTZ' => '-0100', 'WUT' => '+0100', 'X' => '-1100',
196 'Y' => '-1200', 'YAKST' => '+1000', 'YAKT' => '+0900',
197 'YAPT' => '+1000', 'YDT' => '-0800', 'YEKST' => '+0600',
198 'YEKT' => '+0500', 'YST' => '-0900', 'Z' => '+0000',
199 'UTC' => '+0000',
200 );
201  
202 12 for my $i ( map { sprintf( '%02d', $_ ) } 1 .. 12 ) {
203 144 $zone_map{ '-' . $i } = '-' . $i . '00';
204 144 $zone_map{ '+' . $i } = '+' . $i . '00';
205 }
206  
207 294 ▶ sub _build_zone_map {
Subroutine _build_zone_map: called CC=1 SCAR=0.0
208 return {
209 %zone_map,
210 294 %{ $_[1] },
211 };
212 }
213 }
214  
215 12 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=13 SCAR=26.3
Pod documented
216 289 my $self = shift;
217 289 my $string = shift;
218  
219 289 my $parser = $self->_parser;
220 289 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 289 289
221 0 warn "Regex for $self->{pattern}: $parser->{regex}\n";
222 0 warn "Fields: @{$parser->{fields}}\n";
223 }
224  
225 289 my @matches = ( $string =~ $parser->{regex} );
226 289 ▶ unless (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@matches) 6 283 289
227 6 my $msg = 'Your datetime does not match your pattern';
228 6 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 6 6
229 0 $msg .= qq{ - string = "$string" - regex = $parser->{regex}};
230 }
231 6 $msg .= q{.};
232 6 $self->_our_croak($msg);
233 2 return;
234 }
235  
236 283 my %args;
237 283 my $i = 0;
238 283 for my $f ( @{ $parser->{fields} } ) {
239 1109 ▶ unless ( defined $matches[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $matches[$i]) 0 1109 1109
240 0 die
241 "Something horrible happened - the string matched $parser->{regex}"
242 0 . " but did not return the expected fields: [@{$parser->{fields}}]";
243 }
244 1109 $args{$f} = $matches[ $i++ ];
245 }
246  
247 # We need to copy the %args here because _munge_args will delete keys in
248 # order to turn this into something that can be passed to a DateTime
249 # constructor.
250 283 my ( $constructor, $args, $post_construct )
251 = $self->_munge_args( {%args} );
252 272 ▶ return unless $constructor && $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $constructor and $args 5 267 272
Condition66%2/3
$constructor and $args
!ll&&!rl&&r
50267
MC/DC50%1/2
$constructor and $args
$constructor $args
Truth table66%2/3
A and B
A= $constructorB= $args
ABresult
0X0
100
111
253  
254 267 ▶ my $dt = try { DateTime->$constructor($args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
255 267 ▶ $self->_our_croak('Parsed values did not produce a valid date')
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 9 258 267
256 unless $dt;
257 261 ▶ if ($post_construct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_construct) 2 259 261
258 2 $post_construct->($dt);
259 }
260 261 ▶ return unless $dt && $self->_check_dt( $dt, \%args );
Branch: 1 branch
BranchTrueFalseTotal
unless $dt and $self->_check_dt($dt, \%args) 25 192 217
Condition100%3/3
$dt and $self->_check_dt($dt, \%args)
!ll&&!rl&&r
366192
MC/DC100%2/2
$dt and $self->_check_dt($dt, \%args)
$dt $self->_check_dt($dt, \%args)
Truth table100%3/3
A and B
A= $dtB= $self->_check_dt($dt, \%args)
ABresult
0X0
100
111
261  
262 $dt->set_time_zone( $self->{time_zone} )
263 192 ▶ if $self->{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"time_zone"} 1 191 192
264  
265 192 return $dt;
266 }
267  
268 583 ▶sub _parser {
Subroutine _parser: called CC=2 SCAR=7.1
269 583 my $self = shift;
270  
271 583 ▶ return $self->{parser} ||= $self->_build_parser;
Condition66%2/3
$self->{"parser"} ||= $self->_build_parser
l!l&&r!l&&!r
2892940
MC/DC0%0/2
$self->{"parser"} ||= $self->_build_parser
$self->{"parser"} $self->_build_parser
Truth table66%2/3
A ||= B
A= $self->{"parser"}B= $self->_build_parser
ABresult
1X1
011
000
272 }
273  
274 294 ▶sub _build_parser {
Subroutine _build_parser: called CC=12 SCAR=25.0
275 294 my $self = shift;
276  
277 my (
278 294 $replacement_tokens_re,
279 $replacements,
280 $pattern_tokens_re,
281 $patterns,
282 ) = $self->_parser_pieces;
283  
284 294 my $pattern = $self->{pattern};
285  
286 # When the first replacement is a glibc pattern, the first round of
287 # replacements may simply replace one replacement token (like %X) with
288 # another replacement token (like %I).
289 294 $pattern =~ s/%($replacement_tokens_re)/$replacements->{$1}/g for 1 .. 2;
290  
291 294 ▶ if ( $self->{debug} && $pattern ne $self->{pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} and $pattern ne $self->{"pattern"}) 0 294 294
Condition33%1/3
$self->{"debug"} and $pattern ne $self->{"pattern"}
!ll&&!rl&&r
29400
MC/DC0%0/2
$self->{"debug"} and $pattern ne $self->{"pattern"}
$self->{"debug"} $pattern ne $self->{"pattern"}
Truth table33%1/3
A and B
A= $self->{"debug"}B= $pattern ne $self->{"pattern"}
ABresult
0X0
100
111
292 0 warn "Pattern after replacement substitution: $pattern\n";
293 }
294  
295 294 my $regex = q{};
296 294 my @fields;
297  
298 294 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ / \G %($pattern_tokens_re) | %([1-9]?)(N) | (%[0-9]*[a-zA-Z]) | ([^%]+) /gx) 1978 293 2271
299 $pattern =~ /
300 \G
301 %($pattern_tokens_re)
302 |
303 %([1-9]?)(N)
304 |
305 (%[0-9]*[a-zA-Z])
306 |
307 ([^%]+)
308 /xg
309 ) {
310 # Using \G in the regex match fails for some reason on Perl 5.8, so we
311 # do this hack instead.
312 1978 substr( $pattern, 0, pos $pattern, q{} )
313 if PERL_58;
314 1978 ▶ if ($1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1) { } 1139 839 1978
elsif ($3) { } 6 833 839
elsif ($4) { } 1 832 833
315 1139 ▶ my $p = $patterns->{$1}
Branch: 1 branch
BranchTrueFalseTotal
my $p = $patterns->{$1} or croak(qq[Unidentified token in pattern: $1 in $self->{"pattern"}]) 1139 0 1139
316 or croak
317 "Unidentified token in pattern: $1 in $self->{pattern}";
318 1139 ▶ if ( $p->{field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'field'}) { } 1128 11 1139
319 1128 $regex .= qr/($p->{regex})/;
320 1128 push @fields, $p->{field};
321 }
322 else {
323 11 $regex .= qr/$p->{regex}/;
324 }
325 }
326 elsif ($3) {
327 6 ▶ $regex .= $2 ? qr/([0-9]{$2})/ : qr/([0-9]+)/;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 2 4 6
328 6 push @fields, 'nanosecond';
329 }
330 elsif ($4) {
331 1 croak qq{Pattern contained an unrecognized strptime token, "$4"};
332 }
333 else {
334 832 $regex .= qr/\Q$5/;
335 }
336 }
337  
338 return {
339 regex => (
340 293 ▶ $self->{strict} ? qr/(?:\A|\b)$regex(?:\b|\Z)/ : qr/$regex/
Branch: 1 branch
BranchTrueFalseTotal
$self->{'strict'} ? : 8 285 293
341 ),
342 fields => \@fields,
343 };
344 }
345  
346 {
347 12 my $digit = qr/(?:[0-9])/;
348 12 my $one_or_two_digits = qr/[0-9 ]?$digit/;
349  
350 # These patterns are all locale-independent. There are a few that depend
351 # on the locale, and must be re-calculated for each new parser object.
352 12 my %universal_patterns = (
353 '%' => {
354 regex => qr/%/,
355 },
356 C => {
357 regex => $one_or_two_digits,
358 field => 'century',
359 },
360 d => {
361 regex => $one_or_two_digits,
362 field => 'day',
363 },
364 g => {
365 regex => $one_or_two_digits,
366 field => 'iso_week_year_100',
367 },
368 G => {
369 regex => qr/$digit{4}/,
370 field => 'iso_week_year',
371 },
372 H => {
373 regex => $one_or_two_digits,
374 field => 'hour',
375 },
376 I => {
377 regex => $one_or_two_digits,
378 field => 'hour_12',
379 },
380 j => {
381 regex => qr/$digit{1,3}/,
382 field => 'day_of_year',
383 },
384 m => {
385 regex => $one_or_two_digits,
386 field => 'month',
387 },
388 M => {
389 regex => $one_or_two_digits,
390 field => 'minute',
391 },
392 n => {
393 regex => qr/\s+/,
394 },
395 O => {
396 regex => qr{[a-zA-Z_]+(?:/[a-zA-Z_]+(?:/[a-zA-Z_]+)?)?},
397 field => 'time_zone_name',
398 },
399 s => {
400 regex => qr/$digit+/,
401 field => 'epoch',
402 },
403 S => {
404 regex => $one_or_two_digits,
405 field => 'second',
406 },
407 U => {
408 regex => $one_or_two_digits,
409 field => 'week_sun_0',
410 },
411 u => {
412 regex => $one_or_two_digits,
413 field => 'day_of_week',
414 },
415 w => {
416 regex => $one_or_two_digits,
417 field => 'day_of_week_sun_0',
418 },
419 W => {
420 regex => $one_or_two_digits,
421 field => 'week_mon_1',
422 },
423 y => {
424 regex => $one_or_two_digits,
425 field => 'year_100',
426 },
427 Y => {
428 regex => qr/$digit{4}/,
429 field => 'year',
430 },
431 z => {
432 regex => qr/(?:Z|[+-]$digit{2}(?:[:]?$digit{2})?)/,
433 field => 'time_zone_offset',
434 },
435 Z => {
436 regex => qr/[a-zA-Z]{1,6}|[\-\+]$digit{2}/,
437 field => 'time_zone_abbreviation',
438 },
439 );
440  
441 12 $universal_patterns{e} = $universal_patterns{d};
442 12 $universal_patterns{k} = $universal_patterns{H};
443 12 $universal_patterns{l} = $universal_patterns{I};
444 12 $universal_patterns{t} = $universal_patterns{n};
445  
446 12 my %universal_replacements = (
447 D => '%m/%d/%y',
448 F => '%Y-%m-%d',
449 r => '%I:%M:%S %p',
450 R => '%H:%M',
451 T => '%H:%M:%S',
452 );
453  
454 294 ▶ sub _parser_pieces {
Subroutine _parser_pieces: called CC=4 SCAR=13.9
455 294 my $self = shift;
456  
457 294 my %replacements = %universal_replacements;
458 294 $replacements{c} = $self->{locale}->glibc_datetime_format;
459 294 $replacements{x} = $self->{locale}->glibc_date_format;
460 294 $replacements{X} = $self->{locale}->glibc_time_format;
461  
462 294 my %patterns = %universal_patterns;
463 $patterns{a} = $patterns{A} = {
464 294 regex => do {
465 4116 my $days = join '|', map {quotemeta}
466 10996 ▶ sort { ( length $b <=> length $a ) or ( $a cmp $b ) }
Condition66%2/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
611448820
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table66%2/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
467 294 keys %{ $self->_locale_days };
468 294 qr/$days/i;
469 },
470 field => 'day_name',
471 };
472  
473 $patterns{b} = $patterns{B} = $patterns{h} = {
474 294 regex => do {
475 6698 my $months = join '|', map {quotemeta}
476 22540 ▶ sort { ( length $b <=> length $a ) or ( $a cmp $b ) }
Condition66%2/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
11427111130
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table66%2/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
477 294 keys %{ $self->_locale_months };
478 294 qr/$months/i;
479 },
480 field => 'month_name',
481 };
482  
483 $patterns{p} = $patterns{P} = {
484 294 regex => do {
485 my $am_pm = join '|',
486 588 map {quotemeta}
487 294 ▶ sort { ( length $b <=> length $a ) or ( $a cmp $b ) }
Condition33%1/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
02940
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table33%1/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
488 294 @{ $self->{locale}->am_pm_abbreviated };
489 294 qr/$am_pm/i;
490 },
491 field => 'am_pm',
492 };
493  
494 return (
495 294 $self->_token_re_for( keys %replacements ),
496 \%replacements,
497 $self->_token_re_for( keys %patterns ),
498 \%patterns,
499 );
500 }
501 }
502  
503 12 ▶sub _locale_days {
Subroutine _locale_days: called CC=4 SCAR=13.9
504 338 my $self = shift;
505  
506 338 ▶ return $self->{locale_days} if $self->{locale_days};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"locale_days"} 44 294 338
507  
508 294 my $wide = $self->{locale}->day_format_wide;
509 294 my $abbr = $self->{locale}->day_format_abbreviated;
510  
511 294 my %locale_days;
512 294 for my $i ( 0 .. 6 ) {
513 2058 $locale_days{ lc $wide->[$i] } = $i;
514 2058 $locale_days{ lc $abbr->[$i] } = $i;
515 }
516  
517 294 ▶ return $self->{locale_days} ||= \%locale_days;
Condition50%1/2
$self->{"locale_days"} ||= \%locale_days
l!l
0294
MC/DC0%0/1
$self->{"locale_days"} ||= \%locale_days
$self->{"locale_days"}
Truth table50%1/2
A
A= $self->{"locale_days"}
Aresult
11
00
518 }
519  
520 369 ▶sub _locale_months {
Subroutine _locale_months: called CC=4 SCAR=13.9
521 369 my $self = shift;
522  
523 369 ▶ return $self->{locale_months} if $self->{locale_months};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"locale_months"} 75 294 369
524  
525 294 my $wide = $self->{locale}->month_format_wide;
526 294 my $abbr = $self->{locale}->month_format_abbreviated;
527  
528 294 my %locale_months;
529 294 for my $i ( 0 .. 11 ) {
530 3528 $locale_months{ lc $wide->[$i] } = $i + 1;
531 3528 $locale_months{ lc $abbr->[$i] } = $i + 1;
532 }
533  
534 294 ▶ return $self->{locale_months} ||= \%locale_months;
Condition50%1/2
$self->{"locale_months"} ||= \%locale_months
l!l
0294
MC/DC0%0/1
$self->{"locale_months"} ||= \%locale_months
$self->{"locale_months"}
Truth table50%1/2
A
A= $self->{"locale_months"}
Aresult
11
00
535 }
536  
537 588 ▶sub _token_re_for {
Subroutine _token_re_for: called CC=2 SCAR=7.4
538 588 shift;
539 my $t = join '|',
540 588 ▶ sort { ( length $b <=> length $a ) or ( $a cmp $b ) } @_;
Condition33%1/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
0421300
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table33%1/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
541  
542 588 return qr/$t/;
543 }
544  
545 {
546 # These are fields we parse that cannot be passed to a DateTime
547 # constructor
548 12 my @non_dt_keys = qw(
549 am_pm
550 century
551 day_name
552 day_of_week
553 day_of_week_sun_0
554 hour_12
555 iso_week_year
556 iso_week_year_100
557 month_name
558 time_zone_abbreviation
559 time_zone_name
560 time_zone_offset
561 week_mon_1
562 week_sun_0
563 year_100
564 );
565  
566 ## no critic (Subroutines::ProhibitExcessComplexity)
567 283 ▶ sub _munge_args {
Subroutine _munge_args: called CC=32 SCAR=34.7
568 283 my $self = shift;
569 283 my $args = shift;
570  
571 283 ▶ if ( defined $args->{month_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"month_name"}) 75 208 283
572 my $num = $self->_locale_months->{ lc $args->{month_name} }
573 75 ▶ or die "We somehow parsed a month name ($args->{month_name})"
Branch: 1 branch
BranchTrueFalseTotal
my $num = $self->_locale_months->{lc $args->{"month_name"}} or die qq[We somehow parsed a month name ($args->{"month_name"})] . ' that does not correspond to any month in this locale!' 75 0 75
574 . ' that does not correspond to any month in this locale!';
575  
576 75 $args->{month} = $num;
577 }
578  
579 283 ▶ if ( defined $args->{am_pm} && defined $args->{hour_12} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'am_pm'} and defined $args->{'hour_12'}) { } 12 271 283
elsif (defined $args->{'hour_12'}) { } 3 268 271
Condition100%3/3
defined $args->{"am_pm"} and defined $args->{"hour_12"}
!ll&&!rl&&r
2452612
MC/DC100%2/2
defined $args->{"am_pm"} and defined $args->{"hour_12"}
defined $args->{"am_pm"} defined $args->{"hour_12"}
Truth table100%3/3
A and B
A= defined $args->{"am_pm"}B= defined $args->{"hour_12"}
ABresult
0X0
100
111
580 12 my ( $am, $pm ) = @{ $self->{locale}->am_pm_abbreviated };
581 12 $args->{hour} = $args->{hour_12};
582  
583 12 ▶ if ( lc $args->{am_pm} eq lc $am ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $args->{'am_pm'} eq lc $am) { } 4 8 12
584 4 ▶ $args->{hour} = 0 if $args->{hour} == 12;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"hour"} == 12 0 4 4
585 }
586 else {
587 8 ▶ $args->{hour} += 12 unless $args->{hour} == 12;
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"hour"} == 12 8 0 8
588 }
589 }
590 elsif ( defined $args->{hour_12} ) {
591 3 $self->_our_croak(
592 qq{Parsed a 12-hour based hour, "$args->{hour_12}",}
593 . ' but the pattern does not include an AM/PM specifier'
594 );
595 1 return;
596 }
597  
598 280 ▶ if ( defined $args->{year_100} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"year_100"}) 10 270 280
599 10 ▶ if ( defined $args->{century} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'century'}) { } 1 9 10
600 $args->{year}
601 1 = $args->{year_100} + ( $args->{century} * 100 );
602 }
603 else {
604 $args->{year} = $args->{year_100} + (
605 9 ▶ $args->{year_100} >= 69
Branch: 1 branch
BranchTrueFalseTotal
$args->{'year_100'} >= 69 ? : 4 5 9
606 ? 1900
607 : 2000
608 );
609 }
610 }
611  
612 280 ▶ if ( $args->{time_zone_offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"time_zone_offset"}) 14 266 280
613 14 my $offset = $args->{time_zone_offset};
614  
615 14 ▶ if ( $offset eq 'Z' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offset eq 'Z') { } 1 13 14
elsif ($offset =~ /^[+-][0-9]{2}$/) { } 2 11 13
616 1 $offset = '+0000';
617 }
618 elsif ( $offset =~ /^[+-][0-9]{2}$/ ) {
619 2 $offset .= '00';
620 }
621  
622 14 ▶ my $tz = try { DateTime::TimeZone->new( name => $offset ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
623 14 ▶ unless ($tz) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tz) 3 11 14
624 3 $self->_our_croak(
625 qq{The time zone name offset that was parsed does not appear to be valid, "$args->{time_zone_offset}"}
626 );
627 1 return;
628 }
629  
630 11 $args->{time_zone} = $tz;
631 }
632  
633 277 ▶ if ( defined $args->{time_zone_abbreviation} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'time_zone_abbreviation'}) { } 18 259 277
634 18 my $abbr = $args->{time_zone_abbreviation};
635 18 ▶ unless ( exists $self->{zone_map}{$abbr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"zone_map"}{$abbr}) 3 15 18
636 3 $self->_our_croak(
637 qq{Parsed an unrecognized time zone abbreviation, "$args->{time_zone_abbreviation}"}
638 );
639 1 return;
640 }
641 15 ▶ if ( !defined $self->{zone_map}{$abbr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"zone_map"}{$abbr}) 4 11 15
642 4 $self->_our_croak(
643 qq{The time zone abbreviation that was parsed is ambiguous, "$args->{time_zone_abbreviation}"}
644 );
645 1 return;
646 }
647 $args->{time_zone}
648 11 = DateTime::TimeZone->new( name => $self->{zone_map}{$abbr} );
649 }
650 else {
651 259 ▶ $args->{time_zone} ||= 'floating';
Condition100%2/2
$args->{"time_zone"} ||= "floating"
l!l
7252
MC/DC100%1/1
$args->{"time_zone"} ||= "floating"
$args->{"time_zone"}
Truth table100%2/2
A
A= $args->{"time_zone"}
Aresult
11
00
652 }
653  
654 270 ▶ if ( $args->{time_zone_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"time_zone_name"}) 8 262 270
655 8 my $name = $args->{time_zone_name};
656 8 my $tz;
657 8 ▶ unless ( $tz = try { DateTime::TimeZone->new( name => $name ) } )
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless ($tz = &try(sub { "DateTime::TimeZone"->new("name", $name); } )) 6 2 8
658 {
659 6 $name = lc $name;
660 6 $name =~ s{(^|[/_])(.)}{$1\U$2}g;
661 }
662 8 ▶ $tz = try { DateTime::TimeZone->new( name => $name ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
663 8 ▶ unless ($tz) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tz) 3 5 8
664 3 $self->_our_croak(
665 qq{The Olson time zone name that was parsed does not appear to be valid, "$args->{time_zone_name}"}
666 );
667 1 return;
668 }
669 5 ▶ $args->{time_zone} = $tz
Branch: 1 branch
BranchTrueFalseTotal
if $tz 5 0 5
670 if $tz;
671 }
672  
673 267 delete @{$args}{@non_dt_keys};
674 267 $args->{locale} = $self->{locale};
675  
676 267 for my $k ( grep { defined $args->{$_} }
677 qw( month day hour minute second nanosecond ) ) {
678 587 $args->{$k} =~ s/^\s+//;
679 }
680  
681 267 ▶ if ( defined $args->{nanosecond} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"nanosecond"}) 6 261 267
682  
683 # If we parsed "12345" we treat it as "123450000" but if we parsed
684 # "000123456" we treat it as 123,456 nanoseconds. This is all a bit
685 # weird and confusing but it matches how this module has always
686 # worked.
687 $args->{nanosecond} *= 10**( 9 - length $args->{nanosecond} )
688 6 ▶ if length $args->{nanosecond} != 9;
Branch: 1 branch
BranchTrueFalseTotal
if length $args->{"nanosecond"} != 9 3 3 6
689  
690 # If we parsed 000000123 we want to turn this into a number.
691 6 $args->{nanosecond} += 0;
692 }
693  
694 267 for my $k (qw( year month day )) {
695 801 ▶ $args->{$k} = 1 unless defined $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$k} 179 622 801
696 }
697  
698 267 ▶ if ( defined $args->{epoch} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'epoch'}) { } 28 239 267
elsif ($args->{'day_of_year'}) { } 4 235 239
699  
700 # We don't want to pass a non-integer epoch value since that gets
701 # truncated as of DateTime 1.22. Instead, we'll set the nanosecond
702 # to parsed value after constructing the object. This is a hack,
703 # but it's the best I can come up with.
704 28 my $post_construct;
705 28 ▶ if ( my $nano = $args->{nanosecond} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nano = $args->{"nanosecond"}) 2 26 28
706 2 ▶ $post_construct = sub { $_[0]->set( nanosecond => $nano ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
707 }
708  
709 28 delete @{$args}{
710 28 qw( day_of_year year month day hour minute second nanosecond )
711 };
712  
713 28 return ( 'from_epoch', $args, $post_construct );
714 }
715 elsif ( $args->{day_of_year} ) {
716 4 delete @{$args}{qw( epoch month day )};
717 4 return ( 'from_day_of_year', $args );
718 }
719  
720 235 return ( 'new', $args );
721 }
722 }
723  
724 ## no critic (Subroutines::ProhibitExcessComplexity)
725 12 ▶sub _check_dt {
Subroutine _check_dt: called CC=45 SCAR=38.1
726 258 my $self = shift;
727 258 my $dt = shift;
728 258 my $args = shift;
729  
730 my $is_am = defined $args->{am_pm}
731 258 ▶ && lc $args->{am_pm} eq lc $self->{locale}->am_pm_abbreviated->[0];
Condition100%3/3
defined $args->{'am_pm'} && lc $args->{'am_pm'} eq lc $self->{'locale'}->am_pm_abbreviated->[0]
!ll&&!rl&&r
2202117
MC/DC100%2/2
defined $args->{'am_pm'} && lc $args->{'am_pm'} eq lc $self->{'locale'}->am_pm_abbreviated->[0]
defined $args->{'am_pm'} lc $args->{'am_pm'} eq lc $self->{'locale'}->am_pm_abbreviated->[0]
Truth table100%3/3
A && B
A= defined $args->{'am_pm'}B= lc $args->{'am_pm'} eq lc $self->{'locale'}->am_pm_abbreviated->[0]
ABresult
0X0
100
111
732 258 ▶ if ( defined $args->{hour} && defined $args->{hour_12} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"hour"} and defined $args->{"hour_12"}) 4 254 258
Condition100%3/3
defined $args->{"hour"} and defined $args->{"hour_12"}
!ll&&!rl&&r
197574
MC/DC100%2/2
defined $args->{"hour"} and defined $args->{"hour_12"}
defined $args->{"hour"} defined $args->{"hour_12"}
Truth table100%3/3
A and B
A= defined $args->{"hour"}B= defined $args->{"hour_12"}
ABresult
0X0
100
111
733 4 ▶ unless ( ( $args->{hour} % 12 ) == $args->{hour_12} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"hour"} % 12 == $args->{"hour_12"}) 3 1 4
734 3 $self->_our_croak(
735 'Parsed an input with 24-hour and 12-hour time values that do not match'
736 . qq{ - "$args->{hour}" versus "$args->{hour_12}"} );
737 1 return;
738 }
739 }
740  
741 255 ▶ if ( defined $args->{hour} && defined $args->{am_pm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"hour"} and defined $args->{"am_pm"}) 27 228 255
Condition100%3/3
defined $args->{"hour"} and defined $args->{"am_pm"}
!ll&&!rl&&r
1973127
MC/DC100%2/2
defined $args->{"hour"} and defined $args->{"am_pm"}
defined $args->{"hour"} defined $args->{"am_pm"}
Truth table100%3/3
A and B
A= defined $args->{"hour"}B= defined $args->{"am_pm"}
ABresult
0X0
100
111
742 27 ▶ if ( ( $is_am && $args->{hour} >= 12 )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_am and $args->{"hour"} >= 12 or not $is_am and $args->{"hour"} < 12) 6 21 27
Condition100%9/9
$is_am and $args->{"hour"} >= 12 or not $is_am and $args->{"hour"} < 12
l!l&&r!l&&!r
3321
$is_am and $args->{"hour"} >= 12
!ll&&!rl&&r
14103
not $is_am and $args->{"hour"} < 12
!ll&&!rl&&r
10113
MC/DC75%3/4
$is_am and $args->{"hour"} >= 12 or not $is_am and $args->{"hour"} < 12
$is_am $args->{"hour"} >= 12 not $is_am $args->{"hour"} < 12
Truth table57%4/7
A and B or not(C) and D
A= $is_amB= $args->{"hour"} >= 12C= not $is_amD= $args->{"hour"} < 12
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
743 || ( !$is_am && $args->{hour} < 12 ) ) {
744 6 $self->_our_croak(
745 'Parsed an input with 24-hour and AM/PM values that do not match'
746 . qq{ - "$args->{hour}" versus "$args->{am_pm}"} );
747 2 return;
748 }
749 }
750  
751 249 ▶ if ( defined $args->{year} && defined $args->{century} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"year"} and defined $args->{"century"}) 4 245 249
Condition100%3/3
defined $args->{"year"} and defined $args->{"century"}
!ll&&!rl&&r
551904
MC/DC100%2/2
defined $args->{"year"} and defined $args->{"century"}
defined $args->{"year"} defined $args->{"century"}
Truth table100%3/3
A and B
A= defined $args->{"year"}B= defined $args->{"century"}
ABresult
0X0
100
111
752 4 ▶ unless ( int( $args->{year} / 100 ) == $args->{century} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (int $args->{"year"} / 100 == $args->{"century"}) 3 1 4
753 3 $self->_our_croak(
754 'Parsed an input with year and century values that do not match'
755 . qq{ - "$args->{year}" versus "$args->{century}"} );
756 1 return;
757 }
758 }
759  
760 246 ▶ if ( defined $args->{year} && defined $args->{year_100} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"year"} and defined $args->{"year_100"}) 4 242 246
Condition100%3/3
defined $args->{"year"} and defined $args->{"year_100"}
!ll&&!rl&&r
551874
MC/DC100%2/2
defined $args->{"year"} and defined $args->{"year_100"}
defined $args->{"year"} defined $args->{"year_100"}
Truth table100%3/3
A and B
A= defined $args->{"year"}B= defined $args->{"year_100"}
ABresult
0X0
100
111
761 4 ▶ unless ( ( $args->{year} % 100 ) == $args->{year_100} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"year"} % 100 == $args->{"year_100"}) 3 1 4
762 3 $self->_our_croak(
763 'Parsed an input with year and year-within-century values that do not match'
764 . qq{ - "$args->{year}" versus "$args->{year_100}"} );
765 1 return;
766 }
767 }
768  
769 243 ▶ if ( defined $args->{time_zone_abbreviation}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"time_zone_abbreviation"} and defined $args->{"time_zone_offset"}) 4 239 243
Condition100%3/3
defined $args->{"time_zone_abbreviation"} and defined $args->{"time_zone_offset"}
!ll&&!rl&&r
23274
MC/DC100%2/2
defined $args->{"time_zone_abbreviation"} and defined $args->{"time_zone_offset"}
defined $args->{"time_zone_abbreviation"} defined $args->{"time_zone_offset"}
Truth table100%3/3
A and B
A= defined $args->{"time_zone_abbreviation"}B= defined $args->{"time_zone_offset"}
ABresult
0X0
100
111
770 && defined $args->{time_zone_offset} ) {
771 4 ▶ unless ( $self->{zone_map}{ $args->{time_zone_abbreviation} }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"zone_map"}{$args->{"time_zone_abbreviation"}} and $self->{"zone_map"}{$args->{"time_zone_abbreviation"}} eq $args->{"time_zone_offset"}) 3 1 4
Condition66%2/3
$self->{"zone_map"}{$args->{"time_zone_abbreviation"}} and $self->{"zone_map"}{$args->{"time_zone_abbreviation"}} eq $args->{"time_zone_offset"}
!ll&&!rl&&r
031
MC/DC50%1/2
$self->{"zone_map"}{$args->{"time_zone_abbreviation"}} and $self->{"zone_map"}{$args->{"time_zone_abbreviation"}} eq $args->{"time_zone_offset"}
$self->{"zone_map"}{$args->{"time_zone_abbreviation"}} $self->{"zone_map"}{$args->{"time_zone_abbreviation"}} eq $args->{"time_zone_offset"}
Truth table66%2/3
A and B
A= $self->{"zone_map"}{$args->{"time_zone_abbreviation"}}B= $self->{"zone_map"}{$args->{"time_zone_abbreviation"}} eq $args->{"time_zone_offset"}
ABresult
0X0
100
111
772 && $self->{zone_map}{ $args->{time_zone_abbreviation} } eq
773 $args->{time_zone_offset} ) {
774  
775 3 $self->_our_croak(
776 'Parsed an input with time zone abbreviation and time zone offset values that do not match'
777 . qq{ - "$args->{time_zone_abbreviation}" versus "$args->{time_zone_offset}"}
778 );
779 1 return;
780 }
781 }
782  
783 240 ▶ if ( defined $args->{epoch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"epoch"}) 28 212 240
784 28 for my $key (
785 qw( year month day minute hour second hour_12 day_of_year )) {
786 140 ▶ if ( defined $args->{$key} && $dt->$key != $args->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$key} and $dt->$key != $args->{$key}) 24 116 140
Condition100%3/3
defined $args->{$key} and $dt->$key != $args->{$key}
!ll&&!rl&&r
108824
MC/DC100%2/2
defined $args->{$key} and $dt->$key != $args->{$key}
defined $args->{$key} $dt->$key != $args->{$key}
Truth table100%3/3
A and B
A= defined $args->{$key}B= $dt->$key != $args->{$key}
ABresult
0X0
100
111
787 24 ▶ my $print_key
Branch: 2 branches
BranchTrueFalseTotal
$key eq 'hour_12' ? : 3 21 24
$key eq 'day_of_year' ? : 3 18 21
788 = $key eq 'hour_12' ? 'hour (1-12)'
789 : $key eq 'day_of_year' ? 'day of year'
790 : $key;
791 24 $self->_our_croak(
792 "Parsed an input with epoch and $print_key values that do not match"
793 . qq{ - "$args->{epoch}" versus "$args->{$key}"} );
794 8 return;
795 }
796 }
797 }
798  
799 216 ▶ if ( defined $args->{month} && defined $args->{day_of_year} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"month"} and defined $args->{"day_of_year"}) 4 212 216
Condition100%3/3
defined $args->{"month"} and defined $args->{"day_of_year"}
!ll&&!rl&&r
501624
MC/DC100%2/2
defined $args->{"month"} and defined $args->{"day_of_year"}
defined $args->{"month"} defined $args->{"day_of_year"}
Truth table100%3/3
A and B
A= defined $args->{"month"}B= defined $args->{"day_of_year"}
ABresult
0X0
100
111
800 4 ▶ unless ( $dt->month == $args->{month} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt->month == $args->{"month"}) 3 1 4
801 3 $self->_our_croak(
802 'Parsed an input with month and day of year values that do not match'
803 . qq{ - "$args->{month}" versus "$args->{day_of_year}"} );
804 1 return;
805 }
806 }
807  
808 213 ▶ if ( defined $args->{day_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"day_name"}) 44 169 213
809 44 my $dow = $self->_locale_days->{ lc $args->{day_name} };
810 44 ▶ defined $dow
Branch: 1 branch
BranchTrueFalseTotal
defined $dow or die qq[We somehow parsed a day name ($args->{"day_name"})] . ' that does not correspond to any day in this locale!' 44 0 44
811 or die "We somehow parsed a day name ($args->{day_name})"
812 . ' that does not correspond to any day in this locale!';
813  
814 44 ▶ unless ( $dt->day_of_week_0 == $dow ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt->day_of_week_0 == $dow) 3 41 44
815 3 $self->_our_croak(
816 'Parsed an input where the day name does not match the date'
817 . qq{ - "$args->{day_name}" versus "}
818 . $dt->ymd
819 . q{"} );
820 1 return;
821 }
822 }
823  
824 210 ▶ if ( defined $args->{day_of_week} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"day_of_week"}) 4 206 210
825 4 ▶ unless ( $dt->day_of_week == $args->{day_of_week} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt->day_of_week == $args->{"day_of_week"}) 3 1 4
826 3 $self->_our_croak(
827 'Parsed an input where the day of week does not match the date'
828 . qq{ - "$args->{day_of_week}" versus "}
829 . $dt->ymd
830 . q{"} );
831 1 return;
832 }
833 }
834  
835 207 ▶ if ( defined $args->{day_of_week_sun_0} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"day_of_week_sun_0"}) 4 203 207
836 4 ▶ unless ( ( $dt->day_of_week % 7 ) == $args->{day_of_week_sun_0} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt->day_of_week % 7 == $args->{"day_of_week_sun_0"}) 3 1 4
837 3 $self->_our_croak(
838 'Parsed an input where the day of week (Sunday as 0) does not match the date'
839 . qq{ - "$args->{day_of_week_sun_0}" versus "}
840 . $dt->ymd
841 . q{"} );
842 1 return;
843 }
844 }
845  
846 204 ▶ if ( defined $args->{iso_week_year} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"iso_week_year"}) 4 200 204
847 4 ▶ unless ( $dt->week_year == $args->{iso_week_year} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt->week_year == $args->{"iso_week_year"}) 3 1 4
848 3 $self->_our_croak(
849 'Parsed an input where the ISO week year does not match the date'
850 . qq{ - "$args->{iso_week_year}" versus "}
851 . $dt->ymd
852 . q{"} );
853 1 return;
854 }
855 }
856  
857 201 ▶ if ( defined $args->{iso_week_year_100} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"iso_week_year_100"}) 4 197 201
858 4 ▶ unless ( ( 0 + substr( $dt->week_year, -2 ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (0 + substr($dt->week_year, -2) == $args->{"iso_week_year_100"}) 3 1 4
859 == $args->{iso_week_year_100} ) {
860 3 $self->_our_croak(
861 'Parsed an input where the ISO week year (without century) does not match the date'
862 . qq{ - "$args->{iso_week_year_100}" versus "}
863 . $dt->ymd
864 . q{"} );
865 1 return;
866 }
867 }
868  
869 198 ▶ if ( defined $args->{week_mon_1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"week_mon_1"}) 4 194 198
870 4 ▶ unless ( ( 0 + $dt->strftime('%W') ) == $args->{week_mon_1} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (0 + $dt->strftime("%W") == $args->{"week_mon_1"}) 3 1 4
871 3 $self->_our_croak(
872 'Parsed an input where the ISO week number (Monday starts week) does not match the date'
873 . qq{ - "$args->{week_mon_1}" versus "}
874 . $dt->ymd
875 . q{"} );
876 1 return;
877 }
878 }
879  
880 195 ▶ if ( defined $args->{week_sun_0} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"week_sun_0"}) 4 191 195
881 4 ▶ unless ( ( 0 + $dt->strftime('%U') ) == $args->{week_sun_0} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (0 + $dt->strftime("%U") == $args->{"week_sun_0"}) 3 1 4
882 3 $self->_our_croak(
883 'Parsed an input where the ISO week number (Sunday starts week) does not match the date'
884 . qq{ - "$args->{week_sun_0}" versus "}
885 . $dt->ymd
886 . q{"} );
887 1 return;
888 }
889 }
890  
891 192 return 1;
892 }
893 ## use critic
894  
895 29 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
896 29 my $self = shift;
897 29 return $self->{pattern};
898 }
899  
900 29 ▶sub locale {
Subroutine locale: called CC=2 SCAR=7.2
Pod documented
901 29 my $self = shift;
902 return $self->{locale}->can('code')
903 ? $self->{locale}->code
904 29 ▶ : $self->{locale}->id;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'locale'}->can('code') ? : 29 0 29
905 }
906  
907 0 ▶sub time_zone {
Subroutine time_zone: not called CC=1 SCAR=6.9
Pod documented
908 0 my $self = shift;
909 0 return $self->{time_zone}->name;
910 }
911  
912 0 ▶sub parse_duration {
Subroutine parse_duration: not called CC=1 SCAR=6.9
Pod undocumented
913 0 croak q{DateTime::Format::Strptime doesn't do durations.};
914 }
915  
916 {
917 12 my $validator = validation_for( params => [ { type => t('DateTime') } ] );
918  
919 30 ▶ sub format_datetime {
Subroutine format_datetime: called CC=1 SCAR=0.0
Pod documented
920 30 my $self = shift;
921 30 my ($dt) = $validator->(@_);
922  
923 29 my $pattern = $self->pattern;
924 29 $pattern =~ s/%O/$dt->time_zone->name/eg;
925 29 return $dt->clone->set_locale( $self->locale )->strftime($pattern);
926 }
927  
928 }
929  
930 12 ▶sub format_duration {
Subroutine format_duration: not called CC=1 SCAR=1.2
Pod undocumented
931 0 croak q{DateTime::Format::Strptime doesn't do durations.};
932 }
933  
934 97 ▶sub _our_croak {
Subroutine _our_croak: called CC=3 SCAR=11.0
935 97 my $self = shift;
936 97 my $error = shift;
937  
938 97 ▶ return $self->{on_error}->( $self, $error ) if ref $self->{on_error};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"on_error"} 32 65 97
939 65 ▶ croak $error if $self->{on_error} eq 'croak';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_error"} eq "croak" 33 32 65
940 32 $self->{errmsg} = $error;
941 32 return;
942 }
943  
944 32 ▶sub errmsg {
Subroutine errmsg: called CC=1 SCAR=0.0
Pod documented
945 32 $_[0]->{errmsg};
946 }
947  
948 # Exportable functions:
949  
950 1 ▶sub strftime {
Subroutine strftime: called CC=1 SCAR=0.0
Pod documented
951 1 my ( $pattern, $dt ) = @_;
952 1 return DateTime::Format::Strptime->new(
953 pattern => $pattern,
954 on_error => 'croak'
955 )->format_datetime($dt);
956 }
957  
958 1 ▶sub strptime {
Subroutine strptime: called CC=1 SCAR=0.0
Pod documented
959 1 my ( $pattern, $time_string ) = @_;
960 1 return DateTime::Format::Strptime->new(
961 pattern => $pattern,
962 on_error => 'croak'
963 )->parse_datetime($time_string);
964 }
965  
966 12 1;
967  
968 # ABSTRACT: Parse and format strp and strf time patterns
969  
970 __END__
↑ Summary Strptime/Types.pm »