blib/lib/DateTime/Format/RFC3339.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 85.7% 36 / 42 Conditioncond 80.0% 8 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 92.5% 162 / 175 CC 26 SCAR 20.9
CC
26
Cov
94%
parse_datetime
13.3
format_datetime
11.0
new
4.0
CRAP 8.1
↑ Summary
1  
2 package DateTime::Format::RFC3339;
3  
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 ▶use version; our $VERSION = qv( 'v1.10.0' );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DateTime qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 ▶use constant FIRST_IDX => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use constant IDX_FORMAT => FIRST_IDX + 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant IDX_DECIMALS => FIRST_IDX + 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use constant IDX_SEP => FIRST_IDX + 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use constant IDX_SEP_RE => FIRST_IDX + 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant IDX_UC_ONLY => FIRST_IDX + 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant NEXT_IDX => FIRST_IDX + 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 3 my $default_self;
23  
24  
25 20 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
26 20 my $class = shift;
27 20 my %opts = @_;
28  
29 20 my $decimals = delete( $opts{ decimals } );
30 20 my $sep = delete( $opts{ sep } );
31 20 my $sep_re = delete( $opts{ sep_re } );
32 20 my $uc_only = delete( $opts{ uc_only } );
33  
34 20 ▶ $sep //= "T";
Condition100%2/2
$sep //= "T"
l!l
515
MC/DC100%1/1
$sep //= "T"
$sep
Truth table100%2/2
A
A= $sep
Aresult
11
00
35 20 ▶ $sep_re //= quotemeta( $sep );
Condition100%2/2
$sep_re //= quotemeta $sep
l!l
119
MC/DC100%1/1
$sep_re //= quotemeta $sep
$sep_re
Truth table100%2/2
A
A= $sep_re
Aresult
11
00
36 20 ▶ $uc_only = $uc_only ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$uc_only ? : 0 20 20
37  
38 20 my $self = bless( [], $class );
39  
40 #$self->[ IDX_FORMAT ] = undef;
41 20 $self->[ IDX_DECIMALS ] = $decimals;
42 20 $self->[ IDX_SEP ] = $sep;
43 20 $self->[ IDX_SEP_RE ] = $sep_re;
44 20 $self->[ IDX_UC_ONLY ] = $uc_only;
45  
46 20 return $self;
47 }
48  
49  
50 11 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=13 SCAR=25.9
Pod documented
51 11 my $self = shift;
52 11 my $str = shift;
53  
54 11 ▶ $self = $default_self //= $self->new()
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 7 4 11
Condition66%2/3
$default_self //= $self->new
l!l&&r!l&&!r
610
MC/DC0%0/2
$default_self //= $self->new
$default_self $self->new
Truth table66%2/3
A //= B
A= $default_selfB= $self->new
ABresult
1X1
011
000
55 if !ref( $self );
56  
57 11 ▶ $str = uc( $str )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[4] 11 0 11
58 if !$self->[ IDX_UC_ONLY ];
59  
60 11 ▶ my ( $Y, $M, $D ) = $str =~ s/^([0-9]{4})-([0-9]{2})-([0-9]{2})// ? ( 0+$1, 0+$2, 0+$3 ) : ()
Branch: 2 branches
BranchTrueFalseTotal
() = $str =~ s/^([0-9]{4})-([0-9]{2})-([0-9]{2})// ? (0 + $1, 0 + $2, 0 + $3) : () or croak("Incorrectly formatted date") 10 1 11
$str =~ s/^([0-9]{4})-([0-9]{2})-([0-9]{2})// ? : 10 1 11
61 or croak( "Incorrectly formatted date" );
62  
63 10 ▶ $str =~ s/^$self->[ IDX_SEP_RE ]//
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s/^$self->[3]// or croak("Incorrectly formatted datetime") 10 0 10
64 or croak( "Incorrectly formatted datetime" );
65  
66 10 ▶ my ( $h, $m, $s ) = $str =~ s/^([0-9]{2}):([0-9]{2}):([0-9]{2})// ? ( 0+$1, 0+$2, 0+$3 ) : ()
Branch: 2 branches
BranchTrueFalseTotal
() = $str =~ s/^([0-9]{2}):([0-9]{2}):([0-9]{2})// ? (0 + $1, 0 + $2, 0 + $3) : () or croak("Incorrectly formatted time") 9 1 10
$str =~ s/^([0-9]{2}):([0-9]{2}):([0-9]{2})// ? : 9 1 10
67 or croak( "Incorrectly formatted time" );
68  
69 9 ▶ my $ns = $str =~ s/^\.([0-9]{1,9})[0-9]*// ? 0+substr( $1.( '0' x 8 ), 0, 9 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s/^\.([0-9]{1,9})[0-9]*// ? : 1 8 9
70  
71 9 my $tz;
72 9 ▶ if ( $str =~ s/^Z// ) { $tz = 'UTC'; }
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ s/^Z//) { } 8 1 9
elsif ($str =~ s/^([+-])([0-9]{2}):([0-9]{2})//) { } 0 1 1
73 0 elsif ( $str =~ s/^([+-])([0-9]{2}):([0-9]{2})// ) { $tz = "$1$2$3"; }
74 1 else { croak( "Incorrect or missing time zone offset" ); }
75  
76 8 ▶ $str =~ /^\z/
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^\z/ or croak("Incorrectly formatted datetime") 7 1 8
77 or croak( "Incorrectly formatted datetime" );
78  
79 7 return DateTime->new(
80 year => $Y,
81 month => $M,
82 day => $D,
83 hour => $h,
84 minute => $m,
85 second => $s,
86 nanosecond => $ns,
87 time_zone => $tz,
88 formatter => $self,
89 );
90 }
91  
92  
93 20 ▶sub format_datetime {
Subroutine format_datetime: called CC=11 SCAR=24.0
Pod documented
94 20 my $self = shift;
95 20 my $dt = shift;
96  
97 20 ▶ $self = $default_self //= $self->new()
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 15 20
Condition66%2/3
$default_self //= $self->new
l!l&&r!l&&!r
410
MC/DC0%0/2
$default_self //= $self->new
$default_self $self->new
Truth table66%2/3
A //= B
A= $default_selfB= $self->new
ABresult
1X1
011
000
98 if !ref( $self );
99  
100 20 my $format = $self->[ IDX_FORMAT ];
101 20 ▶ if ( !$format ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 20 0 20
102 20 my $decimals = $self->[ IDX_DECIMALS ];
103 20 my $sep = $self->[ IDX_SEP ];
104  
105 20 ▶ $sep = "%%" if $sep eq "%";
Branch: 1 branch
BranchTrueFalseTotal
if $sep eq "%" 0 20 20
106  
107 20 ▶ if ( defined( $decimals ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decimals) { } 10 10 20
108 10 ▶ if ( $decimals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimals) { } 9 1 10
109 9 $self->[ IDX_FORMAT ] = $format = "%Y-%m-%d${sep}%H:%M:%S.%${decimals}N";
110 } else {
111 1 $self->[ IDX_FORMAT ] = $format = "%Y-%m-%d${sep}%H:%M:%S";
112 }
113 } else {
114 10 ▶ if ( $dt->nanosecond() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->nanosecond) { } 1 9 10
115 1 $format = "%Y-%m-%d${sep}%H:%M:%S.%9N";
116 } else {
117 9 $format = "%Y-%m-%d${sep}%H:%M:%S";
118 }
119 }
120 }
121  
122 20 my $tz;
123 20 ▶ if ( $dt->time_zone()->is_utc() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->time_zone->is_utc) { } 16 4 20
124 16 $tz = 'Z';
125 } else {
126 4 my $secs = $dt->offset();
127  
128 # TODO Maybe we could cache this.
129 # There are only so many offests, and most
130 # programs probably only uses one or two.
131 4 ▶ my $sign = $secs < 0 ? '-' : '+'; $secs = abs( $secs );
Branch: 1 branch
BranchTrueFalseTotal
$secs < 0 ? : 2 2 4
132 4 my $mins = int( $secs / 60 ); $secs %= 60;
133 4 my $hours = int( $mins / 60 ); $mins %= 60;
134 4 ▶ if ( $secs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secs) { } 1 3 4
135 1 ( $dt = $dt->clone() )
136 ->set_time_zone( 'UTC' );
137 1 $tz = 'Z';
138 } else {
139 3 $tz = sprintf( '%s%02d:%02d', $sign, $hours, $mins );
140 }
141 }
142  
143 20 return $dt->strftime( $format ) . $tz;
144 }
145  
146  
147 3 1;
148  
149  
150 __END__
↑ Summary