Coerce/perl/To_duration/From_str/iso8601.pm

filter: Statementstmt 96.1% 25 / 26 Branchbran 75.0% 3 / 4 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 81.4% 35 / 43 CC 5 SCAR 12.0
CC
5
Cov
88%
coerce
5.2
BEGIN
1.0
BEGIN
1.0
CRAP 3.3
« Coerce/perl/To_duration/From_str/hms.pm ↑ Summary Coerce/perl/To_bool/From_str/common_words.pm »
1 package Data::Sah::Coerce::perl::To_duration::From_str::iso8601;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 3 our $DATE = '2023-10-24'; # DATE
9 3 our $DIST = 'Data-Sah-Coerce'; # DIST
10 3 our $VERSION = '0.054'; # VERSION
11  
12 3 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
13 +{
14 3 v => 4,
15 summary => 'Coerce duration from (subset of) ISO8601 string (e.g. "P1Y2M", "P14M")',
16 prio => 50,
17 };
18 }
19  
20 3 ▶sub coerce {
Subroutine coerce: called CC=5 SCAR=16.5
Pod undocumented
21 3 my %args = @_;
22  
23 3 my $dt = $args{data_term};
24 3 ▶ my $coerce_to = $args{coerce_to} // 'float(secs)';
Condition50%1/2
$args{"coerce_to"} // "float(secs)"
l!l
30
MC/DC0%0/1
$args{"coerce_to"} // "float(secs)"
$args{"coerce_to"}
Truth table50%1/2
A
A= $args{"coerce_to"}
Aresult
11
00
25  
26 3 my $res = {};
27  
28 3 my $re_num = '[0-9]+(?:\\.[0-9]+)?';
29 3 $res->{expr_match} = join(
30 " && ",
31 "!ref($dt)",
32 # #1=Y #2=M(on) #3=W #4=D #5=H #6=M(in) #7=S
33 "$dt =~ /\\AP(?:($re_num)Y)? (?:($re_num)M)? (?:($re_num)W)? (?:($re_num)D)? (?: T (?:($re_num)H)? (?:($re_num)M)? (?:($re_num)S)? )?\\z/x",
34 );
35  
36 3 ▶ if ($coerce_to eq 'float(secs)') {
Branch: 2 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(secs)') { } 2 1 3
elsif ($coerce_to eq 'DateTime::Duration') { } 1 0 1
37 # approximation
38 2 $res->{expr_coerce} = "((\$1||0)*365.25*86400 + (\$2||0)*30.4375*86400 + (\$3||0)*7*86400 + (\$4||0)*86400 + (\$5||0)*3600 + (\$6||0)*60 + (\$7||0))";
39 } elsif ($coerce_to eq 'DateTime::Duration') {
40 1 ▶ $res->{modules}{"DateTime::Duration"} //= 0;
Condition50%1/2
$res->{"modules"}{"DateTime::Duration"} //= 0
l!l
01
MC/DC0%0/1
$res->{"modules"}{"DateTime::Duration"} //= 0
$res->{"modules"}{"DateTime::Duration"}
Truth table50%1/2
A
A= $res->{"modules"}{"DateTime::Duration"}
Aresult
11
00
41 1 $res->{expr_coerce} = "DateTime::Duration->new( (years=>\$1) x !!defined(\$1), (months=>\$2) x !!defined(\$2), (weeks=>\$3) x !!defined(\$3), (days=>\$4) x !!defined(\$4), (hours=>\$5) x !!defined(\$5), (minutes=>\$6) x !!defined(\$6), (seconds=>\$7) x !!defined(\$7))";
42 } else {
43 0 die "BUG: Unknown coerce_to value '$coerce_to', ".
44 "please use float(secs) or DateTime::Duration";
45 }
46  
47 3 $res;
48 }
49  
50 3 1;
51 # ABSTRACT: Coerce duration from (subset of) ISO8601 string (e.g. "P1Y2M", "P14M")
52  
53 __END__
« Coerce/perl/To_duration/From_str/hms.pm ↑ Summary Coerce/perl/To_bool/From_str/common_words.pm »