Coerce/perl/To_datetime/From_str/iso8601.pm

filter: Statementstmt 96.4% 27 / 28 Branchbran 83.3% 5 / 6 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 77.3% 41 / 53 CC 8 SCAR 18.1
CC
8
Cov
86%
coerce
8.6
BEGIN
1.0
BEGIN
1.0
CRAP 6.1
« Coerce/perl/To_datenotime/From_str/iso8601.pm ↑ Summary Coerce/perl/To_timeofday/From_str/hms.pm »
1 package Data::Sah::Coerce::perl::To_datetime::From_str::iso8601;
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 3 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
13 +{
14 3 v => 4,
15 summary => 'Coerce datetime 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 3 ▶sub coerce {
Subroutine coerce: called CC=8 SCAR=21.6
Pod undocumented
22 3 my %args = @_;
23  
24 3 my $dt = $args{data_term};
25 3 ▶ my $coerce_to = $args{coerce_to} // 'float(epoch)';
Condition50%1/2
$args{"coerce_to"} // "float(epoch)"
l!l
30
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 3 my $res = {};
28  
29 3 $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 3 ▶ if ($coerce_to eq 'float(epoch)') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(epoch)') { } 1 2 3
elsif ($coerce_to eq 'DateTime') { } 1 1 2
elsif ($coerce_to eq 'Time::Moment') { } 1 0 1
37 1 ▶ $res->{modules}{"Time::Local"} //= 0;
Condition50%1/2
$res->{"modules"}{"Time::Local"} //= 0
l!l
01
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 1 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = \$8 ? Time::Local::timegm_modern(\$7, \$6, \$5, \$3, \$2-1, \$1) : Time::Local::timelocal_modern(\$7, \$6, \$5, \$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 1 ▶ $res->{modules}{"DateTime"} //= 0;
Condition50%1/2
$res->{"modules"}{"DateTime"} //= 0
l!l
01
MC/DC0%0/1
$res->{"modules"}{"DateTime"} //= 0
$res->{"modules"}{"DateTime"}
Truth table50%1/2
A
A= $res->{"modules"}{"DateTime"}
Aresult
11
00
41 1 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = DateTime->new(year=>\$1, month=>\$2, day=>\$3, hour=>\$5, minute=>\$6, second=>\$7, 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 1 ▶ $res->{modules}{"Time::Moment"} //= 0;
Condition50%1/2
$res->{"modules"}{"Time::Moment"} //= 0
l!l
01
MC/DC0%0/1
$res->{"modules"}{"Time::Moment"} //= 0
$res->{"modules"}{"Time::Moment"}
Truth table50%1/2
A
A= $res->{"modules"}{"Time::Moment"}
Aresult
11
00
44 # XXX set offset=>... when $8 is not Z
45 1 $res->{expr_coerce} = qq(do { my \$time; eval { \$time = Time::Moment->new(year=>\$1, month=>\$2, day=>\$3, hour=>\$5, minute=>\$6, second=>\$7, 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 3 $res;
52 }
53  
54 2 1;
55 # ABSTRACT: Coerce datetime from (a subset of) ISO8601 string
56  
57 __END__
« Coerce/perl/To_datenotime/From_str/iso8601.pm ↑ Summary Coerce/perl/To_timeofday/From_str/hms.pm »