blib/lib/Regexp/Common/time.pm

filter: Statementstmt 93.3% 197 / 211 Branchbran 77.2% 51 / 66 Conditioncond 51.2% 21 / 41 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 7 totaltotal 79.4% 293 / 369 CC 49 SCAR 18.3
CC
49
Cov
85%
_setup_locale
11.1
_get_abbr_weekday_pattern
8.2
ymd
7.1
CRAP 6.2
↑ Summary
1 =head1 NAME
2  
3 Regexp::Common::time - Date and time regexps.
4  
5 =cut
6  
7 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Regexp::Common::time;
11 14 $Regexp::Common::time::VERSION = '0.16';
12 14 ▶use Regexp::Common qw(pattern);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub _croak { require Carp; goto &Carp::croak}
Subroutine _croak: not called CC=1 SCAR=6.9
15  
16 14 my $can_locale;
17 my $can_posix;
18 BEGIN
19 14 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
20 eval
21 14 {
22 14 $can_posix = 0;
23 14 require POSIX;
24 14 $can_posix = 1;
25 };
26 eval
27 14 {
28 14 $can_locale = 0;
29 14 require I18N::Langinfo;
30 14 I18N::Langinfo->import(qw(langinfo));
31 14 $can_locale = 1;
32 };
33 }
34  
35 # Master list of patterns
36 14 our %master
37 = (
38 c2 => q/\d{2}/, # Century, 2 digits
39 yr2 => q/\d{2}/, # Year, 2 digits
40 yr4 => q/\d{4}/, # Year, 4 digits
41 yr24 => q/(?:\d{2}(?:\d{2})?)/, # Year, 2 or 4 digits
42 mo2 => q/(?:(?=[01])(?:0[1-9]|1[012]))/, # Month, 2 digits
43 mo12 => q/(?:0[1-9]|1[012]|(?<!\d)[1-9])/, # Month, 1 or 2 digits
44 mo_2 => q/(?:(?=[ 1])(?: [1-9]|1[012]))/, # Month, 2 places, leading space
45 dy2 => q/(?:(?=[0123])(?:0[1-9]|[12]\d|3[01]))/, # Day, 2 digits
46 dy12 => q/(?:0[1-9]|[12]\d|3[01]|(?<!\d)[1-9])/, # Day, 1 or 2 digits
47 dy_2 => q/(?:(?=[ 123])(?: [1-9]|[12]\d|3[01]))/, # Day, 2 places, leading space
48 doy3 => q/(?:(?=[0-3])(?:00[1-9]|0[1-9]\d|[12]\d\d|3(?:[0-5]\d|6[0-6])))/, # Day of year, 3 digits
49 hr2 => q/(?:(?=[012])(?:[01]\d|2[0123]))/, # Hour, 00-23, 2 digits
50 hr12 => q/(?:(?=\d)(?:[01]\d|2[0123]|(?<!\d)\d))/, # Hour, 0-23, 1 or 2 digits
51 hr_2 => q/(?:(?=[ 12])(?:[ 1]\d|2[0123]))/, # Hour, 0-23, 2 places, ld sp
52 hx2 => q/(?:(?=[01])(?:0[1-9]|1[012]))/, # Hour, 01-12, 2 digits
53 hx12 => q/(?:(?=\d)(?:0[1-9]|1[012]|(?<!\d)[1-9]))/, # Hour, 1-12, 1 or 2 digits
54 hx_2 => q/(?:(?=[ 1])(?: [1-9]|1[012]))/, # Hour, 1-12, 2 places, ld sp
55 mi2 => q/(?:[0-5]\d)/, # Minute, 2 digits
56 mi12 => q/(?:[0-5]\d|(?<!\d)\d)/, # Minute, 1 or 2 digits
57 mi_2 => q/(?:[ 1-5]\d)/, # Minute, 2 places, leading sp
58 sc2 => q/(?:(?=[0-6])(?:[0-5]\d|6[01]))/, # Second, 2 digits, 00-61
59 sc12 => q/(?:(?=[0-6])(?:[0-5]\d|6[01]|(?<!\d)\d))/, # Second, 1 or 2 digits, 0-61
60 sc_2 => q/(?:(?=[ 1-6])(?:[ 1-5]\d|6[01]))/, # Second, 2 places, 0-61, ld sp
61 wn2 => q/(?:(?=[0-5])(?:0[1-9]|[1-4]\d|5[0-3]))/, # Week number, 2 digits, 01-53
62 wnx2 => q/(?:(?=[0-5])(?:[0-4]\d|5[0-3]))/, # Week number, 2 digits, 00-53
63 wd1 => q/[0-6]/, # Weekday number, 1 digit, 0-6
64 wdx1 => q/[1-7]/, # Weekday number, 1 digit, 1-7
65 msec => q/\d{3}/, # millisecond
66 usec => q/\d{6}/, # microsecond
67 ampm => q/(?:(?=[AaPp])(?:[ap](?:m|\.m\.)?|[AP](?:M|\.M\.)?))/, # am/pm indicator
68 th => q/(?:(?=[SNRTsnrt])(?:st|ST|nd|ND|rd|RD|th|TH))/, # ordinal suffix
69 tz => q/(?:[-+](?:[01]\d|2[0-4])(?::?[0-5]\d)?|Z|GMT|UTC?|[ECMP][DS]T)/, # Time zone
70 ema => _get_abbr_month_pattern(1), # English month abbreviation
71  
72 # The following are locale-specific, and will be populated later
73 mname => q/TBD/, # Full month name
74 mabbr => q/TBD/, # Month abbreviation
75 dname => q/TBD/, # Full weekday name
76 dabbr => q/TBD/, # Weekday abbreviation
77 axpx => q/TBD/, # locale-specific AM/PM indicator
78 );
79  
80 14 my $npd = q/(?<!\d)/; # "No preceeding digit"
81 14 my $nfd = q/(?!\d)/; # "No following digit"
82 14 my $sdig = $npd . q/[1-9]/ . $nfd; # One single digit (used for months and days)
83  
84 sub _nospace
85 132 ▶{
Subroutine _nospace: called CC=1 SCAR=0.0
86 132 my $s = shift;
87 132 $s =~ s/([\x20\x09])/sprintf '\\x%02x', ord $1/eg;
88 132 return $s;
89 }
90  
91 14 my $anymon; # general-purpose month capture. Set in _setup_locale().
92  
93 14 my $d = qq/$sdig|$master{dy2}/;
94 14 my $dcap = qq/(?k:$d)/;
95  
96 # Separator pattern: allows for certain punctuation, or none, plus optional space.
97 14 my $dsep = _nospace q{[-/. ]};
98  
99 # "Middle" day. Must be surrounded by matching separators
100 14 my $dmiddle = _nospace qq{(?=(?>/$master{dy12}/|-$master{dy12}-| $master{dy12},? |\\.$master{dy12}\\.|(?!$dsep)$master{dy12}(?!$dsep)))$dsep?(?k:$master{dy12}),?$dsep?};
101 14 my $d2middle = _nospace qq{(?=(?>/$master{dy2}/|-$master{dy2}-| $master{dy2},? |\\.$master{dy2}\\.|(?!$dsep)$master{dy2}(?!$dsep)))$dsep?(?k:$master{dy2}),?$dsep?};
102  
103 # "Middle" month. Must be surrounded by matching separators
104 14 my $mFULLmiddle; # Full month pattern, in middle (ymd and dmy). Set in _setup_locale().
105 14 my $m2middle = _nospace qq{(?=(?>/$master{mo2}/|-$master{mo2}-| $master{mo2} |\\.$master{mo2}\\.|$master{mo2}(?!$dsep)))$dsep?(?k:$master{mo2})$dsep?};
106  
107 # "Middle" minute. Must be surrounded by matching separators
108 14 my $tsep = _nospace q/[:. ]/;
109 14 my $min2middle = _nospace qq{(?=(?>:$master{mi2}:|\\.$master{mi2}\\.| $master{mi2} |$master{mi2}(?!$tsep)))$tsep?(?k:$master{mi2})$tsep?};
110  
111  
112 # YMD builder
113 sub ymd
114 1017 ▶{
Subroutine ymd: called CC=7 SCAR=19.6
Pod undocumented
115 1017 my ($self, $flags_hr, $keys_ar) = @_;
116 1017 my $pattern = $keys_ar->[1];
117 1017 _setup_locale();
118  
119 # The second separator character is REQUIRED to be the same as the
120 # first for YMD patterns. Otherwise, "2005/10/21" is ambiguous:
121 # it matches "(20)(05)/(10)" and "(2005)/(10)/(21)".
122  
123 # 'ymd' is the most flexible: year: 2/4 digits; month 1/2 digits or name; day 1/2 digits.
124 1017 ▶ if ($pattern eq 'ymd')
Branch: 5 branches
BranchTrueFalseTotal
if ($pattern eq 'ymd') { } 226 791 1017
elsif ($pattern eq 'y4md') { } 214 577 791
elsif ($pattern eq 'y2md') { } 228 349 577
elsif ($pattern eq 'y4m2d2' or $pattern eq 'YMD') { } 233 116 349
elsif ($pattern eq 'y2m2d2') { } 116 0 116
125 {
126 226 return qq/(?k:$npd(?k:$master{yr24})$mFULLmiddle$dcap$nfd)/;
127 }
128 # 'y4md': 4-digit year; 1 or 2 digit month and day. Or named month.
129 elsif ($pattern eq 'y4md')
130 {
131 214 return qq/(?k:(?k:$master{yr4})$mFULLmiddle$dcap$nfd)/;
132 }
133 # 'y2md': 2-digit year; 1 or 2 digit month and day.
134 elsif ($pattern eq 'y2md')
135 {
136 228 return qq/(?k:(?k:$master{yr2})$mFULLmiddle$dcap$nfd)/;
137 }
138 ▶ elsif ($pattern eq 'y4m2d2' || $pattern eq 'YMD')
Condition66%2/3
$pattern eq "y4m2d2" or $pattern eq "YMD"
l!l&&r!l&&!r
0233116
MC/DC50%1/2
$pattern eq "y4m2d2" or $pattern eq "YMD"
$pattern eq "y4m2d2" $pattern eq "YMD"
Truth table66%2/3
A or B
A= $pattern eq "y4m2d2"B= $pattern eq "YMD"
ABresult
1X1
011
000
139 {
140 233 return qq/(?k:(?k:$master{yr4})$m2middle(?k:$master{dy2}))/;
141 }
142 elsif ($pattern eq 'y2m2d2')
143 {
144 116 return qq/(?k:(?k:$master{yr2})$m2middle(?k:$master{dy2}))/;
145 }
146  
147 # Probably the only way to get here is if I goof up and specify this subroutine
148 # for a YMD pattern that is not handled above.
149 0 die "Programming error: Unknown y-m-d pattern '$pattern'. Contact Regexp::Common::time author.";
150 }
151  
152 # MDY builder
153 sub mdy
154 1240 ▶{
Subroutine mdy: called CC=7 SCAR=19.6
Pod undocumented
155 1240 my ($self, $flags_hr, $keys_ar) = @_;
156 1240 my $pattern = $keys_ar->[1];
157 1240 _setup_locale();
158  
159 # The second separator character is REQUIRED to be the same as the
160 # first for YMD patterns, for the STRICT versions of these patterns
161 # (the ones containing "m2d2").
162  
163 # 'mdy' is the most flexible: year: 2/4 digits; month 1/2 digits or named; day 1/2 digits.
164 1240 ▶ if ($pattern eq 'mdy')
Branch: 5 branches
BranchTrueFalseTotal
if ($pattern eq 'mdy') { } 234 1006 1240
elsif ($pattern eq 'mdy4') { } 212 794 1006
elsif ($pattern eq 'mdy2') { } 208 586 794
elsif ($pattern eq 'm2d2y4' or $pattern eq 'MDY') { } 392 194 586
elsif ($pattern eq 'm2d2y2') { } 194 0 194
165 {
166 234 return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr24})$nfd)/;
167 }
168 # 'mdy4': 4-digit year; 1 or 2 digit month and day.
169 elsif ($pattern eq 'mdy4')
170 {
171 212 return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr4}))/;
172 }
173 # 'mdy2': 2-digit year; 1 or 2 digit month and day.
174 elsif ($pattern eq 'mdy2')
175 {
176 208 return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr2}))/;
177 }
178 ▶ elsif ($pattern eq 'm2d2y4' || $pattern eq 'MDY')
Condition66%2/3
$pattern eq "m2d2y4" or $pattern eq "MDY"
l!l&&r!l&&!r
0392194
MC/DC50%1/2
$pattern eq "m2d2y4" or $pattern eq "MDY"
$pattern eq "m2d2y4" $pattern eq "MDY"
Truth table66%2/3
A or B
A= $pattern eq "m2d2y4"B= $pattern eq "MDY"
ABresult
1X1
011
000
179 {
180 392 return qq/(?k:(?k:$master{mo2})$d2middle(?k:$master{yr4}))/;
181 }
182 elsif ($pattern eq 'm2d2y2')
183 {
184 194 return qq/(?k:(?k:$master{mo2})$d2middle(?k:$master{yr2}))/;
185 }
186  
187 # Probably the only way to get here is if I goof up and specify this subroutine
188 # for a YMD pattern that is not handled above.
189 0 die "Programming error: Unknown m-d-y pattern '$pattern'. Contact Regexp::Common::time author.";
190 }
191  
192 # DMY builder
193 sub dmy
194 986 ▶{
Subroutine dmy: called CC=7 SCAR=19.6
Pod undocumented
195 986 my ($self, $flags_hr, $keys_ar) = @_;
196 986 my $pattern = $keys_ar->[1];
197 986 _setup_locale();
198  
199 # The second separator character is REQUIRED to be the same as the
200 # first for YMD patterns, for the STRICT versions of these patterns
201 # (the ones containing "d2m2").
202  
203 # 'dmy' is the most flexible: year: 2/4 digits; month 1/2 digits; day 1/2 digits.
204 986 ▶ if ($pattern eq 'dmy')
Branch: 5 branches
BranchTrueFalseTotal
if ($pattern eq 'dmy') { } 218 768 986
elsif ($pattern eq 'dmy4') { } 208 560 768
elsif ($pattern eq 'dmy2') { } 208 352 560
elsif ($pattern eq 'd2m2y4' or $pattern eq 'DMY') { } 228 124 352
elsif ($pattern eq 'd2m2y2') { } 124 0 124
205 {
206 218 return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr24})$nfd)/;
207 }
208 # 'mdy4': 4-digit year; 1 or 2 digit month and day.
209 elsif ($pattern eq 'dmy4')
210 {
211 208 return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr4}))/;
212 }
213 # 'y2md': 2-digit year; 1 or 2 digit month and day.
214 elsif ($pattern eq 'dmy2')
215 {
216 208 return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr2}))/;
217 }
218 ▶ elsif ($pattern eq 'd2m2y4' || $pattern eq 'DMY')
Condition66%2/3
$pattern eq "d2m2y4" or $pattern eq "DMY"
l!l&&r!l&&!r
0228124
MC/DC50%1/2
$pattern eq "d2m2y4" or $pattern eq "DMY"
$pattern eq "d2m2y4" $pattern eq "DMY"
Truth table66%2/3
A or B
A= $pattern eq "d2m2y4"B= $pattern eq "DMY"
ABresult
1X1
011
000
219 {
220 228 return qq/(?k:(?k:$master{dy2})$m2middle(?k:$master{yr4}))/;
221 }
222 elsif ($pattern eq 'd2m2y2')
223 {
224 124 return qq/(?k:(?k:$master{dy2})$m2middle(?k:$master{yr2}))/;
225 }
226  
227 # Probably the only way to get here is if I goof up and specify this subroutine
228 # for a YMD pattern that is not handled above.
229 0 die "Programming error: Unknown d-m-y pattern '$pattern'. Contact Regexp::Common::time author.";
230 }
231  
232 # HMS builder
233 sub hms
234 85 ▶{
Subroutine hms: called CC=1 SCAR=0.0
Pod undocumented
235 85 my $hr = $npd . q/[01]\d|2[0-4]|\d/;
236 85 my $sec = q/\d\d/; # Can't limit it to 00-59! Because it's optional, and out-of-range = no match.
237  
238 # my ($self, $flags_hr, $keys_ar) = @_;
239 85 return qq/(?k:$npd(?k:$master{hr12})$tsep/ # hour
240 . qq/(?k:$master{mi2})/ # minute
241 . qq/(?:$tsep(?k:$sec))?/ # second
242 . qq/(?:\\s?(?k:$master{ampm}))?)/; # am/pm
243 }
244  
245 # Time::Format-like builder
246  
247 my %tf =
248 (
249 yyyy => $master{yr4},
250 yy => $master{yr2},
251 'm{on}' => $master{mo12},
252 'mm{on}'=> $master{mo2},
253 '?m{on}'=> $master{mo_2},
254 d => $master{dy12},
255 dd => $master{dy2},
256 '?d' => $master{dy_2},
257 h => $master{hr12},
258 hh => $master{hr2},
259 '?h' => $master{hr_2},
260 H => $master{hx12},
261 HH => $master{hx2},
262 '?H' => $master{hx_2},
263 'm{in}' => $master{mi12},
264 'mm{in}'=> $master{mi2},
265 '?m{in}'=> $master{mi_2},
266 s => $master{sc12},
267 ss => $master{sc2},
268 '?s' => $master{sc_2},
269 mmm => $master{msec},
270 uuuuuu => $master{usec},
271 am => $master{ampm},
272 AM => $master{ampm},
273 'a.m.' => $master{ampm},
274 'A.M.' => $master{ampm},
275 pm => $master{ampm},
276 PM => $master{ampm},
277 'p.m.' => $master{ampm},
278 'P.M.' => $master{ampm},
279 th => $master{th},
280 TH => $master{th},
281 tz => $master{tz},
282 14 );
283  
284 14 my %disam; # Disambiguator for 'm' format.
285 14 $disam{$_} = "{on}" foreach qw/yy d dd ?d/; # If year or day is nearby, it's 'month'
286 14 $disam{$_} = "{in}" foreach qw/h hh ?h H HH ?H s ss ?s/; # If hour or second is nearby, it's 'minute'
287 14 my $disambiguate_pat_1 = qr/
288 (?<!\\) # Must not follow a backslash
289 (?=[ydhH]) # Must start with one of these
290 ( # $1 begins
291 ( # $2 begins. Capture:
292 yy # a year
293 | [dhH] # a day or hour
294 )
295 [^?m\\]* # Followed by something that's not part of a month
296 )
297 (?![?m]?m\{[io]n\}) # make sure it's not already unambiguous
298 (?!mon) # don't confuse "mon" with "m" "on"
299 ([?m]?m) # $3 is a month code
300 /x;
301  
302 14 my $disambiguate_pat_2 = qr/
303 (?<!\\) # ignore things that begin with backslash
304 ([?m]?m) # $1 is a month code
305 ( # $2 begins.
306 [^a-zA-Z]* # any number of non-alphas
307 (?<!\\) # no backslash
308 (?=[?dsy]) # Next char must be one of these
309 ( # $3 begins. Capture:
310 \??[ds] # a day or a second
311 | yy # or a year
312 )
313 )/x;
314  
315 # The Big Date/Time Pattern
316 14 my $bigpat = qr/
317 (?<!\\) # Don't expand something preceded by backslash
318 (?=[dDy?hHsaApPMmWwutT]) # Jump to one of these characters
319 (
320 [Dd]ay|DAY # Weekday abbreviation
321 | yy(?:yy)? # Year
322 | [?m]?m\{[oi]n\} # Unambiguous month-minute codes
323 | th | TH # day suffix
324 | [?d]?d # Day
325 | [?h]?h # Hour (24)
326 | [?H]?H # Hour (12)
327 | [?s]?s # Second
328 | [apAP]\.?[mM]\.? # am and pm strings
329 | [Mm]on(?:th)?|MON(?:TH)? # Month names and abbrev
330 | [Ww]eekday|WEEKDAY # Weekday names
331 | mmm|uuuuuu # millisecond and microsecond
332 | tz # time zone
333 )/x;
334  
335 sub tf_builder
336 636 ▶{
Subroutine tf_builder: called CC=3 SCAR=11.0
Pod undocumented
337 636 my ($self, $flags_hr, $keys_ar) = @_;
338  
339 # User must specify *something* as the pattern
340 _croak q{Mandatory "-pat" flag missing in tf pattern}
341 636 ▶ if !exists $flags_hr->{-pat};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flags_hr->{"-pat"} 0 636 636
342  
343 636 my $pattern = $flags_hr->{-pat};
344  
345 # Localize
346 636 _setup_locale();
347  
348 # Copying from Time::Format...
349 # "Guess" how to interpret ambiguous 'm'
350 636 $pattern =~ s/$disambiguate_pat_1/$1$3$disam{$2}/gx;
351 636 $pattern =~ s/$disambiguate_pat_2/$1$disam{$3}$2/gx;
352  
353 # If the pattern contains any parentheses, then the caller is
354 # responsible for doing all the captures.
355 636 ▶ if ($pattern =~ /(?<!\\)(?:\\\\)*\(/) # even number of backslashes
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /(?<!\\)(?:\\\\)*\(/) { } 2 634 636
356 {
357 2 $pattern =~ s/$bigpat/$tf{$1}/gx;
358 }
359 else # we'll handle the capturing
360 {
361 634 $pattern =~ s/$bigpat/(?k:$tf{$1})/gx;
362 634 $pattern = "(?k:$pattern)";
363 }
364  
365 636 return $pattern;
366 }
367  
368 # strftime builder
369 my %strftime =
370 (
371 C => $master{c2}, # two-digit century
372 D =>"$master{mo2}/$master{dy2}/$master{yr2}",
373 d => $master{dy2}, # two-digit day
374 e => $master{dy_2}, # 1 or 2-digit day, leading space
375 H => $master{hr2}, # hour, 00-23
376 I => $master{hx2}, # hour, 01-12
377 j => $master{doy3}, # day-of-year, 001-366
378 m => $master{mo2}, # month, 01-12
379 M => $master{mi2}, # minute, 00-59
380 n => "\n",
381 R =>"$master{hr2}:$master{mi2}",
382 S => $master{sc2}, # Second, 00-61
383 T =>"$master{hr2}:$master{mi2}:$master{sc2}",
384 t => "\t",
385 u => $master{wdx1}, # Weekday number, 1-7
386 U => $master{wnx2}, # Week number, 00-53
387 V => $master{wn2}, # Week number, 01-53
388 w => $master{wd1}, # Weekday number, 0-6
389 W => $master{wnx2}, # Week number, 00-53
390 y => $master{yr2}, # two-digit year
391 Y => $master{yr4}, # four-digit year
392 Z => $master{tz}, # time zone
393 '%' => '%',
394  
395 # additional useful patterns not specified by strftime
396 _d => $master{dy12}, # 1- or 2-digit day number
397 _H => $master{hr12}, # 1- or 2-digit 24-hour hour
398 _I => $master{hx12}, # 1- or 2-digit 12-hour hour
399 _m => $master{mo12}, # 1- or 2-digit month number
400 _M => $master{mi12}, # 1- or 2-digit minute
401 14 );
402  
403 sub strftime_builder
404 689 ▶{
Subroutine strftime_builder: called CC=4 SCAR=13.9
Pod undocumented
405 689 my ($self, $flags_hr, $keys_ar) = @_;
406  
407 # User must specify *something* as the pattern
408 _croak q{Mandatory "-pat" flag missing in strftime pattern}
409 689 ▶ if !exists $flags_hr->{-pat};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flags_hr->{"-pat"} 0 689 689
410  
411 689 my $pattern = $flags_hr->{-pat};
412  
413 # Localize
414 689 _setup_locale();
415  
416 # If the pattern contains any parentheses, then the caller is
417 # responsible for doing all the captures.
418 689 ▶ if ($pattern =~ /(?<!\\)(?:\\\\)*\(/) # even number of backslashes
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /(?<!\\)(?:\\\\)*\(/) { } 3 686 689
419 {
420 3 $pattern =~ s/(?<!\\) %(_?.) /$strftime{$1}/gx;
421 }
422 else # we'll handle the capturing
423 {
424 # If the pattern consists of a single pattern, then
425 # the enclosing (?k:) is redundant and annoying.
426 686 my $solo = $pattern =~ /\A # Start of user's pattern
427 (?:
428 \\b # a word break
429 |
430 \\A # Start of string
431 |
432 \^ # Start of string
433 |
434 \(\?[^\)]*\) # Some other zero-width assertion
435 )* # (any number of such assertions)
436 %_?. # The meat of the user's actual pattern
437 (?:
438 \\b # word break
439 |
440 \\z # REAL end of string
441 |
442 \\Z # end of string
443 |
444 \$ # end of line or string
445 |
446 \(\?[^\)]*\) # some other assertion
447 )*
448 \z # Actual end of user's pattern
449 /x;
450 686 $pattern =~ s/(?<!\\) %(_?.) /(?k:$strftime{$1})/gx;
451 686 ▶ $pattern = "(?k:$pattern)" unless $solo;
Branch: 1 branch
BranchTrueFalseTotal
unless $solo 10 676 686
452 }
453  
454 689 return $pattern;
455 }
456  
457 sub american
458 92 ▶{
Subroutine american: called CC=1 SCAR=0.0
Pod undocumented
459 92 my ($self, $flags_hr, $keys_ar) = @_;
460 92 _setup_locale();
461  
462 92 return join '',
463 qq/(?k:\\b/, # must start on word boundary
464 qq/(?k:$master{mname}|$master{mabbr})/, # Month name or abbr
465 qq/ {1,2}/, # one or two spaces
466 qq/(?k:$master{dy12})/, # one- or two-digit day
467 qq/(?:,| |, )/, # Comma or space or both
468 qq/(?k:'$master{yr2}|$master{yr24})/, # Year: 'yy or yyyy or yy
469 qq/$nfd)/; # No following digits
470 }
471  
472  
473  
474 # Localization.
475 # Bug: This is bulky and inefficient, and sets up many patterns that may never be used.
476 # On the other hand, it's generally only ever called once.
477 14 my $latest_setup_locale;
478 sub _setup_locale
479 4660 ▶{
Subroutine _setup_locale: called CC=10 SCAR=24.1
480 # Do nothing if locale has not changed since we set it up
481 4660 my $current_locale;
482 4660 ▶ $current_locale = $can_posix? POSIX::setlocale(POSIX::LC_TIME()) : q{};
Branch: 1 branch
BranchTrueFalseTotal
$can_posix ? : 4660 0 4660
483 4660 ▶ $current_locale = q{} if !defined $current_locale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_locale 0 4660 4660
484  
485 # No changes needed
486 4660 ▶ return if defined $latest_setup_locale
Branch: 1 branch
BranchTrueFalseTotal
if defined $latest_setup_locale and $latest_setup_locale eq $current_locale 4652 8 4660
Condition66%2/3
defined $latest_setup_locale and $latest_setup_locale eq $current_locale
!ll&&!rl&&r
804652
MC/DC50%1/2
defined $latest_setup_locale and $latest_setup_locale eq $current_locale
defined $latest_setup_locale $latest_setup_locale eq $current_locale
Truth table66%2/3
A and B
A= defined $latest_setup_localeB= $latest_setup_locale eq $current_locale
ABresult
0X0
100
111
487 && $latest_setup_locale eq $current_locale;
488  
489 8 $latest_setup_locale = $current_locale;
490  
491 8 my $dt_fmt; # locale-specific date/time format
492 my $d_fmt; # locale-specific date format
493 8 my $t_fmt; # locale-specific time format
494 8 my $t_ap_fmt; # locale-specific time with am/pm format
495 8 my $am_str; # locale-specific ante-meridian string
496 8 my $pm_str; # locale-specific post-meridian string
497  
498 8 ▶ if ($can_locale)
Branch: 1 branch
BranchTrueFalseTotal
if ($can_locale) 8 0 8
499 {
500 eval
501 8 {
502 8 ($dt_fmt, $d_fmt, $t_fmt, $t_ap_fmt) = map langinfo($_),
503 (
504 I18N::Langinfo::D_T_FMT(),
505 I18N::Langinfo::D_FMT(),
506 I18N::Langinfo::T_FMT(),
507 I18N::Langinfo::T_FMT_AMPM(),
508 );
509 8 ($am_str, $pm_str) = map langinfo($_),
510 (
511 I18N::Langinfo::AM_STR(),
512 I18N::Langinfo::PM_STR(),
513 );
514  
515 };
516 }
517 8 ▶ if (!$can_locale || $@) # Internationalization didn't work for some reason
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_locale or $@) 0 8 8
Condition33%1/3
not $can_locale or $@
l!l&&r!l&&!r
008
MC/DC0%0/2
not $can_locale or $@
not $can_locale $@
Truth table33%1/3
not(A) or B
A= not $can_localeB= $@
ABresult
1X1
011
000
518 {
519 0 $dt_fmt = q{%a %b %e %H:%M:%S %Y};
520 0 $d_fmt = q{%m/%d/%y};
521 0 $t_fmt = q{%H:%M:%S};
522 0 $t_ap_fmt = q{%I:%M:%S %p};
523 0 $am_str = q{AM};
524 0 $pm_str = q{PM};
525 }
526  
527 # Update master patterns
528 8 $master{dname} = _get_full_weekday_pattern();
529 8 $master{dabbr} = _get_abbr_weekday_pattern();
530 8 $master{mname} = _get_full_month_pattern();
531 8 $master{mabbr} = _get_abbr_month_pattern();
532 8 $master{axpx} = qq/(?:\Q$am_str\E|\Q$pm_str\E)/;
533  
534 # Pattern variables for dmy-mdy-ymd patterns
535 8 $anymon = _nospace qq/(?>(?i)$master{mo2}|$sdig|$master{mname}|$master{mabbr})/;
536 8 $mFULLmiddle = _nospace qq{(?=(?>/$anymon/|-$anymon-| $anymon |\\.$anymon\\.|(?!$dsep)$anymon(?!$dsep)))$dsep?(?k:$anymon)$dsep?};
537  
538 # Pattern variables for Time::Format
539 8 $tf{Weekday} = $tf{WEEKDAY} = $tf{weekday} = $master{dname};
540 8 $tf{Day} = $tf{DAY} = $tf{day} = $master{dabbr};
541 8 $tf{Month} = $tf{MONTH} = $tf{month} = $master{mname};
542 8 $tf{Mon} = $tf{MON} = $tf{mon} = $master{mabbr};
543  
544 # Pattern variables for strftime
545 8 $strftime{A} = $master{dname};
546 8 $strftime{a} = $master{dabbr};
547 8 $strftime{B} = $master{mname};
548 8 $strftime{b} = $master{mabbr};
549 8 $strftime{h} = $strftime{b}; # defined synonym
550 $strftime{r} ="$master{hx2}:$master{mi2}:$master{sc2} (?:$am_str|$pm_str)",
551  
552 # Set up locale-dependent strftime patterns
553 8 $strftime{p} = $master{axpx};
554 8 foreach ($dt_fmt, $d_fmt, $t_fmt, $t_ap_fmt)
555 {
556 # the "|| q{}" below is to avoid "uninitialized" warnings.
557 32 ▶ s/%(.)/$strftime{$1} || q{}/eg;
Condition50%1/2
$strftime{$1} || ''
l!l
1360
MC/DC0%0/1
$strftime{$1} || ''
$strftime{$1}
Truth table50%1/2
A
A= $strftime{$1}
Aresult
11
00
558 }
559 8 $strftime{c} = _nospace $dt_fmt;
560 8 $strftime{r} = _nospace $t_ap_fmt;
561 8 $strftime{x} = _nospace $d_fmt;
562 8 $strftime{X} = _nospace $t_fmt;
563 }
564  
565 sub _first_chars
566 46 ▶{
Subroutine _first_chars: called CC=1 SCAR=0.0
567 46 my %uniq = map {substr ($_,0,1) => 1} @_;
568 46 return join q{}, map quotemeta, keys %uniq;
569 }
570  
571 sub _get_full_month_pattern
572 8 ▶{
Subroutine _get_full_month_pattern: called CC=4 SCAR=15.0
573 8 my @Mon_Name;
574 8 ▶ if ($can_locale)
Branch: 1 branch
BranchTrueFalseTotal
if ($can_locale) 8 0 8
575 {
576 eval
577 8 {
578 8 @Mon_Name = map langinfo($_),
579 (
580 I18N::Langinfo::MON_1(),
581 I18N::Langinfo::MON_2(),
582 I18N::Langinfo::MON_3(),
583 I18N::Langinfo::MON_4(),
584 I18N::Langinfo::MON_5(),
585 I18N::Langinfo::MON_6(),
586 I18N::Langinfo::MON_7(),
587 I18N::Langinfo::MON_8(),
588 I18N::Langinfo::MON_9(),
589 I18N::Langinfo::MON_10(),
590 I18N::Langinfo::MON_11(),
591 I18N::Langinfo::MON_12(),
592 );
593 };
594 }
595 8 ▶ if (!$can_locale || $@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_locale or $@) 0 8 8
Condition33%1/3
not $can_locale or $@
l!l&&r!l&&!r
008
MC/DC0%0/2
not $can_locale or $@
not $can_locale $@
Truth table33%1/3
not(A) or B
A= not $can_localeB= $@
ABresult
1X1
011
000
596 {
597 0 @Mon_Name = qw(January February March April May June July August September October November December);
598 }
599  
600 8 my $prematch = _first_chars(@Mon_Name);
601 8 my $alternat = join '|', map quotemeta, @Mon_Name;
602 8 return qq/(?=[$prematch])(?>$alternat)/;
603 }
604  
605 sub _get_abbr_month_pattern
606 22 ▶{
Subroutine _get_abbr_month_pattern: called CC=6 SCAR=18.0
607 22 my $english_only = shift;
608 22 my @Mon_Abbr;
609 22 ▶ if (!$english_only && $can_locale)
Branch: 1 branch
BranchTrueFalseTotal
if (not $english_only and $can_locale) 8 14 22
Condition66%2/3
not $english_only and $can_locale
!ll&&!rl&&r
1408
MC/DC50%1/2
not $english_only and $can_locale
not $english_only $can_locale
Truth table66%2/3
not(A) and B
A= not $english_onlyB= $can_locale
ABresult
0X0
100
111
610 {
611 eval
612 8 {
613 8 @Mon_Abbr = map langinfo($_),
614 (
615 I18N::Langinfo::ABMON_1(),
616 I18N::Langinfo::ABMON_2(),
617 I18N::Langinfo::ABMON_3(),
618 I18N::Langinfo::ABMON_4(),
619 I18N::Langinfo::ABMON_5(),
620 I18N::Langinfo::ABMON_6(),
621 I18N::Langinfo::ABMON_7(),
622 I18N::Langinfo::ABMON_8(),
623 I18N::Langinfo::ABMON_9(),
624 I18N::Langinfo::ABMON_10(),
625 I18N::Langinfo::ABMON_11(),
626 I18N::Langinfo::ABMON_12(),
627 );
628 };
629 }
630 22 ▶ if ($english_only || !$can_locale || $@)
Branch: 1 branch
BranchTrueFalseTotal
if ($english_only or not $can_locale or $@) 14 8 22
Condition66%4/6
$english_only or not $can_locale or $@
l!l&&r!l&&!r
1408
$english_only or not $can_locale
l!l&&r!l&&!r
1408
MC/DC33%1/3
$english_only or not $can_locale or $@
$english_only not $can_locale $@
Truth table50%2/4
A or not(B) or C
A= $english_onlyB= not $can_localeC= $@
ABCresult
1XX1
01X1
0011
0000
631 {
632 14 @Mon_Abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
633 }
634  
635 22 my $prematch = _first_chars(@Mon_Abbr);
636 22 my $alternat = join '|', map quotemeta, @Mon_Abbr;
637 22 return qq/(?=[$prematch])(?>$alternat)/;
638 }
639  
640 sub _get_full_weekday_pattern
641 8 ▶{
Subroutine _get_full_weekday_pattern: called CC=4 SCAR=15.0
642 8 my @Day_Name;
643 8 ▶ if ($can_locale)
Branch: 1 branch
BranchTrueFalseTotal
if ($can_locale) 8 0 8
644 {
645 eval
646 8 {
647 8 @Day_Name = map langinfo($_),
648 (
649 I18N::Langinfo::DAY_1(),
650 I18N::Langinfo::DAY_2(),
651 I18N::Langinfo::DAY_3(),
652 I18N::Langinfo::DAY_4(),
653 I18N::Langinfo::DAY_5(),
654 I18N::Langinfo::DAY_6(),
655 I18N::Langinfo::DAY_7(),
656 );
657 };
658 }
659 8 ▶ if (!$can_locale || $@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_locale or $@) 0 8 8
Condition33%1/3
not $can_locale or $@
l!l&&r!l&&!r
008
MC/DC0%0/2
not $can_locale or $@
not $can_locale $@
Truth table33%1/3
not(A) or B
A= not $can_localeB= $@
ABresult
1X1
011
000
660 {
661 0 @Day_Name = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
662 }
663  
664 8 my $prematch = _first_chars(@Day_Name);
665 8 my $alternat = join '|', map quotemeta, @Day_Name;
666 8 return qq/(?=[$prematch])(?>$alternat)/;
667 }
668  
669 sub _get_abbr_weekday_pattern
670 8 ▶{
Subroutine _get_abbr_weekday_pattern: called CC=6 SCAR=21.0
671 8 my $english_only = shift;
672 8 my @Day_Abbr;
673 8 ▶ if (!$english_only && $can_locale)
Branch: 1 branch
BranchTrueFalseTotal
if (not $english_only and $can_locale) 8 0 8
Condition33%1/3
not $english_only and $can_locale
!ll&&!rl&&r
008
MC/DC0%0/2
not $english_only and $can_locale
not $english_only $can_locale
Truth table33%1/3
not(A) and B
A= not $english_onlyB= $can_locale
ABresult
0X0
100
111
674 {
675 eval
676 8 {
677 8 @Day_Abbr = map langinfo($_),
678 (
679 I18N::Langinfo::ABDAY_1(),
680 I18N::Langinfo::ABDAY_2(),
681 I18N::Langinfo::ABDAY_3(),
682 I18N::Langinfo::ABDAY_4(),
683 I18N::Langinfo::ABDAY_5(),
684 I18N::Langinfo::ABDAY_6(),
685 I18N::Langinfo::ABDAY_7(),
686 );
687 };
688 }
689 8 ▶ if ($english_only || !$can_locale || $@)
Branch: 1 branch
BranchTrueFalseTotal
if ($english_only or not $can_locale or $@) 0 8 8
Condition33%2/6
$english_only or not $can_locale or $@
l!l&&r!l&&!r
008
$english_only or not $can_locale
l!l&&r!l&&!r
008
MC/DC0%0/3
$english_only or not $can_locale or $@
$english_only not $can_locale $@
Truth table25%1/4
A or not(B) or C
A= $english_onlyB= not $can_localeC= $@
ABCresult
1XX1
01X1
0011
0000
690 {
691 0 @Day_Abbr = qw(Sun Mon Tue Wed Thu Fri Sat);
692 }
693  
694 8 my $prematch = _first_chars(@Day_Abbr);
695 8 my $alternat = join '|', map quotemeta, @Day_Abbr;
696 8 return qq/(?=[$prematch])(?>$alternat)/;
697 }
698  
699 # Set up all the patterns
700  
701 14 for (qw(ymd y4md y2md y4m2d2 y2m2d2 YMD))
702 {
703 84 pattern name => ['time', $_],
704 create => \&ymd,
705 }
706  
707 14 for (qw(mdy mdy4 mdy2 m2d2y4 m2d2y2 MDY))
708 {
709 84 pattern name => ['time', $_],
710 create => \&mdy,
711 }
712  
713 14 for (qw(dmy dmy4 dmy2 d2m2y4 d2m2y2 DMY))
714 {
715 84 pattern name => ['time', $_],
716 create => \&dmy,
717 }
718  
719 14 for (qw(hms))
720 {
721 14 pattern name => ['time', $_],
722 create => \&hms,
723 }
724  
725 14 for (qw(strftime))
726 {
727 14 pattern name => ['time', $_],
728 create => \&strftime_builder,
729 }
730  
731 14 for (qw(tf))
732 {
733 14 pattern name => ['time', $_],
734 create => \&tf_builder,
735 }
736 14 for (qw(american))
737 {
738 14 pattern name => ['time', $_],
739 create => \&american,
740 }
741  
742  
743 14 my $dt_sep = q/(?:(?<=\\d)[T_ ](?=\\d))?/;
744 14 pattern name => ['time', 'iso'],
745 create => join '',
746 qq/(?k:/,
747 qq/(?=\\d)/, # Expect a digit
748 qq/(?:/, # Begin optional date portion
749 qq/(?k:$master{yr4})/, qq/(?:-)(?k:$master{mo2})(?:-)/, qq/(?k:$master{dy2})/,
750 qq/)?/, # End optional date portion
751 $dt_sep,
752 qq/(?:/, # Begin optional time portion
753 qq/(?k:$master{hr2})/, qq/(?::)(?k:$master{mi2})(?::)/, qq/(?k:$master{sc2}(?:\.\\d+)?)/,
754 qq/)?/, # End optional time portion
755 qq/(?k:$master{tz})?/, # Optional time zone portion
756 qq/)/;
757  
758 14 pattern name => ['time', 'mail'],
759 create => join '',
760 qq/(?k:$npd/, # No preceeding digit
761 qq/(?=\\d)/, # Expect a digit
762 qq/(?k:$master{dy12})\\s*/, # Day
763 qq/(?k:$master{ema})\\s*/, # Month (english name abbreviation)
764 qq/(?k:$master{yr24})\\s+/, # Year
765 qq/(?k:$master{hr2}):/, # Hour
766 qq/(?k:$master{mi2}):/, # Minute
767 qq/(?k:$master{sc2})\\s*/, # Second
768 qq/(?k:$master{tz})/, # Time zone
769 qq/$nfd)/; # No trailing digit
770  
771 14 pattern name => ['time', 'MAIL'],
772 create => join '',
773 qq/(?k:$npd/, # No preceeding digit
774 qq/(?=\\d)/, # Expect a digit
775 qq/(?k:$master{dy12})\\s*/, # Day
776 qq/(?k:$master{ema})\\s*/, # Month (english name abbreviation)
777 qq/(?k:$master{yr4})\\s+/, # Year
778 qq/(?k:$master{hr2}):/, # Hour
779 qq/(?k:$master{mi2}):/, # Minute
780 qq/(?k:$master{sc2})\\s*/, # Second
781 qq/(?k:[-+]\\d{4})/, # Time zone
782 qq/$nfd)/; # No trailing digit
783  
784  
785 14 1;
786 __END__
↑ Summary