« Coerce/perl/To_timeofday/From_obj/date_timeofday.pm
↑ Summary
Coerce/perl/To_duration/From_float/seconds.pm »
| 1 | package Data::Sah::Coerce::perl::To_duration::From_str::human; | ||||||||||||||
| 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 human notation string (e.g. "2 days 10 hours", "3h")', | ||||||||||||||
| 16 | might_fail => 1, # we feed most string to Time::Duration::Parse::AsHash which might croak when fed invalid string | ||||||||||||||
| 17 | prio => 60, | ||||||||||||||
| 18 | }; | ||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 3 | ▶ | sub coerce { | ||||||||||||
|
Subroutine
coerce: called CC=6 SCAR=18.6
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)"
MC/DC0%0/1
$args{"coerce_to"} // "float(secs)"
$args{"coerce_to"}
Truth table50%1/2
A
A= $args{"coerce_to"}
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 3 | my $res = {}; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 3 | $res->{expr_match} = join( | |||||||||||||
| 30 | " && ", | ||||||||||||||
| 31 | "!ref($dt)", | ||||||||||||||
| 32 | "$dt =~ /\\d.*[a-z]/", | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 3 | ▶ | $res->{modules}{"Time::Duration::Parse::AsHash"} //= 0; | ||||||||||||
|
Condition50%1/2
$res->{"modules"}{"Time::Duration::Parse::AsHash"} //= 0
MC/DC0%0/1
$res->{"modules"}{"Time::Duration::Parse::AsHash"} //= 0
$res->{"modules"}{"Time::Duration::Parse::AsHash"}
Truth table50%1/2
A
A= $res->{"modules"}{"Time::Duration::Parse::AsHash"}
| |||||||||||||||
| 36 | 3 | ▶ | if ($coerce_to eq 'float(secs)') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 37 | # approximation | ||||||||||||||
| 38 | 2 | $res->{expr_coerce} = qq(do { my \$p; eval { \$p = Time::Duration::Parse::AsHash::parse_duration($dt) }; my \$err = \$@; if (\$err) { \$err =~ s/ at .+//s; ["Invalid duration: \$err"] } else { [undef, (\$p->{years}||0) * 365.25*86400 + (\$p->{months}||0) * 30.4375*86400 + (\$p->{weeks}||0) * 7*86400 + (\$p->{days}||0) * 86400 + (\$p->{hours}||0) * 3600 + (\$p->{minutes}||0) * 60 + (\$p->{seconds}||0)] } }); | |||||||||||||
| 39 | } elsif ($coerce_to eq 'DateTime::Duration') { | ||||||||||||||
| 40 | 1 | ▶ | $res->{modules}{"DateTime::Duration"} //= 0; | ||||||||||||
|
Condition50%1/2
$res->{"modules"}{"DateTime::Duration"} //= 0
MC/DC0%0/1
$res->{"modules"}{"DateTime::Duration"} //= 0
$res->{"modules"}{"DateTime::Duration"}
Truth table50%1/2
A
A= $res->{"modules"}{"DateTime::Duration"}
| |||||||||||||||
| 41 | 1 | $res->{expr_coerce} = qq(do { my \$p; eval { \$p = Time::Duration::Parse::AsHash::parse_duration($dt) }; my \$err = \$@; if (\$err) { \$err =~ s/ at .+//s; ["Invalid duration: \$err"] } else { [undef, DateTime::Duration->new( (years=>\$p->{years}) x !!defined(\$p->{years}), (months=>\$p->{months}) x !!defined(\$p->{months}), (weeks=>\$p->{weeks}) x !!defined(\$p->{weeks}), (days=>\$p->{days}) x !!defined(\$p->{days}), (hours=>\$p->{hours}) x !!defined(\$p->{hours}), (minutes=>\$p->{minutes}) x !!defined(\$p->{minutes}), (seconds=>\$p->{seconds}) x !!defined(\$p->{seconds}))] } }); | |||||||||||||
| 42 | } else { | ||||||||||||||
| 43 | 0 | die "BUG: Unknown coerce_to value '$coerce_to', ". | |||||||||||||
| 44 | "please use float(secs) or DateTime::Duration"; | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 3 | $res; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 3 | 1; | |||||||||||||
| 51 | # ABSTRACT: Coerce duration from human notation string (e.g. "2 days 10 hours", "3h") | ||||||||||||||
| 52 | |||||||||||||||
| 53 | __END__ | ||||||||||||||