« 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'])
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()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # If either the time or date fails, it all fails | ||||||||||||||||||||||||||||
| 50 | 4 | ▶ | return ($date_test && $time_test) ? $date : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$date_test and $time_test
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 61 | ▶ | $q = '' if not defined $chr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 61 | $order[$i++] = substr($chr,0,1); | |||||||||||||||||||||||||||
| 69 | 61 | ▶ | if ($chr eq 'pp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 6 | "(AM|PM|am|pm)" | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 55 | ▶ | '(' . ('\d' x length($chr)) . ($q ? $q : "") . ")" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | or return; | ||||||||||||||||||||||||||||
| 87 | 9 | my %result; | |||||||||||||||||||||||||||
| 88 | 9 | ▶ | for(my $i = 0; $i <= $#data; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 49 | ▶ | $result{$format->[1]->[$i]} ||= $data[$i]; | ||||||||||||||||||||||||||
|
Condition66%2/3
$result{$format->[1][$i]} ||= $data[$i]
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]
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 9 | ▶ | if (exists $result{p}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | $result{h} += 12 if ($result{p} eq 'PM' and $result{h} != 12); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$result{"p"} eq "PM" and $result{"h"} != 12
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
| |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | $result{h} = 0 if ($result{p} eq 'AM' and $result{h} == 12); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$result{"p"} eq "AM" and $result{"h"} == 12
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
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 9 | ▶ | return $untainted_date, map {defined $result{$_} ? $result{$_} : 0} qw(Y M D h m s); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | 1; | |||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||