Coerce/perl/To_duration/From_str/hms.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_obj/datetime_duration.pm ↑ Summary Coerce/perl/To_duration/From_str/iso8601.pm »
1 package Data::Sah::Coerce::perl::To_duration::From_str::hms;
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 string in the form of hh:mm:ss (or hh:mm)',
16 might_fail => 1, # we match any (hh:mm:ss string, so the conversion might fail on invalid value)
17 prio => 40, # higher than From_str::human
18 };
19 }
20  
21 3 ▶sub coerce {
Subroutine coerce: called CC=5 SCAR=16.5
Pod undocumented
22 3 my %args = @_;
23  
24 3 my $dt = $args{data_term};
25 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
26  
27 3 my $res = {};
28  
29 3 $res->{expr_match} = join(
30 " && ",
31 "$dt =~ /\\A([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2})(\\.[0-9]{1,9})?)?\\z/",
32 );
33  
34 3 my $code_check = qq(if (\$1 > 23) { ["Invalid hour '\$1', must be between 0-23"] } elsif (\$2 > 59) { ["Invalid minute '\$2', must be between 0-59"] } elsif (defined \$3 && \$3 > 59) { ["Invalid second '\$3', must be between 0-59"] });
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 2 $res->{expr_coerce} = qq(do { $code_check else { [undef, \$1*3600 + \$2*60 + (defined \$3 ? \$3 : 0) + (defined \$4 ? \$4 : 0)] } });
38 } elsif ($coerce_to eq 'DateTime::Duration') {
39 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
40 1 $res->{expr_coerce} = qq(do { $code_check else { [undef, DateTime::Duration->new(hours => \$1, minutes => \$2, seconds => (defined \$3 ? \$3 : 0), nanoseconds => (defined \$4 ? \$4 * 1e9 : 0))] } });
41 } else {
42 0 die "BUG: Unknown coerce_to value '$coerce_to', ".
43 "please use 'float(secs)' or 'DateTime::Duration'";
44 }
45  
46 3 $res;
47 }
48  
49 3 1;
50 # ABSTRACT: Coerce duration from string in the form of hh:mm:ss (or hh:mm)
51  
52 __END__
« Coerce/perl/To_duration/From_obj/datetime_duration.pm ↑ Summary Coerce/perl/To_duration/From_str/iso8601.pm »