| 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)"
MC/DC0%0/1
$args{"coerce_to"} // "float(epoch)"
$args{"coerce_to"}
Truth table50%1/2
A
A= $args{"coerce_to"}
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 37 | 5 | ▶ | $res->{modules}{"Time::Local"} //= 0; | ||||||||||||||||
|
Condition50%1/2
$res->{"modules"}{"Time::Local"} //= 0
MC/DC0%0/1
$res->{"modules"}{"Time::Local"} //= 0
$res->{"modules"}{"Time::Local"}
Truth table50%1/2
A
A= $res->{"modules"}{"Time::Local"}
| |||||||||||||||||||
| 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
MC/DC0%0/1
$res->{"modules"}{"DateTime"} //= 0
$res->{"modules"}{"DateTime"}
Truth table50%1/2
A
A= $res->{"modules"}{"DateTime"}
| |||||||||||||||||||
| 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
MC/DC0%0/1
$res->{"modules"}{"Time::Moment"} //= 0
$res->{"modules"}{"Time::Moment"}
Truth table0%0/2
A
A= $res->{"modules"}{"Time::Moment"}
| |||||||||||||||||||
| 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__ | ||||||||||||||||||