blib/lib/DateTime/Format/Duration/XSD.pm

filter: Statementstmt 95.8% 46 / 48 Branchbran 60.7% 17 / 28 Conditioncond 88.2% 15 / 17 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 82.6% 95 / 115 CC 23 SCAR 23.9
CC
23
Cov
84%
format_duration
16.8
parse_duration
11.3
BEGIN
1.0
CRAP 11.0
↑ Summary
1 package DateTime::Format::Duration::XSD;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = 0.01;
13  
14 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 2 my $class = shift;
16 2 return bless {
17 fmt => DateTime::Format::Duration->new(
18 pattern => '%PP%YY%mM%dDT%HH%MM%S.%NS',
19 normalize => 1,
20 ),
21 }, $class;
22 }
23  
24 8 ▶sub parse_duration {
Subroutine parse_duration: called CC=11 SCAR=24.2
Pod undocumented
25 8 my ($self, $xs_duration) = @_;
26 8 my ($neg, $year, $mounth, $day, $hour, $min, $sec, $fsec);
27 8 ▶ if ($xs_duration =~ /^(-)?
Branch: 1 branch
BranchTrueFalseTotal
if ($xs_duration =~ /^(-)? P ((\d+)Y)? ((\d+)M)? ((\d+)D)? ( T ((\d+)H)? ((\d+)M)? (((\d+)(\.(\d+))?)S)? )? $/x) { } 8 0 8
28 P
29 ((\d+)Y)?
30 ((\d+)M)?
31 ((\d+)D)?
32 (
33 T
34 ((\d+)H)?
35 ((\d+)M)?
36 (((\d+)(\.(\d+))?)S)?
37 )?
38 $/x)
39 {
40 8 ($neg, $year, $mounth, $day, $hour, $min, $sec, $fsec) =
41 ($1, $3, $5, $7, $10, $12, $15, $17);
42 8 ▶ unless (grep {defined} ($year, $mounth, $day, $hour, $min, $sec)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {defined $_;} $year, $mounth, $day, $hour, $min, $sec) 0 8 8
43 0 croak "duration contains no data '$xs_duration'";
44 }
45 } else {
46 0 croak "duration string does not match standart: '$xs_duration'";
47 }
48 8 ▶ my $dt = DateTime::Duration->new(
Branch: 1 branch
BranchTrueFalseTotal
$fsec ? : 4 4 8
Condition100%12/12
$year || 0
l!l
62
$mounth || 0
l!l
62
$day || 0
l!l
62
$hour || 0
l!l
35
$min || 0
l!l
62
$sec || 0
l!l
44
MC/DC100%1/1
$year || 0
$year
MC/DC100%1/1
$mounth || 0
$mounth
MC/DC100%1/1
$day || 0
$day
MC/DC100%1/1
$hour || 0
$hour
MC/DC100%1/1
$min || 0
$min
MC/DC100%1/1
$sec || 0
$sec
Truth table100%2/2
A
A= $year
Aresult
11
00
Truth table100%2/2
A
A= $mounth
Aresult
11
00
Truth table100%2/2
A
A= $day
Aresult
11
00
Truth table100%2/2
A
A= $hour
Aresult
11
00
Truth table100%2/2
A
A= $min
Aresult
11
00
Truth table100%2/2
A
A= $sec
Aresult
11
00
49 years => $year || 0,
50 months => $mounth || 0,
51 days => $day || 0,
52 hours => $hour || 0,
53 minutes => $min || 0,
54 seconds => $sec || 0,
55 nanoseconds => ($fsec ? "0.$fsec" * 1E9 : 0),
56 );
57 8 ▶ return defined $neg ? $dt->inverse : $dt;
Branch: 1 branch
BranchTrueFalseTotal
defined $neg ? : 4 4 8
58 }
59  
60  
61 4 ▶sub format_duration {
Subroutine format_duration: called CC=13 SCAR=28.2
Pod undocumented
62 4 my ($self, $duration) = @_;
63 4 my %deltas = $self->{fmt}->normalize($duration);
64 4 ▶ if (exists $deltas{seconds} or exists $deltas{nanoseconds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $deltas{"seconds"} or exists $deltas{"nanoseconds"}) 4 0 4
Condition33%1/3
exists $deltas{"seconds"} or exists $deltas{"nanoseconds"}
l!l&&r!l&&!r
400
MC/DC0%0/2
exists $deltas{"seconds"} or exists $deltas{"nanoseconds"}
exists $deltas{"seconds"} exists $deltas{"nanoseconds"}
Truth table33%1/3
A or B
A= exists $deltas{"seconds"}B= exists $deltas{"nanoseconds"}
ABresult
1X1
011
000
65 $deltas{seconds} = ($deltas{seconds} || 0)
66 4 ▶ + (exists $deltas{nanoseconds} ? $deltas{nanoseconds} / 1E9 : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $deltas{'nanoseconds'} ? : 4 0 4
Condition100%2/2
$deltas{'seconds'} || 0
l!l
22
MC/DC100%1/1
$deltas{'seconds'} || 0
$deltas{'seconds'}
Truth table100%2/2
A
A= $deltas{'seconds'}
Aresult
11
00
67 }
68 4 ▶ my $str = $deltas{negative} ? "-P" : "P";
Branch: 1 branch
BranchTrueFalseTotal
$deltas{'negative'} ? : 2 2 4
69 4 ▶ $str .= "$deltas{years}Y" if exists $deltas{years};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"years"} 4 0 4
70 4 ▶ $str .= "$deltas{months}M" if exists $deltas{months};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"months"} 4 0 4
71 4 ▶ $str .= "$deltas{days}D" if exists $deltas{days};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"days"} 4 0 4
72 4 ▶ $str .= "T" if grep {exists $deltas{$_}} qw(hours minutes seconds);
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $deltas{$_};} "hours", "minutes", "seconds" 4 0 4
73 4 ▶ $str .= "$deltas{hours}H" if exists $deltas{hours};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"hours"} 4 0 4
74 4 ▶ $str .= "$deltas{minutes}M" if exists $deltas{minutes};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"minutes"} 4 0 4
75 4 ▶ $str .= "$deltas{seconds}S" if exists $deltas{seconds};
Branch: 1 branch
BranchTrueFalseTotal
if exists $deltas{"seconds"} 4 0 4
76  
77 4 return $str;
78 }
79  
80 1 1;
81  
82 =head1 NAME
83  
84 DateTime::Format::Duration::XSD
85  
86 =head1 SYNOPSIS
87  
88 use DateTime::Format::Duration::XSD;
89 my $dfdx = DateTime::Format::Duration::XSD->new();
90 my $duration_object = $dfdx->parse_duration('-P1Y2M3DT1H2M3.33S');
91 my $xsd_duration = $dfdx->format_duration($duration_object);
92  
93 =head1 DESCRIPTION
94  
95 DateTime::Format::Duration::XSD - Formats and parses DateTime::Duration
96 according to xs:duration
97  
98 =head1 SEE ALSO
99  
100 L<DateTime>, L<DateTime::Duration>, L<DateTime::Format::XSD>
101  
102 The XML Schema speficitation
103  
104 =head1 AUTHOR
105  
106 Smal D A, E<lt>mialinx@gmail.comE<gt>
107  
108 =head1 COPYRIGHT AND LICENSE
109  
110 Copyright (C) 2009 by Smal D A
111  
112 This library is free software; you can redistribute it and/or modify
113 it under the same terms as Perl itself, either Perl version 5.8.8 or,
114 at your option, any later version of Perl 5 you may have available.
115  
116 =cut
117  
↑ Summary