Data/Sah/Util/Type/Date.pm

filter: Statementstmt 86.5% 77 / 89 Branchbran 81.2% 52 / 64 Conditioncond 65.6% 44 / 67 MC/DCmcdc 63.1% 24 / 38 Subroutinesub 83.3% 5 / 6 Podpod 100% 2 / 2 totaltotal 76.6% 204 / 266 CC 56 SCAR 42.6
CC
56
Cov
79%
coerce_duration
182.0
coerce_date
44.3
BEGIN
1.0
CRAP 70.8
« Data/Sah/Compiler/perl/TH/duration.pm ↑ Summary Data/Sah/Compiler/Prog.pm »
1 package Data::Sah::Util::Type::Date;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 2 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 require Exporter;
11  
12 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 2 our $DATE = '2024-02-16'; # DATE
14 2 our $DIST = 'Data-Sah'; # DIST
15 2 our $VERSION = '0.917'; # VERSION
16  
17 2 our @ISA = qw(Exporter);
18 2 our @EXPORT_OK = qw(
19 coerce_date
20 coerce_duration
21 );
22  
23 our $DATE_MODULE = $ENV{DATA_SAH_DATE_MODULE} // $ENV{PERL_DATE_MODULE} //
24 2 ▶ "DateTime"; # XXX change defaults to Time::Piece (core)
Condition40%2/5
($ENV{"DATA_SAH_DATE_MODULE"} // $ENV{"PERL_DATE_MODULE"}) // "DateTime"
l!l
02
$ENV{"DATA_SAH_DATE_MODULE"} // $ENV{"PERL_DATE_MODULE"}
l!l&&r!l&&!r
002
MC/DC0%0/2
($ENV{"DATA_SAH_DATE_MODULE"} // $ENV{"PERL_DATE_MODULE"}) // "DateTime"
$ENV{"DATA_SAH_DATE_MODULE"} $ENV{"PERL_DATE_MODULE"}
Truth table33%1/3
A // B
A= $ENV{"DATA_SAH_DATE_MODULE"}B= $ENV{"PERL_DATE_MODULE"}
ABresult
1X1
011
000
25  
26 2 my $re_ymd = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/;
27 2 my $re_ymdThmsZ = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z\z/;
28  
29 33 ▶sub coerce_date {
Subroutine coerce_date: called CC=44 SCAR=37.9
Pod documented
30 33 my $val = shift;
31 33 ▶ if (!defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 3 30 33
32 3 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
33 }
34  
35 30 ▶ if ($DATE_MODULE eq 'DateTime') {
Branch: 3 branches
BranchTrueFalseTotal
if ($Data::Sah::Util::Type::Date::DATE_MODULE eq 'DateTime') { } 10 20 30
elsif ($Data::Sah::Util::Type::Date::DATE_MODULE eq 'Time::Moment') { } 10 10 20
elsif ($Data::Sah::Util::Type::Date::DATE_MODULE eq 'Time::Piece') { } 10 0 10
36 10 require DateTime;
37 10 ▶ if (blessed($val) && $val->isa('DateTime')) {
Branch: 6 branches
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa('DateTime')) { } 1 9 10
elsif (&looks_like_number($val) and $val >= 100000000 and $val <= 2147483648.0) { } 1 8 9
elsif ($val =~ /$re_ymd/) { } 2 6 8
elsif ($val =~ /$re_ymdThmsZ/) { } 1 5 6
elsif (builtin::blessed($val) and $val->isa('Time::Moment')) { } 1 4 5
elsif (builtin::blessed($val) and $val->isa('Time::Piece')) { } 1 3 4
Condition100%3/3
builtin::blessed($val) and $val->isa("DateTime")
!ll&&!rl&&r
721
MC/DC100%2/2
builtin::blessed($val) and $val->isa("DateTime")
builtin::blessed($val) $val->isa("DateTime")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("DateTime")
ABresult
0X0
100
111
38 1 return $val;
39 ▶ } elsif (looks_like_number($val) && $val >= 10**8 && $val <= 2**31) {
Condition100%6/6
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
!ll&&!rl&&r
711
&looks_like_number($val) and $val >= 100000000
!ll&&!rl&&r
612
MC/DC100%3/3
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
&looks_like_number($val) $val >= 100000000 $val <= "2147483648"
Truth table100%4/4
A and B and C
A= &looks_like_number($val)B= $val >= 100000000C= $val <= "2147483648"
ABCresult
0XX0
10X0
1100
1111
40 1 return DateTime->from_epoch(epoch => $val);
41 } elsif ($val =~ $re_ymd) {
42 2 my $d;
43 2 eval { $d = DateTime->new(year=>$1, month=>$2, day=>$3, time_zone=>'UTC') };
44 2 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
45 1 return $d;
46 } elsif ($val =~ $re_ymdThmsZ) {
47 1 my $d;
48 1 eval { $d = DateTime->new(year=>$1, month=>$2, day=>$3, hour=>$4, minute=>$5, second=>$6, time_zone=>'UTC') };
49 1 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
50 1 return $d;
51 ▶ } elsif (blessed($val) && $val->isa('Time::Moment')) {
Condition100%3/3
builtin::blessed($val) and $val->isa("Time::Moment")
!ll&&!rl&&r
311
MC/DC100%2/2
builtin::blessed($val) and $val->isa("Time::Moment")
builtin::blessed($val) $val->isa("Time::Moment")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Moment")
ABresult
0X0
100
111
52 1 return DateTime->from_epoch(epoch => $val->epoch);
53 ▶ } elsif (blessed($val) && $val->isa('Time::Piece')) {
Condition66%2/3
builtin::blessed($val) and $val->isa("Time::Piece")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($val) and $val->isa("Time::Piece")
builtin::blessed($val) $val->isa("Time::Piece")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Piece")
ABresult
0X0
100
111
54 1 return DateTime->from_epoch(epoch => $val->epoch);
55 } else {
56 3 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
57 }
58 } elsif ($DATE_MODULE eq 'Time::Moment') {
59 10 require Time::Moment;
60 10 ▶ if (blessed($val) && $val->isa('Time::Moment')) {
Branch: 6 branches
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa('Time::Moment')) { } 1 9 10
elsif (&looks_like_number($val) and $val >= 100000000 and $val <= 2147483648.0) { } 1 8 9
elsif ($val =~ /$re_ymd/) { } 2 6 8
elsif ($val =~ /$re_ymdThmsZ/) { } 1 5 6
elsif (builtin::blessed($val) and $val->isa('DateTime')) { } 1 4 5
elsif (builtin::blessed($val) and $val->isa('Time::Piece')) { } 1 3 4
Condition100%3/3
builtin::blessed($val) and $val->isa("Time::Moment")
!ll&&!rl&&r
721
MC/DC100%2/2
builtin::blessed($val) and $val->isa("Time::Moment")
builtin::blessed($val) $val->isa("Time::Moment")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Moment")
ABresult
0X0
100
111
61 1 return $val;
62 ▶ } elsif (looks_like_number($val) && $val >= 10**8 && $val <= 2**31) {
Condition100%6/6
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
!ll&&!rl&&r
711
&looks_like_number($val) and $val >= 100000000
!ll&&!rl&&r
612
MC/DC100%3/3
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
&looks_like_number($val) $val >= 100000000 $val <= "2147483648"
Truth table100%4/4
A and B and C
A= &looks_like_number($val)B= $val >= 100000000C= $val <= "2147483648"
ABCresult
0XX0
10X0
1100
1111
63 1 return Time::Moment->from_epoch(int($val), $val-int($val));
64 } elsif ($val =~ $re_ymd) {
65 2 my $d;
66 2 eval { $d = Time::Moment->new(year=>$1, month=>$2, day=>$3) };
67 2 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
68 1 return $d;
69 } elsif ($val =~ $re_ymdThmsZ) {
70 1 my $d;
71 1 eval { $d = Time::Moment->new(year=>$1, month=>$2, day=>$3, hour=>$4, minute=>$5, second=>$6) };
72 1 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
73 1 return $d;
74 ▶ } elsif (blessed($val) && $val->isa('DateTime')) {
Condition100%3/3
builtin::blessed($val) and $val->isa("DateTime")
!ll&&!rl&&r
311
MC/DC100%2/2
builtin::blessed($val) and $val->isa("DateTime")
builtin::blessed($val) $val->isa("DateTime")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("DateTime")
ABresult
0X0
100
111
75 1 return Time::Moment->from_epoch($val->epoch);
76 ▶ } elsif (blessed($val) && $val->isa('Time::Piece')) {
Condition66%2/3
builtin::blessed($val) and $val->isa("Time::Piece")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($val) and $val->isa("Time::Piece")
builtin::blessed($val) $val->isa("Time::Piece")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Piece")
ABresult
0X0
100
111
77 1 return Time::Moment->from_epoch($val->epoch);
78 } else {
79 3 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
80 }
81 } elsif ($DATE_MODULE eq 'Time::Piece') {
82 10 require Time::Piece;
83 10 ▶ if (blessed($val) && $val->isa('Time::Piece')) {
Branch: 6 branches
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa('Time::Piece')) { } 1 9 10
elsif (&looks_like_number($val) and $val >= 100000000 and $val <= 2147483648.0) { } 1 8 9
elsif ($val =~ /$re_ymd/) { } 2 6 8
elsif ($val =~ /$re_ymdThmsZ/) { } 1 5 6
elsif (builtin::blessed($val) and $val->isa('DateTime')) { } 1 4 5
elsif (builtin::blessed($val) and $val->isa('Time::Moment')) { } 1 3 4
Condition100%3/3
builtin::blessed($val) and $val->isa("Time::Piece")
!ll&&!rl&&r
721
MC/DC100%2/2
builtin::blessed($val) and $val->isa("Time::Piece")
builtin::blessed($val) $val->isa("Time::Piece")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Piece")
ABresult
0X0
100
111
84 1 return $val;
85 ▶ } elsif (looks_like_number($val) && $val >= 10**8 && $val <= 2**31) {
Condition100%6/6
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
!ll&&!rl&&r
711
&looks_like_number($val) and $val >= 100000000
!ll&&!rl&&r
612
MC/DC100%3/3
&looks_like_number($val) and $val >= 100000000 and $val <= "2147483648"
&looks_like_number($val) $val >= 100000000 $val <= "2147483648"
Truth table100%4/4
A and B and C
A= &looks_like_number($val)B= $val >= 100000000C= $val <= "2147483648"
ABCresult
0XX0
10X0
1100
1111
86 1 return scalar Time::Piece->gmtime($val);
87 } elsif ($val =~ $re_ymd) {
88 2 my $d;
89 2 eval { $d = Time::Piece->strptime($val, "%Y-%m-%d") };
90 2 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
91 1 return $d;
92 } elsif ($val =~ $re_ymdThmsZ) {
93 1 my $d;
94 1 eval { $d = Time::Piece->strptime($val, "%Y-%m-%dT%H:%M:%SZ") };
95 1 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
96 1 return $d;
97 ▶ } elsif (blessed($val) && $val->isa('DateTime')) {
Condition100%3/3
builtin::blessed($val) and $val->isa("DateTime")
!ll&&!rl&&r
311
MC/DC100%2/2
builtin::blessed($val) and $val->isa("DateTime")
builtin::blessed($val) $val->isa("DateTime")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa("DateTime")
ABresult
0X0
100
111
98 1 return scalar Time::Piece->gmtime(epoch => $val->epoch);
99 ▶ } elsif (blessed($val) && $val->isa('Time::Moment')) {
Condition66%2/3
builtin::blessed($val) and $val->isa("Time::Moment")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($val) and $val->isa("Time::Moment")
builtin::blessed($val) $val->isa("Time::Moment")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("Time::Moment")
ABresult
0X0
100
111
100 1 return scalar Time::Piece->gmtime(epoch => $val->epoch);
101 } else {
102 3 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
103 }
104 } else {
105 0 die "BUG: Unknown Perl date module '$DATE_MODULE'";
106 }
107 }
108  
109 0 ▶sub coerce_duration {
Subroutine coerce_duration: not called CC=13 SCAR=52.0
Pod documented
110 0 my $val = shift;
111 0 ▶ if (!defined($val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (builtin::blessed($val) and $val->isa('DateTime::Duration')) { } 0 0 0
elsif ($val =~ /\AP (?: ([0-9]+(?:\.[0-9]+)?)Y )? (?: ([0-9]+(?:\.[0-9]+)?)M )? (?: ([0-9]+(?:\.[0-9]+)?)W )? (?: ([0-9]+(?:\.[0-9]+)?)D )? (?: T (?: ([0-9]+(?:\.[0-9]+)?)H )? (?: ([0-9]+(?:\.[0-9]+)?)M )? (?: ([0-9]+(?:\.[0-9]+)?)S )? )? \z/x) { } 0 0 0
112 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
113 ▶ } elsif (blessed($val) && $val->isa('DateTime::Duration')) {
Condition0%0/3
builtin::blessed($val) and $val->isa("DateTime::Duration")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($val) and $val->isa("DateTime::Duration")
builtin::blessed($val) $val->isa("DateTime::Duration")
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa("DateTime::Duration")
ABresult
0X0
100
111
114 0 return $val;
115 } elsif ($val =~ /\AP
116 (?: ([0-9]+(?:\.[0-9]+)?)Y )?
117 (?: ([0-9]+(?:\.[0-9]+)?)M )?
118 (?: ([0-9]+(?:\.[0-9]+)?)W )?
119 (?: ([0-9]+(?:\.[0-9]+)?)D )?
120 (?:
121 T
122 (?: ([0-9]+(?:\.[0-9]+)?)H )?
123 (?: ([0-9]+(?:\.[0-9]+)?)M )?
124 (?: ([0-9]+(?:\.[0-9]+)?)S )?
125 )?
126 \z/x) {
127 0 require DateTime::Duration;
128 0 my $d;
129 0 eval {
130 0 ▶ $d = DateTime::Duration->new(
Condition0%0/14
$1 // 0
l!l
00
$2 // 0
l!l
00
$3 // 0
l!l
00
$4 // 0
l!l
00
$5 // 0
l!l
00
$6 // 0
l!l
00
$7 // 0
l!l
00
MC/DC0%0/1
$1 // 0
$1
MC/DC0%0/1
$2 // 0
$2
MC/DC0%0/1
$3 // 0
$3
MC/DC0%0/1
$4 // 0
$4
MC/DC0%0/1
$5 // 0
$5
MC/DC0%0/1
$6 // 0
$6
MC/DC0%0/1
$7 // 0
$7
Truth table0%0/2
A
A= $1
Aresult
11
00
Truth table0%0/2
A
A= $2
Aresult
11
00
Truth table0%0/2
A
A= $3
Aresult
11
00
Truth table0%0/2
A
A= $4
Aresult
11
00
Truth table0%0/2
A
A= $5
Aresult
11
00
Truth table0%0/2
A
A= $6
Aresult
11
00
Truth table0%0/2
A
A= $7
Aresult
11
00
131 years => $1 // 0,
132 months => $2 // 0,
133 weeks => $3 // 0,
134 days => $4 // 0,
135 hours => $5 // 0,
136 minutes => $6 // 0,
137 seconds => $7 // 0,
138 );
139 };
140 0 ▶ return undef if $@; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
141 0 return $d;
142 } else {
143 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
144 }
145 }
146  
147 2 1;
148 # ABSTRACT: Utility related to date/duration type
149  
150 __END__
« Data/Sah/Compiler/perl/TH/duration.pm ↑ Summary Data/Sah/Compiler/Prog.pm »