blib/lib/Astro/App/Satpass2/ParseTime.pm

filter: Statementstmt 74.8% 110 / 147 Branchbran 47.1% 33 / 70 Conditioncond 27.2% 3 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 68.1% 15 / 22 Podpod 100% 11 / 11 totaltotal 64.5% 173 / 268 CC 44 SCAR 25.8
CC
44
Cov
64%
config
38.6
__ANON__
20.0
decode
20.0
CRAP 13.3
« eg/My/Macros.pm ↑ Summary blib/lib/Astro/App/Satpass2/ParseTime/ISO8601.pm »
1 package Astro::App::Satpass2::ParseTime;
2  
3 20 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use parent qw{ Astro::App::Satpass2::Copier };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use Astro::App::Satpass2::FormatTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Astro::App::Satpass2::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 load_package
13 ARRAY_REF CODE_REF SCALAR_REF
14 @CARP_NOT
15 20 };
16 20 ▶use Astro::Coord::ECI::Utils 0.059 qw{ looks_like_number };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 20 our $VERSION = '0.059';
19  
20 20 my %static = (
21 perltime => 0,
22 );
23  
24 15 ▶sub new {
Subroutine new: called CC=8 SCAR=21.6
Pod documented
25 15 my ( $class, %args ) = @_;
26 15 ▶ ref $class and $class = ref $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 15 15
27  
28 # Workaround for bug (well, _I_ think it's a bug) introduced into
29 # Date::Manip with 6.34, while fixing RT #78566. My bug report is RT
30 # #80435.
31 15 my $path = $ENV{PATH};
32 15 local $ENV{PATH} = $path;
33  
34 15 ▶ if ( __PACKAGE__ eq $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__ eq $class) { } 15 0 15
35  
36 15 ▶ $args{class} ||= [ qw{ Date::Manip ISO8601 } ];
Condition100%2/2
$args{"class"} ||= ["Date::Manip", "ISO8601"]
l!l
78
MC/DC100%1/1
$args{"class"} ||= ["Date::Manip", "ISO8601"]
$args{"class"}
Truth table100%2/2
A
A= $args{"class"}
Aresult
11
00
37  
38 8 my @classes = ARRAY_REF eq ref $args{class} ? @{ $args{class} } :
39 15 ▶ split qr{ \s* , \s* }smx, $args{class};
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $args{'class'} ? : 8 7 15
40  
41 15 ▶ $class = _try ( @classes )
Branch: 1 branch
BranchTrueFalseTotal
$class = _try(@classes) or return 15 0 15
42 or return;
43  
44 } else {
45 0 ▶ $class = _try( $class )
Branch: 1 branch
BranchTrueFalseTotal
$class = _try($class) or return 0 0 0
46 or return;
47 }
48 15 delete $args{class};
49  
50 defined $args{base}
51 15 ▶ or $args{base} = time;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"base"} or $args{"base"} = time 0 15 15
52  
53 15 my $self = { %static };
54 15 bless $self, $class;
55 15 $self->warner( delete $args{warner} );
56 15 $self->base( delete $args{base} );
57 15 $self->init( %args );
58 15 return $self;
59 }
60  
61 35 ▶sub attribute_names {
Subroutine attribute_names: called CC=1 SCAR=0.0
Pod documented
62 35 my ( $self ) = @_;
63 35 return ( $self->SUPER::attribute_names(), qw{
64 base perltime tz } );
65 }
66  
67 452 ▶sub base {
Subroutine base: called CC=2 SCAR=6.9
Pod documented
68 452 my ( $self, @args ) = @_;
69 452 ▶ if ( @args > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 0) 127 325 452
70 127 $self->{base} = $self->{absolute} = $args[0];
71 127 return $self;
72 }
73 325 return $self->{base};
74 }
75  
76 0 ▶sub class_name_of_record {
Subroutine class_name_of_record: not called CC=3 SCAR=24.8
Pod documented
77 0 my ( $self ) = @_;
78 0 my $rslt = substr $self->__class_name(), 2 + length __PACKAGE__;
79 0 foreach my $attr ( qw{ tz } ) {
80 0 ▶ my $value = $self->$attr()
Branch: 1 branch
BranchTrueFalseTotal
my $value = $self->$attr or next 0 0 0
81 or next;
82 0 $rslt .= ",$attr=$value";
83 }
84 0 return $rslt;
85 }
86  
87 # For the use of class_name_of_record(). It exists so that
88 # Astro::App::Satpass2::ParseTime::Date::Manip can override it, so that
89 # we do not get the trailing '::v5' or '::v6';
90 0 ▶sub __class_name {
Subroutine __class_name: not called CC=1 SCAR=6.9
91 0 my ( $self ) = @_;
92 0 return ref $self;
93 }
94  
95 {
96  
97 20 my %skip = map { $_ => 1 } qw{ base warner };
98  
99 0 ▶ sub config {
Subroutine config: not called CC=7 SCAR=36.5
Pod documented
100 0 my ( $self, %args ) = @_;
101 0 my @data;
102  
103 0 foreach my $name ( $self->attribute_names() ) {
104 0 ▶ $skip{$name} and next;
Branch: 1 branch
BranchTrueFalseTotal
$skip{$name} and next 0 0 0
105 0 my $val = $self->$name();
106 20 ▶ no warnings qw{ uninitialized };
Subroutine BEGIN: called CC=1 SCAR=0.0
107 0 ▶ next if $args{changes} && $val eq $static{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"changes"} and $val eq $static{$name} 0 0 0
Condition0%0/3
$args{"changes"} and $val eq $static{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"changes"} and $val eq $static{$name}
$args{"changes"} $val eq $static{$name}
Truth table0%0/3
A and B
A= $args{"changes"}B= $val eq $static{$name}
ABresult
0X0
100
111
108 0 ▶ push @data, [ $name, $args{decode} ? $self->decode( $name )
Branch: 1 branch
BranchTrueFalseTotal
$args{'decode'} ? : 0 0 0
109 : $val ];
110 }
111  
112 0 ▶ return wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
113 }
114  
115 }
116  
117 20 ▶sub delegate { ## no critic (RequireFinalReturn)
Subroutine delegate: not called CC=1 SCAR=2.6
Pod documented
118 0 my ( $self ) = @_;
119 0 $self->weep( 'The delegate() method must be overridden' );
120 # Weep throws an exception, but there is no way to tell perlcritic
121 # this.
122 }
123  
124 {
125  
126 my %decoder = (
127 0 ▶ base => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
128 0 my ( $self, $method, @args ) = @_;
129 0 my $rslt = $self->$method( @args );
130 @args
131 0 ▶ and return $rslt;
Branch: 1 branch
BranchTrueFalseTotal
@args and return $rslt 0 0 0
132 0 ▶ $rslt
Branch: 1 branch
BranchTrueFalseTotal
$rslt or return $rslt 0 0 0
133 or return $rslt;
134 $self->{_time_formatter} ||=
135 0 ▶ Astro::App::Satpass2::FormatTime->new();
Condition0%0/3
$self->{"_time_formatter"} ||= "Astro::App::Satpass2::FormatTime"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_time_formatter"} ||= "Astro::App::Satpass2::FormatTime"->new
$self->{"_time_formatter"} "Astro::App::Satpass2::FormatTime"->new
Truth table0%0/3
A ||= B
A= $self->{"_time_formatter"}B= "Astro::App::Satpass2::FormatTime"->new
ABresult
1X1
011
000
136 return $self->{_time_formatter}->format_datetime(
137 0 $self->{_time_formatter}->ISO_8601_FORMAT(),
138 $rslt, 1 );
139 },
140 20 );
141  
142 0 ▶ sub decode {
Subroutine decode: not called CC=4 SCAR=30.0
Pod documented
143 0 my ( $self, $method, @args ) = @_;
144 0 ▶ my $dcdr = $decoder{$method}
Branch: 1 branch
BranchTrueFalseTotal
my $dcdr = $decoder{$method} or return $self->$method(@args) 0 0 0
145 or return $self->$method( @args );
146 0 ▶ my $type = ref $dcdr
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $dcdr or $self->weep("Decoder for $method is scalar") 0 0 0
147 or $self->weep( "Decoder for $method is scalar" );
148 0 ▶ CODE_REF eq $type
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq $type or $self->weep("Decoder for $method is $type reference") 0 0 0
149 or $self->weep(
150 "Decoder for $method is $type reference" );
151 0 return $dcdr->( $self, $method, @args );
152 }
153 }
154  
155 {
156  
157 20 my @scale = ( 24, 60, 60, 1 );
158  
159 149 ▶ sub parse {
Subroutine parse: called CC=14 SCAR=27.6
Pod documented
160 149 my ( $self, $string, $default ) = @_;
161  
162 149 ▶ if ( SCALAR_REF eq ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("SCALAR" eq ref $string) 1 148 149
163 1 my $time = ${ $string };
164 1 $self->base( $self->{absolute} = $time );
165 1 return $time;
166 }
167  
168 148 ▶ if ( ! defined $string || '' eq $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $string or "" eq $string) 0 148 148
Condition33%1/3
not defined $string or "" eq $string
l!l&&r!l&&!r
00148
MC/DC0%0/2
not defined $string or "" eq $string
not defined $string "" eq $string
Truth table33%1/3
not(A) or B
A= not defined $stringB= "" eq $string
ABresult
1X1
011
000
169 defined $default
170 0 ▶ and $self->base( $self->{absolute} = $default );
Branch: 1 branch
BranchTrueFalseTotal
defined $default and $self->base($self->{"absolute"} = $default) 0 0 0
171 0 return $default;
172 }
173  
174 148 ▶ if ( $string =~ m/ \A \s* [+-] /smx ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ / \A \s* [+-] /msx) { } 39 109 148
elsif ($string =~ / \A epoch \s* ( [0-9]+ (?: [.] [0-9]* )? ) \z /msx) { } 1 108 109
175 39 ▶ defined $self->{base} or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"base"} or return 39 0 39
176 defined $self->{absolute}
177 39 ▶ or $self->{absolute} = $self->base();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"absolute"} or $self->{"absolute"} = $self->base 39 0 39
178 39 $string =~ s/ \A \s+ //smx;
179 39 $string =~ s/ \s+ \z //smx;
180 39 my $sign = substr $string, 0, 1;
181 39 substr( $string, 0, 1, '' );
182 39 my @delta = split qr{ \s* : \s* | \s+ }smx, $string;
183 39 ▶ @delta > 4 and return;
Branch: 1 branch
BranchTrueFalseTotal
@delta > 4 and return 0 39 39
184 39 push @delta, ( 0 ) x ( 4 - @delta );
185 39 my $dt = 0;
186 39 foreach my $inx ( 0 .. 3 ) {
187 156 ▶ looks_like_number( $delta[$inx] ) or return;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($delta[$inx]) or return 156 0 156
188 156 $dt += $delta[$inx];
189 156 $dt *= $scale[$inx];
190 }
191 39 ▶ '-' eq $sign and $dt = - $dt;
Branch: 1 branch
BranchTrueFalseTotal
"-" eq $sign and $dt = -$dt 4 35 39
192 39 return ( $self->{absolute} = $dt + $self->{absolute} );
193  
194 } elsif ( $string =~
195 m/ \A epoch \s* ( [0-9]+ (?: [.] [0-9]* )? ) \z /smx ) {
196  
197 1 my $time = $1 + 0;
198 1 $self->base( $self->{absolute} = $time );
199 1 return $time;
200  
201 } else {
202  
203 108 ▶ defined( my $time = $self->parse_time_absolute( $string ) )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $time = $self->parse_time_absolute($string)) or return 108 0 108
204 or return;
205 108 $self->base( $self->{absolute} = $time );
206 108 return $time;
207  
208 }
209  
210 }
211  
212 }
213  
214 20 ▶sub parse_time_absolute { ## no critic (RequireFinalReturn)
Subroutine parse_time_absolute: not called CC=1 SCAR=2.6
Pod documented
215 ## my ( $self, $string ) = @_;
216 0 my ( $self ) = @_; # $string unused
217 0 $self->weep(
218 'parse_time_absolute() must be overridden' );
219 # Weep throws an exception, but there is no way to tell perlcritic
220 # this.
221 }
222  
223 325 ▶sub reset : method { ## no critic (ProhibitBuiltinHomonyms)
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
224 325 my ( $self ) = @_;
225 325 $self->{absolute} = $self->base();
226 325 return $self;
227 }
228  
229 1 ▶sub use_perltime {
Subroutine use_perltime: called CC=1 SCAR=0.0
Pod documented
230 1 return 0;
231 }
232  
233 {
234  
235 # %trial is indexed by class name. The value is the class to
236 # delegate to (which can be the same as the class itself), or undef
237 # if the class can not be loaded, or has no delegate.
238  
239 20 my %trial;
240  
241 15 ▶ sub _try {
Subroutine _try: called CC=9 SCAR=22.5
242 15 my ( @args ) = @_;
243  
244 15 my @flatten;
245  
246 15 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 31 0 31
247  
248 31 my $try = shift @args;
249  
250 31 ▶ $trial{$try} and return $trial{$try};
Branch: 1 branch
BranchTrueFalseTotal
$trial{$try} and return $trial{$try} 4 27 31
251  
252 27 ▶ exists $trial{$try} and next;
Branch: 1 branch
BranchTrueFalseTotal
exists $trial{$try} and next 2 25 27
253  
254 25 ▶ $try =~ m/ \A \w+ (?: :: \w+ )* \z /smx or do {
Branch: 1 branch
BranchTrueFalseTotal
$try =~ / \A \w+ (?: :: \w+ )* \z /msx or do { $trial{$try} = undef; next } 25 0 25
255 0 $trial{$try} = undef;
256 0 next;
257 };
258  
259 25 ▶ my $pkg = $trial{$try} = load_package(
Branch: 1 branch
BranchTrueFalseTotal
my $pkg = $trial{$try} = load_package($try, "Astro::App::Satpass2::ParseTime") or next 25 0 25
260 $try, 'Astro::App::Satpass2::ParseTime' )
261 or next;
262  
263 25 ▶ my $delegate = $trial{$try} = eval { $pkg->delegate() }
Branch: 1 branch
BranchTrueFalseTotal
my $delegate = $trial{$try} = eval { do { $pkg->delegate } } or next 17 8 25
264 or next;
265  
266 17 ▶ if ( $trial{$delegate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trial{$delegate}) 11 6 17
267 11 foreach ( @flatten ) {
268 6 $trial{$_} = $delegate;
269 }
270 11 return $delegate;
271 }
272  
273 6 push @flatten, $try;
274 6 unshift @args, $delegate;
275 }
276  
277 0 return;
278 }
279 }
280  
281 20 __PACKAGE__->create_attribute_methods();
282  
283 20 1;
284  
285 __END__
« eg/My/Macros.pm ↑ Summary blib/lib/Astro/App/Satpass2/ParseTime/ISO8601.pm »