Coerce/perl/To_timeofday/From_str/hms.pm

filter: Statementstmt 88.8% 24 / 27 Branchbran 50.0% 3 / 6 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 71.7% 33 / 46 CC 6 SCAR 16.7
CC
6
Cov
76%
coerce
8.2
BEGIN
1.0
BEGIN
1.0
CRAP 5.3
« Coerce/perl/To_datetime/From_str/iso8601.pm ↑ Summary Coerce/perl/To_date/From_float/epoch.pm »
1 package Data::Sah::Coerce::perl::To_timeofday::From_str::hms;
2  
3 2 ▶use 5.010001;
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  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2023-10-24'; # DATE
9 2 our $DIST = 'Data-Sah-Coerce'; # DIST
10 2 our $VERSION = '0.054'; # VERSION
11  
12 2 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
13 +{
14 2 v => 4,
15 summary => 'Coerce timeofday from string in the form of hh:mm:ss',
16 might_fail => 1, # we match any (hh:mm:ss string, so the conversion might fail on invalid value)
17 prio => 50,
18 };
19 }
20  
21 2 ▶sub coerce {
Subroutine coerce: called CC=6 SCAR=21.0
Pod undocumented
22 2 my %args = @_;
23  
24 2 my $dt = $args{data_term};
25 2 ▶ my $coerce_to = $args{coerce_to} // 'str_hms';
Condition50%1/2
$args{"coerce_to"} // "str_hms"
l!l
20
MC/DC0%0/1
$args{"coerce_to"} // "str_hms"
$args{"coerce_to"}
Truth table50%1/2
A
A= $args{"coerce_to"}
Aresult
11
00
26  
27 2 my $res = {};
28  
29 2 $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 2 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 2 ▶ if ($coerce_to eq 'float') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float') { } 1 1 2
elsif ($coerce_to eq 'str_hms') { } 1 0 1
elsif ($coerce_to eq 'Date::TimeOfDay') { } 0 0 0
37 1 $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 'str_hms') {
39 1 $res->{expr_coerce} = qq(do { $code_check else { [undef, defined(\$4) && \$4 > 0 ? sprintf("%02d:%02d:%s%.11g", \$1, \$2, (!defined(\$3) || \$3 < 10 ? "0":""), (defined \$3 ? \$3:0)+\$4) : sprintf("%02d:%02d:%02d", \$1, \$2, (defined \$3 ? \$3 : 0))] } });
40 } elsif ($coerce_to eq 'Date::TimeOfDay') {
41 0 ▶ $res->{modules}{"Date::TimeOfDay"} //= 0.002;
Condition0%0/2
$res->{"modules"}{"Date::TimeOfDay"} //= "0.002"
l!l
00
MC/DC0%0/1
$res->{"modules"}{"Date::TimeOfDay"} //= "0.002"
$res->{"modules"}{"Date::TimeOfDay"}
Truth table0%0/2
A
A= $res->{"modules"}{"Date::TimeOfDay"}
Aresult
11
00
42 0 $res->{expr_coerce} = qq(do { $code_check else { [undef, Date::TimeOfDay->new(hour=>\$1, minute=>\$2, second=>(defined \$3 ? \$3 : 0), nanosecond=>(defined \$4 ? \$4*1e9 : 0))] } });
43 } else {
44 0 die "BUG: Unknown coerce_to value '$coerce_to', ".
45 "please use float, str_hms, or Date::TimeOfDay";
46 }
47  
48 2 $res;
49 }
50  
51 2 1;
52 # ABSTRACT: Coerce timeofday from string in the form of hh:mm:ss
53  
54 __END__
« Coerce/perl/To_datetime/From_str/iso8601.pm ↑ Summary Coerce/perl/To_date/From_float/epoch.pm »