LIVR/Rules/DateTime.pm

filter: Statementstmt 100% 168 / 168 Branchbran 97.9% 145 / 148 Conditioncond 90.0% 117 / 130 MC/DCmcdc 83.7% 62 / 74 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 3 totaltotal 94.3% 513 / 544 CC 123 SCAR 24.5
CC
123
Cov
96%
_parse_datetime
19.0
__ANON__
18.0
_instant_bounds
14.1
CRAP 11.6
↑ Summary LIVR/Rules/Collection.pm »
1 package Validator::LIVR::Rules::DateTime;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.22';
7  
8 # LIVR 2.5: dates with bounds, datetimes and times. Grammars use ASCII digits only and match the
9 # whole value: \z, not $, which also matches before a trailing newline
10  
11 2 my $DATE_RE = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/;
12  
13 # YYYY-MM-DD "T" HH ":" MM [ ":" SS [ "." 1*9DIGIT ] ] ( "Z" / ( "+" / "-" ) HH ":" MM )
14 2 my $DATETIME_RE = qr/\A
15 ([0-9]{4})-([0-9]{2})-([0-9]{2})
16 T([0-9]{2}):([0-9]{2})
17 (?: :([0-9]{2}) (?: \.([0-9]{1,9}) )? )?
18 (?: Z | ([+-])([0-9]{2}):([0-9]{2}) )
19 \z/x;
20  
21 # HH:MM, HH:MM:SS or HH:MM:SS.fraction
22 2 my $TIME_RE = qr/\A([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\.([0-9]{1,9}))?)?\z/;
23  
24 2 my %RELATIVE_DAYS = ( yesterday => -1, today => 0, tomorrow => 1 );
25  
26 2 my @DAYS_IN_MONTH = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
27  
28 2 my $DAY_MS = 86_400_000;
29  
30 235 ▶sub _days_in_month {
Subroutine _days_in_month: called CC=5 SCAR=16.3
31 235 my ( $year, $month ) = @_;
32 235 ▶ return $DAYS_IN_MONTH[ $month - 1 ] if $month != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $month != 2 220 15 235
33 15 ▶ return ( $year % 4 == 0 && $year % 100 != 0 ) || $year % 400 == 0 ? 29 : 28;
Branch: 1 branch
BranchTrueFalseTotal
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0 ? : 10 5 15
Condition66%4/6
$year % 4 == 0 and $year % 100 != 0 or $year % 400 == 0
l!l&&r!l&&!r
1005
$year % 4 == 0 and $year % 100 != 0
!ll&&!rl&&r
5010
MC/DC33%1/3
$year % 4 == 0 and $year % 100 != 0 or $year % 400 == 0
$year % 4 == 0 $year % 100 != 0 $year % 400 == 0
Truth table40%2/5
A and B or C
A= $year % 4 == 0B= $year % 100 != 0C= $year % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
34 }
35  
36 223 ▶sub _is_date {
Subroutine _is_date: called CC=4 SCAR=14.1
37 223 my ( $year, $month, $day ) = @_;
38 223 ▶ return $month >= 1 && $month <= 12 && $day >= 1 && $day <= _days_in_month( $year, $month );
Condition77%7/9
$month >= 1 && $month <= 12 && $day >= 1 && $day <= _days_in_month($year, $month)
!ll&&!rl&&r
25216
$month >= 1 and $month <= 12 and $day >= 1
!ll&&!rl&&r
20221
$month >= 1 and $month <= 12
!ll&&!rl&&r
02221
MC/DC50%2/4
$month >= 1 && $month <= 12 && $day >= 1 && $day <= _days_in_month($year, $month)
$month >= 1 $month <= 12 $day >= 1 $day <= _days_in_month($year, $month)
Truth table60%3/5
A and B and C && D
A= $month >= 1B= $month <= 12C= $day >= 1D= $day <= _days_in_month($year, $month)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
39 }
40  
41 # iso_date with { min, max }. Validator::LIVR::Rules::Special::iso_date calls it for an object
42 # argument: the LIVR 2.0 forms are there
43 104 ▶sub iso_date {
Subroutine iso_date: called CC=3 SCAR=11.0
Pod undocumented
44 104 my ( $options, $rule_builders ) = ( $_[0], $_[-1] );
45  
46 # Arguments after the options are ignored
47 104 _check_options( 'iso_date', $options );
48 90 my $min = _date_bound( $options, 'min' );
49 86 my $max = _date_bound( $options, 'max' );
50  
51 80 ▶ return Validator::LIVR::Rules::Special::iso_date($rule_builders) if !defined($min) && !defined($max);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min or defined $max 18 62 80
Condition100%3/3
defined $min or defined $max
l!l&&r!l&&!r
392318
MC/DC100%2/2
defined $min or defined $max
defined $min defined $max
Truth table100%3/3
A or B
A= defined $minB= defined $max
ABresult
1X1
011
000
52 62 _check_order( 'iso_date', $min, $max );
53  
54 # A bound is a date, or the number of days from the current date: dates YYYY-MM-DD are
55 # compared as strings
56 64 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
57 64 my $value = shift;
58 64 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 58 64
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
3358
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
59  
60 58 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 4 54 58
61 4 ▶ return ref($value) =~ /Boolean/ ? 'WRONG_DATE' : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 1 3 4
62 }
63  
64 54 ▶ return 'WRONG_DATE' unless $value =~ $DATE_RE && _is_date( $1, $2, $3 );
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$DATE_RE/ and _is_date($1, $2, $3) 9 45 54
Condition100%3/3
$value =~ /$DATE_RE/ and _is_date($1, $2, $3)
!ll&&!rl&&r
7245
MC/DC100%2/2
$value =~ /$DATE_RE/ and _is_date($1, $2, $3)
$value =~ /$DATE_RE/ _is_date($1, $2, $3)
Truth table100%3/3
A and B
A= $value =~ /$DATE_RE/B= _is_date($1, $2, $3)
ABresult
0X0
100
111
65  
66 45 ▶ my $min_date = ref($min) ? _relative_date($$min) : $min;
Branch: 1 branch
BranchTrueFalseTotal
ref $min ? : 18 27 45
67 45 ▶ return 'DATE_TOO_EARLY' if defined($min_date) && $value lt $min_date;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min_date and $value lt $min_date 11 34 45
Condition100%3/3
defined $min_date and $value lt $min_date
!ll&&!rl&&r
102411
MC/DC100%2/2
defined $min_date and $value lt $min_date
defined $min_date $value lt $min_date
Truth table100%3/3
A and B
A= defined $min_dateB= $value lt $min_date
ABresult
0X0
100
111
68  
69 34 ▶ my $max_date = ref($max) ? _relative_date($$max) : $max;
Branch: 1 branch
BranchTrueFalseTotal
ref $max ? : 18 16 34
70 34 ▶ return 'DATE_TOO_LATE' if defined($max_date) && $value gt $max_date;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_date and $value gt $max_date 12 22 34
Condition100%3/3
defined $max_date and $value gt $max_date
!ll&&!rl&&r
61612
MC/DC100%2/2
defined $max_date and $value gt $max_date
defined $max_date $value gt $max_date
Truth table100%3/3
A and B
A= defined $max_dateB= $value gt $max_date
ABresult
0X0
100
111
71  
72 22 return;
73 60 };
74 }
75  
76 # A date, or a reference to the days from the current date, undef without the bound
77 176 ▶sub _date_bound {
Subroutine _date_bound: called CC=9 SCAR=22.0
78 176 my ( $options, $name ) = @_;
79 176 ▶ return undef if !exists $options->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{$name} 79 97 176
80  
81 97 my $bound = $options->{$name};
82 97 ▶ if ( defined($bound) && !ref($bound) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bound and not ref $bound) 96 1 97
Condition66%2/3
defined $bound and not ref $bound
!ll&&!rl&&r
0196
MC/DC50%1/2
defined $bound and not ref $bound
defined $bound not ref $bound
Truth table66%2/3
A and not(B)
A= defined $boundB= not ref $bound
ABresult
0X0
100
111
83 96 ▶ return \$RELATIVE_DAYS{$bound} if exists $RELATIVE_DAYS{$bound};
Branch: 1 branch
BranchTrueFalseTotal
if exists $RELATIVE_DAYS{$bound} 49 47 96
84 47 ▶ return $bound if $bound =~ $DATE_RE && _is_date( $1, $2, $3 );
Branch: 1 branch
BranchTrueFalseTotal
if $bound =~ /$DATE_RE/ and _is_date($1, $2, $3) 38 9 47
Condition100%3/3
$bound =~ /$DATE_RE/ and _is_date($1, $2, $3)
!ll&&!rl&&r
8138
MC/DC100%2/2
$bound =~ /$DATE_RE/ and _is_date($1, $2, $3)
$bound =~ /$DATE_RE/ _is_date($1, $2, $3)
Truth table100%3/3
A and B
A= $bound =~ /$DATE_RE/B= _is_date($1, $2, $3)
ABresult
0X0
100
111
85 }
86  
87 10 ▶ my $hint = defined($bound) && $bound eq 'current' ? ', use "today" instead of "current"' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $bound && $bound eq 'current' ? : 1 9 10
Condition66%2/3
defined $bound and $bound eq "current"
!ll&&!rl&&r
091
MC/DC50%1/2
defined $bound and $bound eq "current"
defined $bound $bound eq "current"
Truth table66%2/3
A and B
A= defined $boundB= $bound eq "current"
ABresult
0X0
100
111
88 10 die qq{Rule [iso_date] takes a date YYYY-MM-DD, "today", "yesterday" or "tomorrow" as "$name"$hint\n};
89 }
90  
91 156 ▶sub iso_datetime {
Subroutine iso_datetime: called CC=1 SCAR=0.0
Pod undocumented
92 156 my ( $min, $max ) = _instant_bounds(@_);
93  
94 140 ▶ return sub {
Subroutine __ANON__: called CC=18 SCAR=28.9
95 140 my ( $value, undef, $output_ref ) = @_;
96 140 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 8 132 140
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
35132
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
97  
98 132 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 7 125 132
99 7 ▶ return ref($value) =~ /Boolean/ ? 'WRONG_DATETIME' : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 3 4 7
100 }
101  
102 125 my @utc = _parse_datetime($value);
103 125 ▶ return 'WRONG_DATETIME' if !@utc || $utc[0] < 0 || $utc[0] > 9999;
Branch: 1 branch
BranchTrueFalseTotal
if not @utc or $utc[0] < 0 or $utc[0] > 9999 46 79 125
Condition100%6/6
not @utc or $utc[0] < 0 or $utc[0] > 9999
l!l&&r!l&&!r
44279
not @utc or $utc[0] < 0
l!l&&r!l&&!r
42281
MC/DC100%3/3
not @utc or $utc[0] < 0 or $utc[0] > 9999
not @utc $utc[0] < 0 $utc[0] > 9999
Truth table100%4/4
not(A) or B or C
A= not @utcB= $utc[0] < 0C= $utc[0] > 9999
ABCresult
1XX1
01X1
0011
0000
104  
105 79 ▶ if ( defined($min) || defined($max) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min or defined $max) 49 30 79
Condition100%3/3
defined $min or defined $max
l!l&&r!l&&!r
262330
MC/DC100%2/2
defined $min or defined $max
defined $min defined $max
Truth table100%3/3
A or B
A= defined $minB= defined $max
ABresult
1X1
011
000
106 49 my $key = _instant_key(@utc);
107  
108 49 ▶ my $min_key = ref($min) ? _relative_day_key($$min) : $min;
Branch: 1 branch
BranchTrueFalseTotal
ref $min ? : 9 40 49
109 49 ▶ return 'DATETIME_TOO_EARLY' if defined($min_key) && $key < $min_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min_key and $key < $min_key 10 39 49
Condition100%3/3
defined $min_key and $key < $min_key
!ll&&!rl&&r
231610
MC/DC100%2/2
defined $min_key and $key < $min_key
defined $min_key $key < $min_key
Truth table100%3/3
A and B
A= defined $min_keyB= $key < $min_key
ABresult
0X0
100
111
110  
111 39 ▶ my $max_key = ref($max) ? _relative_day_key($$max) + $DAY_MS - 1 : $max;
Branch: 1 branch
BranchTrueFalseTotal
ref $max ? : 14 25 39
112 39 ▶ return 'DATETIME_TOO_LATE' if defined($max_key) && $key > $max_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_key and $key > $max_key 13 26 39
Condition100%3/3
defined $max_key and $key > $max_key
!ll&&!rl&&r
91713
MC/DC100%2/2
defined $max_key and $key > $max_key
defined $max_key $key > $max_key
Truth table100%3/3
A and B
A= defined $max_keyB= $key > $max_key
ABresult
0X0
100
111
113 }
114  
115 # The instant in UTC with three fraction digits. 24 characters with "Z" is this form
116 56 ▶ if ( length($value) != 24 || substr( $value, 23 ) ne 'Z' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value != 24 or substr($value, 23) ne "Z") 51 5 56
Condition66%2/3
length $value != 24 or substr($value, 23) ne "Z"
l!l&&r!l&&!r
5105
MC/DC50%1/2
length $value != 24 or substr($value, 23) ne "Z"
length $value != 24 substr($value, 23) ne "Z"
Truth table66%2/3
A or B
A= length $value != 24B= substr($value, 23) ne "Z"
ABresult
1X1
011
000
117 51 $$output_ref = sprintf '%04d-%02d-%02dT%02d:%02d:%02d.%03dZ',
118 $utc[0], $utc[1], $utc[2], int( $utc[3] / 60 ), $utc[3] % 60, $utc[4], $utc[5];
119 }
120  
121 56 return;
122 139 };
123 }
124  
125 # The bounds of iso_datetime as keys of instants, or as references to the days from the
126 # current date. A date is the start of its UTC day as "min", the end of it as "max"
127 156 ▶sub _instant_bounds {
Subroutine _instant_bounds: called CC=14 SCAR=26.5
128 156 my @args = @_;
129 156 pop @args; # rule_builders
130 156 ▶ return () if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 73 83 156
131  
132 # Arguments after the options are ignored
133 83 _check_options( 'iso_datetime', $args[0] );
134  
135 80 my @bounds;
136 80 foreach my $name (qw/min max/) {
137 153 ▶ if ( !exists $args[0]{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args[0]{$name}) 64 89 153
138 64 push @bounds, undef;
139 64 next;
140 }
141  
142 89 my $bound = $args[0]{$name};
143 89 my $key;
144  
145 89 ▶ if ( defined($bound) && !ref($bound) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bound and not ref $bound) 89 0 89
Condition33%1/3
defined $bound and not ref $bound
!ll&&!rl&&r
0089
MC/DC0%0/2
defined $bound and not ref $bound
defined $bound not ref $bound
Truth table33%1/3
A and not(B)
A= defined $boundB= not ref $bound
ABresult
0X0
100
111
146 89 ▶ if ( exists $RELATIVE_DAYS{$bound} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $RELATIVE_DAYS{$bound}) { } 36 53 89
elsif ($bound =~ /$DATE_RE/) { } 20 33 53
elsif (my(@utc) = _parse_datetime($bound)) { } 24 9 33
147 36 $key = \$RELATIVE_DAYS{$bound};
148 } elsif ( $bound =~ $DATE_RE ) {
149 20 ▶ $key = _instant_key( $1, $2, $3, 0, 0, 0 ) + ( $name eq 'max' ? $DAY_MS - 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if _is_date($1, $2, $3) 19 1 20
$name eq 'max' ? : 8 11 19
150 if _is_date( $1, $2, $3 );
151 } elsif ( my @utc = _parse_datetime($bound) ) {
152 # Of any year: a bound is only compared
153 24 $key = _instant_key(@utc);
154 }
155 }
156  
157 89 ▶ if ( !defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 10 79 89
158 10 ▶ my $hint = defined($bound) && $bound eq 'current' ? ', use "today" instead of "current"' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $bound && $bound eq 'current' ? : 1 9 10
Condition66%2/3
defined $bound and $bound eq "current"
!ll&&!rl&&r
091
MC/DC50%1/2
defined $bound and $bound eq "current"
defined $bound $bound eq "current"
Truth table66%2/3
A and B
A= defined $boundB= $bound eq "current"
ABresult
0X0
100
111
159 10 die qq{Rule [iso_datetime] takes a datetime with an offset, a date YYYY-MM-DD, "today", "yesterday" or "tomorrow" as "$name"$hint\n};
160 }
161  
162 79 push @bounds, $key;
163 }
164  
165 70 _check_order( 'iso_datetime', @bounds );
166 66 return @bounds;
167 }
168  
169 # "min" later than "max" dies when both bounds are fixed or both are relative: no value could pass.
170 # A fixed and a relative bound depend on the current date. Fixed bounds are dates YYYY-MM-DD or
171 # keys of instants, relative bounds are references to the days from the current date
172 132 ▶sub _check_order {
Subroutine _check_order: called CC=7 SCAR=19.5
173 132 my ( $rule_name, $min, $max ) = @_;
174 132 ▶ return if !defined($min) || !defined($max) || ref($min) ne ref($max);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min && defined $max or ref $min ne ref $max 105 27 132
Condition100%6/6
not defined $min && defined $max or ref $min ne ref $max
l!l&&r!l&&!r
931227
defined $min && defined $max
!ll&&!rl&&r
603339
MC/DC100%3/3
not defined $min && defined $max or ref $min ne ref $max
defined $min defined $max ref $min ne ref $max
Truth table100%4/4
not(A && B) or C
A= defined $minB= defined $maxC= ref $min ne ref $max
ABCresult
0XX1
10X1
1111
1100
175  
176 27 ▶ my $is_later = ref($min) ? $$min > $$max : $rule_name eq 'iso_date' ? $min gt $max : $min > $max;
Branch: 2 branches
BranchTrueFalseTotal
ref $min ? : 10 17 27
$rule_name eq 'iso_date' ? : 11 6 17
177 27 ▶ die qq{Rule [$rule_name] got "min" later than "max"\n} if $is_later;
Branch: 1 branch
BranchTrueFalseTotal
if $is_later 6 21 27
178 }
179  
180 # A datetime with an offset as (year, month, day, minutes of the day, seconds, milliseconds)
181 # in UTC, cut to milliseconds. An empty list for any other string. The year may be -1 or 10000
182 158 ▶sub _parse_datetime {
Subroutine _parse_datetime: called CC=19 SCAR=29.4
183 158 ▶ return () unless $_[0] =~ $DATETIME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /$DATETIME_RE/x 41 117 158
184  
185 117 my ( $year, $month, $day, $hours, $minutes ) = ( $1, $2, $3, $4, $5 );
186 117 ▶ my $seconds = $6 // 0;
Condition100%2/2
$6 // 0
l!l
8037
MC/DC100%1/1
$6 // 0
$6
Truth table100%2/2
A
A= $6
Aresult
11
00
187 117 ▶ my $ms = defined $7 ? substr( $7 . '00', 0, 3 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $7 ? : 27 90 117
188 117 ▶ my $offset = defined $8 ? ( $9 * 60 + $10 ) * ( $8 eq '-' ? -1 : 1 ) : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $8 ? : 38 79 117
$8 eq '-' ? : 11 27 38
189  
190 117 ▶ return () if !_is_date( $year, $month, $day ) || $hours > 23 || $minutes > 59 || $seconds > 59;
Branch: 1 branch
BranchTrueFalseTotal
if not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59 or $seconds > 59 8 109 117
Condition100%9/9
not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59 or $seconds > 59
l!l&&r!l&&!r
71109
not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59
l!l&&r!l&&!r
61110
not _is_date($year, $month, $day) or $hours > 23
l!l&&r!l&&!r
33111
MC/DC100%4/4
not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59 or $seconds > 59
not _is_date($year, $month, $day) $hours > 23 $minutes > 59 $seconds > 59
Truth table100%5/5
not(A) or B or C or D
A= not _is_date($year, $month, $day)B= $hours > 23C= $minutes > 59D= $seconds > 59
ABCDresult
1XXX1
01XX1
001X1
00011
00000
191 109 ▶ return () if defined($8) && ( $9 > 23 || $10 > 59 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $8 and $9 > 23 || $10 > 59 2 107 109
Condition100%6/6
defined $8 and $9 > 23 || $10 > 59
!ll&&!rl&&r
71362
$9 > 23 or $10 > 59
l!l&&r!l&&!r
1136
MC/DC100%3/3
defined $8 and $9 > 23 || $10 > 59
defined $8 $9 > 23 $10 > 59
Truth table100%4/4
A and B or C
A= defined $8B= $9 > 23C= $10 > 59
ABCresult
0XX0
1000
11X1
1011
192  
193 # An offset moves the time by less than a day
194 107 my $minutes_of_day = $hours * 60 + $minutes - $offset;
195 107 ▶ if ( $minutes_of_day < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($minutes_of_day < 0) { } 10 97 107
elsif ($minutes_of_day >= 1440) { } 8 89 97
196 10 $minutes_of_day += 1440;
197 10 ▶ if ( --$day == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$day == 0) 6 4 10
198 6 ▶ if ( --$month == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$month == 0) 5 1 6
199 5 $month = 12;
200 5 $year--;
201 }
202 6 $day = _days_in_month( $year, $month );
203 }
204 } elsif ( $minutes_of_day >= 1440 ) {
205 8 $minutes_of_day -= 1440;
206 8 ▶ if ( ++$day > _days_in_month( $year, $month ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$day > _days_in_month($year, $month)) 4 4 8
207 4 $day = 1;
208 4 ▶ if ( ++$month == 13 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$month == 13) 4 0 4
209 4 $month = 1;
210 4 $year++;
211 }
212 }
213 }
214  
215 107 return ( $year + 0, $month + 0, $day + 0, $minutes_of_day, $seconds + 0, $ms + 0 );
216 }
217  
218 # A number in the order of instants: no calendar arithmetic, exact for all years of the grammar
219 110 ▶sub _instant_key {
Subroutine _instant_key: called CC=1 SCAR=0.0
220 110 my ( $year, $month, $day, $minutes_of_day, $seconds, $ms ) = @_;
221 110 return ( $year * 512 + $month * 32 + $day ) * $DAY_MS + ( $minutes_of_day * 60 + $seconds ) * 1000 + $ms;
222 }
223  
224 92 ▶sub iso_time {
Subroutine iso_time: called CC=8 SCAR=20.8
Pod undocumented
225 92 my @args = @_;
226 92 pop @args; # rule_builders
227  
228 92 my ( $min, $max );
229 92 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 51 41 92
230 # Arguments after the options are ignored
231 51 _check_options( 'iso_time', $args[0] );
232  
233 46 foreach my $name (qw/min max/) {
234 88 ▶ next if !exists $args[0]{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args[0]{$name} 15 73 88
235  
236 73 my $time = _time_of_day( $args[0]{$name} );
237 73 ▶ die qq{Rule [iso_time] takes a time HH:MM, HH:MM:SS or HH:MM:SS.fraction as "$name"\n} if !defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 7 66 73
238  
239 66 ▶ if ( $name eq 'min' ) { $min = $time }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'min') { } 33 33 66
240 33 else { $max = $time }
241 }
242 }
243  
244 # Over midnight, { min => '22:00', max => '06:00' }: a time in the gap gets the error of the
245 # nearer bound, TIME_TOO_EARLY exactly in the middle
246 80 ▶ my $is_over_midnight = defined($min) && defined($max) && $min > $max;
Condition100%6/6
defined $min && defined $max && $min > $max
!ll&&!rl&&r
501515
defined $min and defined $max
!ll&&!rl&&r
47330
MC/DC100%3/3
defined $min && defined $max && $min > $max
defined $min defined $max $min > $max
Truth table100%4/4
A and B && C
A= defined $minB= defined $maxC= $min > $max
ABCresult
0XX0
10X0
1100
1111
247  
248 80 ▶ return sub {
Subroutine __ANON__: called CC=14 SCAR=26.4
249 80 my $value = shift;
250 80 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 8 72 80
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
4472
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
251  
252 72 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 7 65 72
253 7 ▶ return ref($value) =~ /Boolean/ ? 'WRONG_TIME' : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 2 5 7
254 }
255  
256 65 my $time = _time_of_day($value);
257 65 ▶ return 'WRONG_TIME' if !defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 28 37 65
258  
259 37 ▶ if ($is_over_midnight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_over_midnight) 15 22 37
260 15 ▶ return if $time >= $min || $time <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if $time >= $min or $time <= $max 7 8 15
Condition100%3/3
$time >= $min or $time <= $max
l!l&&r!l&&!r
348
MC/DC100%2/2
$time >= $min or $time <= $max
$time >= $min $time <= $max
Truth table100%3/3
A or B
A= $time >= $minB= $time <= $max
ABresult
1X1
011
000
261 8 ▶ return $time - $max < $min - $time ? 'TIME_TOO_LATE' : 'TIME_TOO_EARLY';
Branch: 1 branch
BranchTrueFalseTotal
$time - $max < $min - $time ? : 3 5 8
262 }
263  
264 22 ▶ return 'TIME_TOO_EARLY' if defined($min) && $time < $min;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $time < $min 2 20 22
Condition100%3/3
defined $min and $time < $min
!ll&&!rl&&r
1192
MC/DC100%2/2
defined $min and $time < $min
defined $min $time < $min
Truth table100%3/3
A and B
A= defined $minB= $time < $min
ABresult
0X0
100
111
265 20 ▶ return 'TIME_TOO_LATE' if defined($max) && $time > $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $time > $max 3 17 20
Condition100%3/3
defined $max and $time > $max
!ll&&!rl&&r
1073
MC/DC100%2/2
defined $max and $time > $max
defined $max $time > $max
Truth table100%3/3
A and B
A= defined $maxB= $time > $max
ABresult
0X0
100
111
266 17 return;
267 80 };
268 }
269  
270 # Nanoseconds since midnight, exact (below 2^53), or undef for a wrong time
271 138 ▶sub _time_of_day {
Subroutine _time_of_day: called CC=10 SCAR=23.1
272 138 my $time = shift;
273 138 ▶ return undef if !defined($time) || ref($time) || $time !~ $TIME_RE;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $time or ref $time or not $time =~ /$TIME_RE/ 30 108 138
Condition50%3/6
not defined $time or ref $time or not $time =~ /$TIME_RE/
l!l&&r!l&&!r
030108
not defined $time or ref $time
l!l&&r!l&&!r
00138
MC/DC33%1/3
not defined $time or ref $time or not $time =~ /$TIME_RE/
not defined $time ref $time not $time =~ /$TIME_RE/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $timeB= ref $timeC= not $time =~ /$TIME_RE/
ABCresult
1XX1
01X1
0011
0000
274 108 ▶ return undef if $1 > 23 || $2 > 59 || ( defined($3) && $3 > 59 );
Branch: 1 branch
BranchTrueFalseTotal
if $1 > 23 or $2 > 59 or defined $3 and $3 > 59 5 103 108
Condition100%9/9
$1 > 23 or $2 > 59 or defined $3 and $3 > 59
l!l&&r!l&&!r
41103
$1 > 23 or $2 > 59
l!l&&r!l&&!r
31104
defined $3 and $3 > 59
!ll&&!rl&&r
78251
MC/DC100%4/4
$1 > 23 or $2 > 59 or defined $3 and $3 > 59
$1 > 23 $2 > 59 defined $3 $3 > 59
Truth table100%5/5
A or B or C and D
A= $1 > 23B= $2 > 59C= defined $3D= $3 > 59
ABCDresult
1XXX1
01XX1
00111
000X0
00100
275  
276 103 ▶ my $fraction = defined $4 ? $4 . '0' x ( 9 - length($4) ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 18 85 103
277 103 ▶ return ( ( $1 * 60 + $2 ) * 60 + ( $3 // 0 ) ) * 1_000_000_000 + $fraction;
Condition100%2/2
$3 // 0
l!l
2578
MC/DC100%1/1
$3 // 0
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
278 }
279  
280 238 ▶sub _check_options {
Subroutine _check_options: called CC=7 SCAR=19.5
281 238 my ( $rule_name, $options ) = @_;
282  
283 238 ▶ die qq{Rule [$rule_name] takes no arguments or an object with "min" and "max"\n} if ref($options) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $options ne "HASH" 13 225 238
284  
285 225 foreach my $name ( sort keys %$options ) {
286 269 ▶ next if $name eq 'min' || $name eq 'max';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "min" or $name eq "max" 260 9 269
Condition100%3/3
$name eq "min" or $name eq "max"
l!l&&r!l&&!r
1211399
MC/DC100%2/2
$name eq "min" or $name eq "max"
$name eq "min" $name eq "max"
Truth table100%3/3
A or B
A= $name eq "min"B= $name eq "max"
ABresult
1X1
011
000
287  
288 9 ▶ my $hint = $name eq 'format' && $rule_name eq 'iso_date' ? ', use "iso_datetime" for datetimes' : '';
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'format' && $rule_name eq 'iso_date' ? : 5 4 9
Condition100%3/3
$name eq "format" and $rule_name eq "iso_date"
!ll&&!rl&&r
315
MC/DC100%2/2
$name eq "format" and $rule_name eq "iso_date"
$name eq "format" $rule_name eq "iso_date"
Truth table100%3/3
A and B
A= $name eq "format"B= $rule_name eq "iso_date"
ABresult
0X0
100
111
289 9 die qq{Rule [$rule_name] got an unknown option "$name"$hint\n};
290 }
291 }
292  
293 # The current date: days since 1970-01-01 in UTC. Tests fix the time with Test::MockTime, which
294 # replaces CORE::GLOBAL::time: it has to be loaded before this module
295 59 ▶sub _current_day {
Subroutine _current_day: called CC=2 SCAR=7.0
296 59 my $seconds = time;
297  
298 59 my $day = int( $seconds / 86400 );
299 59 ▶ $day-- if $day * 86400 > $seconds;
Branch: 1 branch
BranchTrueFalseTotal
if $day * 86400 > $seconds 0 59 59
300 59 return $day;
301 }
302  
303 # The dates and keys of yesterday, today and tomorrow for the day in $cached_day: they change
304 # once a day
305 2 my $cached_day = 'none';
306 2 my ( %cached_dates, %cached_keys );
307  
308 6 ▶sub _cache_day {
Subroutine _cache_day: called CC=2 SCAR=6.9
309 6 my $today = shift;
310  
311 6 foreach my $days ( -1, 0, 1 ) {
312 18 my ( undef, undef, undef, $day, $month, $year ) = gmtime( ( $today + $days ) * 86400 );
313 18 $cached_dates{$days} = sprintf '%04d-%02d-%02d', $year + 1900, $month + 1, $day;
314 18 $cached_keys{$days} = _instant_key( $year + 1900, $month + 1, $day, 0, 0, 0 );
315 }
316  
317 6 $cached_day = $today;
318 }
319  
320 # YYYY-MM-DD of the current date plus $days
321 36 ▶sub _relative_date {
Subroutine _relative_date: called CC=2 SCAR=6.9
322 36 my $days = shift;
323 36 my $today = _current_day();
324 36 ▶ _cache_day($today) if $today ne $cached_day;
Branch: 1 branch
BranchTrueFalseTotal
if $today ne $cached_day 4 32 36
325 36 return $cached_dates{$days};
326 }
327  
328 # The key of the start of the current date plus $days
329 23 ▶sub _relative_day_key {
Subroutine _relative_day_key: called CC=2 SCAR=6.9
330 23 my $days = shift;
331 23 my $today = _current_day();
332 23 ▶ _cache_day($today) if $today ne $cached_day;
Branch: 1 branch
BranchTrueFalseTotal
if $today ne $cached_day 2 21 23
333 23 return $cached_keys{$days};
334 }
335  
336 2 1;
337  
338 =head1 NAME
339  
340 Validator::LIVR::Rules::DateTime - LIVR 2.5 rules of Validator::LIVR for dates and times: iso_date with bounds, iso_datetime and iso_time
341  
342 =head1 DESCRIPTION
343  
344 This module has the code of these rules, which are documented in
345 L<Validator::LIVR/LIVR 2.5 rules>. Validator::LIVR loads it when a validator
346 uses one of the rules. C<iso_date> without bounds is in
347 L<Validator::LIVR::Rules::Special>.
348  
349 Each sub is the builder of the rule with the same name.
350  
351 =cut
↑ Summary LIVR/Rules/Collection.pm »