| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 15 | ▶ | return ( $year % 4 == 0 && $year % 100 != 0 ) || $year % 400 == 0 ? 29 : 28; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$year % 4 == 0 and $year % 100 != 0 or $year % 400 == 0
$year % 4 == 0 and $year % 100 != 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
$month >= 1 and $month <= 12 and $day >= 1
$month >= 1 and $month <= 12
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $min or defined $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 58 | ▶ | if ( ref($value) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | return ref($value) =~ /Boolean/ ? 'WRONG_DATE' : 'FORMAT_ERROR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 54 | ▶ | return 'WRONG_DATE' unless $value =~ $DATE_RE && _is_date( $1, $2, $3 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value =~ /$DATE_RE/ and _is_date($1, $2, $3)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 45 | ▶ | my $min_date = ref($min) ? _relative_date($$min) : $min; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 45 | ▶ | return 'DATE_TOO_EARLY' if defined($min_date) && $value lt $min_date; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $min_date and $value lt $min_date
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 34 | ▶ | my $max_date = ref($max) ? _relative_date($$max) : $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 34 | ▶ | return 'DATE_TOO_LATE' if defined($max_date) && $value gt $max_date; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $max_date and $value gt $max_date
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 97 | my $bound = $options->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 97 | ▶ | if ( defined($bound) && !ref($bound) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bound and not ref $bound
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 96 | ▶ | return \$RELATIVE_DAYS{$bound} if exists $RELATIVE_DAYS{$bound}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 47 | ▶ | return $bound if $bound =~ $DATE_RE && _is_date( $1, $2, $3 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$bound =~ /$DATE_RE/ and _is_date($1, $2, $3)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 10 | ▶ | my $hint = defined($bound) && $bound eq 'current' ? ', use "today" instead of "current"' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bound and $bound eq "current"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 132 | ▶ | if ( ref($value) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | return ref($value) =~ /Boolean/ ? 'WRONG_DATETIME' : 'FORMAT_ERROR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not @utc or $utc[0] < 0 or $utc[0] > 9999
not @utc or $utc[0] < 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 79 | ▶ | if ( defined($min) || defined($max) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $min or defined $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 49 | my $key = _instant_key(@utc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 49 | ▶ | my $min_key = ref($min) ? _relative_day_key($$min) : $min; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 49 | ▶ | return 'DATETIME_TOO_EARLY' if defined($min_key) && $key < $min_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $min_key and $key < $min_key
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 39 | ▶ | my $max_key = ref($max) ? _relative_day_key($$max) + $DAY_MS - 1 : $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 39 | ▶ | return 'DATETIME_TOO_LATE' if defined($max_key) && $key > $max_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $max_key and $key > $max_key
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
length $value != 24 or substr($value, 23) ne "Z"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $bound and not ref $bound
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 89 | ▶ | if ( exists $RELATIVE_DAYS{$bound} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 10 | ▶ | my $hint = defined($bound) && $bound eq 'current' ? ', use "today" instead of "current"' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bound and $bound eq "current"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not defined $min && defined $max or ref $min ne ref $max
defined $min && defined $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 27 | ▶ | my $is_later = ref($min) ? $$min > $$max : $rule_name eq 'iso_date' ? $min gt $max : $min > $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 27 | ▶ | die qq{Rule [$rule_name] got "min" later than "max"\n} if $is_later; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$6 // 0
$6
Truth table100%2/2
A
A= $6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 117 | ▶ | my $ms = defined $7 ? substr( $7 . '00', 0, 3 ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 117 | ▶ | my $offset = defined $8 ? ( $9 * 60 + $10 ) * ( $8 eq '-' ? -1 : 1 ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 117 | ▶ | return () if !_is_date( $year, $month, $day ) || $hours > 23 || $minutes > 59 || $seconds > 59; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59 or $seconds > 59
not _is_date($year, $month, $day) or $hours > 23 or $minutes > 59
not _is_date($year, $month, $day) or $hours > 23
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 109 | ▶ | return () if defined($8) && ( $9 > 23 || $10 > 59 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $8 and $9 > 23 || $10 > 59
$9 > 23 or $10 > 59
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 10 | $minutes_of_day += 1440; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | if ( --$day == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | ▶ | if ( --$month == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 4 | $day = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 4 | ▶ | if ( ++$month == 13 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 66 | ▶ | if ( $name eq 'min' ) { $min = $time } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
defined $min and defined $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 72 | ▶ | if ( ref($value) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 7 | ▶ | return ref($value) =~ /Boolean/ ? 'WRONG_TIME' : 'FORMAT_ERROR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 65 | my $time = _time_of_day($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 65 | ▶ | return 'WRONG_TIME' if !defined $time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 37 | ▶ | if ($is_over_midnight) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 15 | ▶ | return if $time >= $min || $time <= $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$time >= $min or $time <= $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | ▶ | return $time - $max < $min - $time ? 'TIME_TOO_LATE' : 'TIME_TOO_EARLY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 22 | ▶ | return 'TIME_TOO_EARLY' if defined($min) && $time < $min; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $min and $time < $min
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 20 | ▶ | return 'TIME_TOO_LATE' if defined($max) && $time > $max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $max and $time > $max
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
not defined $time or ref $time or not $time =~ /$TIME_RE/
not defined $time or ref $time
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 108 | ▶ | return undef if $1 > 23 || $2 > 59 || ( defined($3) && $3 > 59 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$1 > 23 or $2 > 59 or defined $3 and $3 > 59
$1 > 23 or $2 > 59
defined $3 and $3 > 59
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 103 | ▶ | my $fraction = defined $4 ? $4 . '0' x ( 9 - length($4) ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 103 | ▶ | return ( ( $1 * 60 + $2 ) * 60 + ( $3 // 0 ) ) * 1_000_000_000 + $fraction; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$3 // 0
MC/DC100%1/1
$3 // 0
$3
Truth table100%2/2
A
A= $3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 225 | foreach my $name ( sort keys %$options ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 269 | ▶ | next if $name eq 'min' || $name eq 'max'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "min" or $name eq "max"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 9 | ▶ | my $hint = $name eq 'format' && $rule_name eq 'iso_date' ? ', use "iso_datetime" for datetimes' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "format" and $rule_name eq "iso_date"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||