blib/lib/Data/FormValidator/Constraints/Dates.pm

filter: Statementstmt 100% 54 / 54 Branchbran 82.1% 23 / 28 Conditioncond 50.0% 7 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 81.7% 94 / 115 CC 20 SCAR 19.6
CC
20
Cov
88%
_parse_date_format
11.2
match_date_and_time
7.1
_prepare_date_format
5.0
CRAP 7.1
« blib/lib/Data/FormValidator/ConstraintsFactory.pm ↑ Summary blib/lib/Data/FormValidator/Filters.pm »
1 package Data::FormValidator::Constraints::Dates;
2 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
7  
8 our @EXPORT_OK = (
9 'date_and_time',
10 2 @{ $EXPORT_TAGS{'all'} }
11 );
12  
13 2 our @EXPORT = qw(
14 match_date_and_time
15 );
16  
17 2 our $VERSION = 4.88;
18  
19 5 ▶sub date_and_time {
Subroutine date_and_time: called CC=1 SCAR=0.0
Pod documented
20 5 my $fmt = shift;
21 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 4 my $self = shift;
23 4 $self->set_current_constraint_name('date_and_time');
24 4 return match_date_and_time($self,\$fmt);
25 }
26 5 }
27  
28 6 ▶sub match_date_and_time {
Subroutine match_date_and_time: called CC=7 SCAR=19.6
Pod undocumented
29 6 my $self = shift;
30 6 ▶ my $fmt_ref = shift || die q!date_and_time: need format parameter. Be sure to pass it by reference, like this: \'MM/DD/YYYY'!;
Condition50%1/2
shift() || die(q[date_and_time: need format parameter. Be sure to pass it by reference, like this: \\'MM/DD/YYYY'])
l!l
60
MC/DC0%0/1
shift() || die(q[date_and_time: need format parameter. Be sure to pass it by reference, like this: \\'MM/DD/YYYY'])
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
31 6 my $fmt = $$fmt_ref;
32  
33 6 require Date::Calc;
34 6 import Date::Calc (qw/check_date check_time/);
35  
36 6 my $format = _prepare_date_format($fmt);
37 6 my ($date,$Y,$M,$D,$h,$m,$s) = _parse_date_format($format,$self->get_current_constraint_value);
38 6 ▶ return if not defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 2 4 6
39  
40  
41 # We need to check the date if we find any in the format string, otherwise, it succeeds
42 4 my $date_test = 1;
43 4 ▶ $date_test = check_date($Y,$M,$D) if ($fmt =~ /[YMD]/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $fmt =~ /[YMD]/ 4 0 4
44  
45 # If we find a time, check that
46 4 my $time_test = 1;
47 4 ▶ $time_test = check_time($h,$m,$s) if ($fmt =~ /[hms]/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $fmt =~ /[hms]/ 2 2 4
48  
49 # If either the time or date fails, it all fails
50 4 ▶ return ($date_test && $time_test) ? $date : undef;
Branch: 1 branch
BranchTrueFalseTotal
$date_test && $time_test ? : 3 1 4
Condition66%2/3
$date_test and $time_test
!ll&&!rl&&r
103
MC/DC50%1/2
$date_test and $time_test
$date_test $time_test
Truth table66%2/3
A and B
A= $date_testB= $time_test
ABresult
0X0
100
111
51 }
52  
53 11 ▶sub _prepare_date_format {
Subroutine _prepare_date_format: called CC=5 SCAR=16.1
54 11 my $format = shift;
55  
56 # Originally by Jan Krynicky
57  
58 # TODO: check that only valid characters appear in the format
59 # The logic should be: for any character A-Z in the format string,
60 # die if it's not one of: Y M D h m s p
61  
62 11 my ($i, @order) = 0;
63 11 $format =~ s{(Y+|M+|D+|h+|m+|s+|pp)(\?)?}{
64 61 my ($chr,$q) = ($1,$2);
65 61 ▶ $chr = '' if not defined $chr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chr 0 61 61
66 61 ▶ $q = '' if not defined $chr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chr 0 61 61
67  
68 61 $order[$i++] = substr($chr,0,1);
69 61 ▶ if ($chr eq 'pp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chr eq 'pp') { } 6 55 61
70 6 "(AM|PM|am|pm)"
71 } else {
72 55 ▶ '(' . ('\d' x length($chr)) . ($q ? $q : "") . ")"
Branch: 1 branch
BranchTrueFalseTotal
$q ? : 6 49 55
73 }
74 }ge;
75  
76  
77 11 $format = qr/^((?:$format))$/;
78 11 return [$format, \@order];
79 }
80  
81 11 ▶sub _parse_date_format {
Subroutine _parse_date_format: called CC=10 SCAR=24.1
82 # Originally by Jan Krynicky
83  
84 11 my ($format, $date) = @_;
85 11 ▶ my ($untainted_date,@data) = ($date =~ $format->[0])
Branch: 1 branch
BranchTrueFalseTotal
() = $date =~ /$format->[0]/ or return 9 2 11
86 or return;
87 9 my %result;
88 9 ▶ for(my $i = 0; $i <= $#data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#data) 49 9 58
89 49 ▶ $result{$format->[1]->[$i]} ||= $data[$i];
Condition66%2/3
$result{$format->[1][$i]} ||= $data[$i]
l!l&&r!l&&!r
2470
MC/DC0%0/2
$result{$format->[1][$i]} ||= $data[$i]
$result{$format->[1][$i]} $data[$i]
Truth table66%2/3
A ||= B
A= $result{$format->[1][$i]}B= $data[$i]
ABresult
1X1
011
000
90 }
91  
92 9 ▶ if (exists $result{p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result{"p"}) 4 5 9
93 4 ▶ $result{h} += 12 if ($result{p} eq 'PM' and $result{h} != 12);
Branch: 1 branch
BranchTrueFalseTotal
if $result{"p"} eq "PM" and $result{"h"} != 12 4 0 4
Condition33%1/3
$result{"p"} eq "PM" and $result{"h"} != 12
!ll&&!rl&&r
004
MC/DC0%0/2
$result{"p"} eq "PM" and $result{"h"} != 12
$result{"p"} eq "PM" $result{"h"} != 12
Truth table33%1/3
A and B
A= $result{"p"} eq "PM"B= $result{"h"} != 12
ABresult
0X0
100
111
94 4 ▶ $result{h} = 0 if ($result{p} eq 'AM' and $result{h} == 12);
Branch: 1 branch
BranchTrueFalseTotal
if $result{"p"} eq "AM" and $result{"h"} == 12 0 4 4
Condition33%1/3
$result{"p"} eq "AM" and $result{"h"} == 12
!ll&&!rl&&r
400
MC/DC0%0/2
$result{"p"} eq "AM" and $result{"h"} == 12
$result{"p"} eq "AM" $result{"h"} == 12
Truth table33%1/3
A and B
A= $result{"p"} eq "AM"B= $result{"h"} == 12
ABresult
0X0
100
111
95 }
96  
97  
98 9 ▶ return $untainted_date, map {defined $result{$_} ? $result{$_} : 0} qw(Y M D h m s);
Branch: 1 branch
BranchTrueFalseTotal
defined $result{$_} ? : 43 11 54
99 }
100  
101 2 1;
102 __END__
« blib/lib/Data/FormValidator/ConstraintsFactory.pm ↑ Summary blib/lib/Data/FormValidator/Filters.pm »