| 1 | package DateTime::Format::Duration::ISO8601; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $DATE = '2018-06-23'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.008'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # Default error handler | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | unless (exists $args{ on_error }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | $args{ on_error } = sub { die shift }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | return bless \%args, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub format_duration { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_duration: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | my ($self, $dtdur) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | unless (eval { $dtdur->isa('DateTime::Duration') }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | return $self->_error(sprintf | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | '"%s": not a DateTime::Duration instance', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | $dtdur | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my ($y, $m, $w, $d, $H, $M, $S, $ns) = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | $dtdur->years, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | $dtdur->months, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | $dtdur->weeks, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | $dtdur->days, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | $dtdur->hours, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $dtdur->minutes, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | $dtdur->seconds, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | $dtdur->nanoseconds, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | $S += $ns / 1_000_000_000; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | $d += $w * 7; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | my $has_date = $y || $m || $w || $d; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%6/9
$y || $m || $w || $d
$y or $m or $w
$y or $m
MC/DC25%1/4
$y || $m || $w || $d
$y
$m
$w
$d
Truth table40%2/5
A or B or C || D
A= $yB= $mC= $wD= $d
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | my $has_time = $H || $M || $S; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$H || $M || $S
$H or $M
MC/DC33%1/3
$H || $M || $S
$H
$M
$S
Truth table50%2/4
A or B || C
A= $HB= $MC= $S
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | return "PT0H0M0S" if !$has_date && !$has_time; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$has_date or $has_time
MC/DC100%2/2
$has_date or $has_time
$has_date
$has_time
Truth table100%3/3
A or B
A= $has_dateB= $has_time
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | join( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | "", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | "P", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ($y, "Y") x !!$y, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ($m, "M") x !!$m, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ($d, "D") x !!$d, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | "T", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ($H, "H") x !!$H, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ($M, "M") x !!$M, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ($S, "S") x !!$S, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ) x !!$has_time, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | sub parse_duration { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_duration: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | my ($self, $duration_string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | my $duration_args = $self->parse_duration_as_deltas($duration_string); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | ▶ | return unless defined $duration_args; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | if ($duration_args->{ repeats }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | return $self->_error(sprintf( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | '"%s": duration repetitions are not supported', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | $duration_string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # Convert weeks to days | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if (exists $duration_args->{ weeks }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $duration_args->{days} += delete($duration_args->{weeks}) * 7; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # Convert ss.sss floating seconds to seconds and nanoseconds | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | if (exists $duration_args->{ seconds }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | my ($seconds, $floating) = $duration_args->{ seconds } =~ qr{(?x) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ([0-9]+) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | (\.[0-9]+) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | if ($floating) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | my $nanoseconds = $floating * 1_000_000_000; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | $duration_args->{ seconds } = $seconds; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $duration_args->{ nanoseconds } = $nanoseconds; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | require DateTime::Duration; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | return DateTime::Duration->new(%{ $duration_args }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | sub parse_duration_as_deltas { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_duration_as_deltas: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | my ($self, $duration_string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | unless (defined $duration_string) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return $self->_error('Duration string undefined'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 7 | my $regex = qr{(?x) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ^ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | (?:(?<repeats>R(?<repetitions>[0-9]+(?:\.[0-9]*)?)?))? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | P | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | (?: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # PnW | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | (?: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | (?:(?<weeks>[0-9]+(?:\.[0-9]*)?)W) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # PnYnMnDTnHnMnS & P<date>T<time> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | (?: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | (?:(?<years>[0-9]+(?:\.[0-9]*)?)Y)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | (?:(?<months>[0-9]+(?:\.[0-9]*)?)M)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | (?:(?<days>[0-9]+(?:\.[0-9]*)?)D)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | (?:T | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | (?:(?<hours>[0-9]+(?:\.[0-9]*)?)H)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | (?:(?<minutes>[0-9]+(?:\.[0-9]*)?)M)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | (?:(?<seconds>[0-9]+(?:\.[0-9]*)?)S)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | )? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | )$ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 7 | ▶ | unless ($duration_string =~ $regex) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | return $self->_error(sprintf( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | '"%s": not a valid ISO 8601 duration string', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | $duration_string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 12 | my %fields = map { $_ => $+{ $_ } } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 5 | grep { defined $+{ $_ } } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | keys %+; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # XXX ISO standard only allows decimal fraction for the smallest unit | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 5 | return \%fields; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | sub _error { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 4 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | ▶ | die @args unless ref $self and ref $self->{ on_error } eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $self and ref $self->{"on_error"} eq "CODE"
MC/DC0%0/2
ref $self and ref $self->{"on_error"} eq "CODE"
ref $self
ref $self->{"on_error"} eq "CODE"
Truth table33%1/3
A and B
A= ref $selfB= ref $self->{"on_error"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | return $self->{ on_error }->(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # ABSTRACT: Parse and format ISO8601 duration | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||