Coerce/perl/To_date/From_str/iso8601.pm

filter: Statementstmt 89.2% 25 / 28 Branchbran 50.0% 3 / 6 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 67.9% 36 / 53 CC 8 SCAR 21.1
CC
8
Cov
74%
coerce
11.9
BEGIN
1.0
BEGIN
1.0
CRAP 8.3
« Coerce.pm ↑ Summary Coerce/perl/To_datenotime/From_str/iso8601.pm »
1 package Data::Sah::Coerce::perl::To_date::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 7 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
13 +{
14 7 v => 4,
15 summary => 'Coerce date from (a subset of) ISO8601 string',
16 might_fail => 1, # we match any (YYYY-MM-DD... string, so the conversion to date might fail on invalid dates)
17 prio => 50,
18 };
19 }
20  
21 7 ▶sub coerce {
Subroutine coerce: called CC=8 SCAR=24.7
Pod undocumented
22 7 my %args = @_;
23  
24 7 my $dt = $args{data_term};
25 7 ▶ my $coerce_to = $args{coerce_to} // 'float(epoch)';
Condition50%1/2
$args{"coerce_to"} // "float(epoch)"
l!l
70
MC/DC0%0/1
$args{"coerce_to"} // "float(epoch)"
$args{"coerce_to"}
Truth table50%1/2
A
A= $args{"coerce_to"}
Aresult
11
00
26  
27 7 my $res = {};
28  
29 7 $res->{expr_match} = join(
30 " && ",
31 "!ref($dt)",
32 # 1=Y 2=M 3=D 4="T" 5=h 6=m 7=s 8="Z"
33 "$dt =~ /\\A([0-9]{4})-([0-9]{2})-([0-9]{2})(?:([T ])([0-9]{2}):([0-9]{2}):([0-9]{2})(Z?))?\\z/",
34 );
35  
36 7 ▶ if ($coerce_to eq 'float(epoch)') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(epoch)') { } 5 2 7
elsif ($coerce_to eq 'DateTime') { } 2 0 2
elsif ($coerce_to eq 'Time::Moment') { } 0 0 0
37 5 ▶ $res->{modules}{"Time::Local"} //= 0;
Condition50%1/2
$res->{"modules"}{"Time::Local"} //= 0
l!l
05
MC/DC0%0/1
$res->{"modules"}{"Time::Local"} //= 0
$res->{"modules"}{"Time::Local"}
Truth table50%1/2
A
A= $res->{"modules"}{"Time::Local"}
Aresult
11
00
38 5 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = \$8 ? Time::Local::timegm_modern(\$7, \$6, \$5, \$3, \$2-1, \$1) : \$4 ? Time::Local::timelocal_modern(\$7, \$6, \$5, \$3, \$2-1, \$1) : Time::Local::timelocal_modern(0, 0, 0, \$3, \$2-1, \$1) }; my \$err = \$@; if (\$err) { \$err =~ s/ at .+//s; ["Invalid date/time: \$err", \$time] } else { [undef, \$time] } });
39 } elsif ($coerce_to eq 'DateTime') {
40 2 ▶ $res->{modules}{"DateTime"} //= 0;
Condition50%1/2
$res->{"modules"}{"DateTime"} //= 0
l!l
02
MC/DC0%0/1
$res->{"modules"}{"DateTime"} //= 0
$res->{"modules"}{"DateTime"}
Truth table50%1/2
A
A= $res->{"modules"}{"DateTime"}
Aresult
11
00
41 2 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = DateTime->new(year=>\$1, month=>\$2, day=>\$3, ((hour=>\$5, minute=>\$6, second=>\$7) x !!\$4), time_zone => \$8 ? 'UTC' : 'local') }; my \$err = \$@; if (\$err) { \$err =~ s/ at .+//s; ["Invalid date/time: \$err", \$time] } else { [undef, \$time] } });
42 } elsif ($coerce_to eq 'Time::Moment') {
43 0 ▶ $res->{modules}{"Time::Moment"} //= 0;
Condition0%0/2
$res->{"modules"}{"Time::Moment"} //= 0
l!l
00
MC/DC0%0/1
$res->{"modules"}{"Time::Moment"} //= 0
$res->{"modules"}{"Time::Moment"}
Truth table0%0/2
A
A= $res->{"modules"}{"Time::Moment"}
Aresult
11
00
44 # XXX set offset=>... when $8 is not Z?
45 0 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = Time::Moment->new(year=>\$1, month=>\$2, day=>\$3, ((hour=>\$5, minute=>\$6, second=>\$7) x !!\$4), offset=>0) }; my \$err = \$@; if (\$err) { \$err =~ s/ at .+//s; ["Invalid date/time: \$err", \$time] } else { [undef, \$time] } });
46 } else {
47 0 die "BUG: Unknown coerce_to value '$coerce_to', ".
48 "please use float(epoch), DateTime, or Time::Moment";
49 }
50  
51 7 $res;
52 }
53  
54 3 1;
55 # ABSTRACT: Coerce date from (a subset of) ISO8601 string
56  
57 __END__
« Coerce.pm ↑ Summary Coerce/perl/To_datenotime/From_str/iso8601.pm »