lib/Date/Manip/Delta.pm

filter: Statementstmt 88.7% 495 / 558 Branchbran 81.3% 231 / 284 Conditioncond 75.2% 70 / 93 MC/DCmcdc 57.6% 30 / 52 Subroutinesub 96.8% 31 / 32 Podpod 100% 11 / 11 totaltotal 84.2% 868 / 1030 CC 186 SCAR 34.3
CC
186
Cov
85%
parse
78.7
set
42.1
printf
25.1
CRAP 31.0
« lib/Date/Manip/DM6.pm ↑ Summary lib/Date/Manip/Base.pm »
1 package Date::Manip::Delta;
2 # Copyright (c) 1995-2026 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5  
6 ########################################################################
7 # Any routine that starts with an underscore (_) is NOT intended for
8 # public use. They are for internal use in the the Date::Manip
9 # modules and are subject to change without warning or notice.
10 #
11 # ABSOLUTELY NO USER SUPPORT IS OFFERED FOR THESE ROUTINES!
12 ########################################################################
13  
14 170 ▶use Date::Manip::Obj;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 170 @ISA = ('Date::Manip::Obj');
16  
17 170 require 5.010000;
18 170 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 170 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 170 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 170 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 170 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 #use re 'debug';
24  
25 170 ▶use Date::Manip::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 170 ▶use Date::Manip::TZ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 170 our $VERSION;
29 170 $VERSION='7.00';
30 170 ▶END { undef $VERSION; }
Subroutine END: called CC=1 SCAR=0.0
31  
32 ########################################################################
33 # BASE METHODS
34 ########################################################################
35  
36 1 ▶sub is_delta {
Subroutine is_delta: called CC=1 SCAR=0.0
Pod documented
37 1 return 1;
38 }
39  
40 15 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
41 15 my($self,@args) = @_;
42 15 $self->SUPER::config(@args);
43  
44 # A new config can change the value of the format fields, so clear them.
45 15 $$self{'data'}{'f'} = {};
46 15 $$self{'data'}{'flen'} = {};
47 }
48  
49 # Call this every time a new delta is put in to make sure everything is
50 # correctly initialized.
51 #
52 12028 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
53 12028 my($self) = @_;
54  
55 12028 my $def = [0,0,0,0,0,0,0];
56 12028 my $dmt = $$self{'tz'};
57 12028 my $dmb = $$dmt{'base'};
58  
59 12028 $$self{'err'} = '';
60 12028 $$self{'data'} =
61 {
62 'delta' => $def, # the delta put in (all negative fields signed)
63  
64 'in' => '', # the string that was parsed (if any)
65 'length' => 0, # length of delta (in seconds)
66  
67 'gotmode' => 0, # 1 if mode set explicitly
68 'mode' => 'standard', # standard/business
69 'type' => 'exact', # exact, semi, estimated, approx
70 'type_from' => 'init', # where did the type come from
71 # init - from here
72 # opt - specified in an option/string
73 # det - determined automatically
74 'normalized' => 1, # 1 if normalized
75  
76 'f' => {}, # format fields
77 'flen' => {}, # field lengths
78 }
79 }
80  
81 2 ▶sub _init_args {
Subroutine _init_args: called CC=1 SCAR=0.0
82 2 my($self) = @_;
83  
84 2 my @args = @{ $$self{'args'} };
85 2 $self->parse(@args);
86 }
87  
88 3422 ▶sub value {
Subroutine value: called CC=4 SCAR=14.6
Pod documented
89 3422 my($self,$as_input) = @_;
90  
91 3422 ▶ if ($$self{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err"}) 0 3422 3422
92 0 ▶ return () if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
93 0 return '';
94 }
95  
96 3422 my $dmt = $$self{'tz'};
97 3422 my $dmb = $$dmt{'base'};
98  
99 3422 my @delta = @{ $$self{'data'}{'delta'} };
100  
101 3422 ▶ return @delta if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3083 339 3422
102 339 my $err;
103  
104 my %o = ( 'source' => 'delta',
105 'nonorm' => 1,
106 'type' => $$self{'data'}{'type'},
107 'sign' => 0,
108 339 'mode' => $$self{'data'}{'mode'},
109 );
110  
111 339 ($err,@delta) = $dmb->_delta_fields( \%o, [@delta]);
112 339 return join(':',@delta);
113 }
114  
115 0 ▶sub input {
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
116 0 my($self) = @_;
117 0 return $$self{'data'}{'in'};
118 }
119  
120 ########################################################################
121 # DELTA METHODS
122 ########################################################################
123  
124 170 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
125 170 my %f = qw( y 0 M 1 w 2 d 3 h 4 m 5 s 6 );
126  
127 4988 ▶ sub set {
Subroutine set: called CC=42 SCAR=37.4
Pod documented
128 4988 my($self,@args) = @_;
129 4988 my %opts;
130 4988 ▶ if (ref($args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 120 4868 4988
131 120 %opts = %{ $args[0] };
132 } else {
133 # *** DEPRECATED 7.0 ***
134 4868 ▶ if (@args == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 3) { } 15 4853 4868
135 15 ▶ %opts = ( $args[0] => $args[1],
Branch: 1 branch
BranchTrueFalseTotal
$args[2] ? : 15 0 15
136 'nonorm' => ($args[2] ? 1 : 0) );
137 } else {
138 4853 %opts = ( $args[0] => $args[1] );
139 }
140 }
141  
142 # Check for some invalid opts
143  
144 4988 foreach my $key (keys %opts) {
145 5164 my $val = $opts{$key};
146 5164 delete $opts{$key};
147  
148 # *** DEPRECATED 7.0 ***
149 5164 ▶ $key = 'standard' if (lc($key) eq 'normal');
Branch: 1 branch
BranchTrueFalseTotal
if lc $key eq "normal" 0 5164 5164
150  
151 5164 ▶ if (lc($key) eq 'delta' ||
Branch: 2 branches
BranchTrueFalseTotal
if (lc $key eq 'delta' or lc $key eq 'business' or lc $key eq 'standard' or lc $key eq 'nonorm' or lc $key eq 'mode' or lc $key eq 'type') { } 5154 10 5164
elsif ($key =~ /^[yMwdhms]$/) { } 9 1 10
Condition100%15/15
lc $key eq "delta" or lc $key eq "business" or lc $key eq "standard" or lc $key eq "nonorm" or lc $key eq "mode" or lc $key eq "type"
l!l&&r!l&&!r
50599510
lc $key eq "delta" or lc $key eq "business" or lc $key eq "standard" or lc $key eq "nonorm" or lc $key eq "mode"
l!l&&r!l&&!r
500158105
lc $key eq "delta" or lc $key eq "business" or lc $key eq "standard" or lc $key eq "nonorm"
l!l&&r!l&&!r
494457163
lc $key eq "delta" or lc $key eq "business" or lc $key eq "standard"
l!l&&r!l&&!r
493410220
lc $key eq "delta" or lc $key eq "business"
l!l&&r!l&&!r
4768166230
MC/DC100%6/6
lc $key eq "delta" or lc $key eq "business" or lc $key eq "standard" or lc $key eq "nonorm" or lc $key eq "mode" or lc $key eq "type"
lc $key eq "delta" lc $key eq "business" lc $key eq "standard" lc $key eq "nonorm" lc $key eq "mode" lc $key eq "type"
Truth table100%7/7
A or B or C or D or E or F
A= lc $key eq "delta"B= lc $key eq "business"C= lc $key eq "standard"D= lc $key eq "nonorm"E= lc $key eq "mode"F= lc $key eq "type"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
152 lc($key) eq 'business' ||
153 lc($key) eq 'standard' ||
154 lc($key) eq 'nonorm' ||
155 lc($key) eq 'mode' ||
156 lc($key) eq 'type') {
157  
158 5154 ▶ if (exists $opts{lc($key)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{lc $key}) 1 5153 5154
159 1 $key = lc($key);
160 1 $$self{'err'} = "[set] Invalid option: $key entered twice";
161 1 return 1;
162 }
163  
164 5153 $opts{lc($key)} = $val;
165  
166 } elsif ($key =~ /^[yMwdhms]$/) {
167  
168 9 $opts{$key} = $val;
169  
170 } else {
171 1 $$self{'err'} = "[set] Unknown option: $key";
172 1 return 1;
173 }
174 }
175  
176 4986 ▶ if ( (exists $opts{'delta'}) +
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"delta"} + exists $opts{"business"} + exists $opts{"standard"} + (exists $opts{"y"} || exists $opts{"M"} || exists $opts{"w"} || exists $opts{"d"} || exists $opts{"h"} || exists $opts{"m"} || exists $opts{"s"}) > 1) 2 4984 4986
Condition77%14/18
exists $opts{'y'} || exists $opts{'M'} || exists $opts{'w'} || exists $opts{'d'} || exists $opts{'h'} || exists $opts{'m'} || exists $opts{'s'}
l!l&&r!l&&!r
904977
exists $opts{"y"} or exists $opts{"M"} or exists $opts{"w"} or exists $opts{"d"} or exists $opts{"h"} or exists $opts{"m"}
l!l&&r!l&&!r
544977
exists $opts{"y"} or exists $opts{"M"} or exists $opts{"w"} or exists $opts{"d"} or exists $opts{"h"}
l!l&&r!l&&!r
504981
exists $opts{"y"} or exists $opts{"M"} or exists $opts{"w"} or exists $opts{"d"}
l!l&&r!l&&!r
504981
exists $opts{"y"} or exists $opts{"M"} or exists $opts{"w"}
l!l&&r!l&&!r
504981
exists $opts{"y"} or exists $opts{"M"}
l!l&&r!l&&!r
324981
MC/DC42%3/7
exists $opts{'y'} || exists $opts{'M'} || exists $opts{'w'} || exists $opts{'d'} || exists $opts{'h'} || exists $opts{'m'} || exists $opts{'s'}
exists $opts{"y"} exists $opts{"M"} exists $opts{"w"} exists $opts{"d"} exists $opts{"h"} exists $opts{"m"} exists $opts{'s'}
Truth table50%4/8
A or B or C or D or E or F || G
A= exists $opts{"y"}B= exists $opts{"M"}C= exists $opts{"w"}D= exists $opts{"d"}E= exists $opts{"h"}F= exists $opts{"m"}G= exists $opts{'s'}
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
177 (exists $opts{'business'}) +
178 (exists $opts{'standard'}) +
179 (exists $opts{'y'} || exists $opts{'M'} || exists $opts{'w'} ||
180 exists $opts{'d'} || exists $opts{'h'} || exists $opts{'m'} ||
181 exists $opts{'s'})
182 > 1 ) {
183 2 $$self{'err'} = "[set] Fields set multiple times";
184 2 return 1;
185 }
186  
187 4984 ▶ if (exists $opts{'mode'} && $opts{'mode'} !~ /^(business|standard)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"mode"} and not $opts{"mode"} =~ /^(business|standard)$/) 1 4983 4984
Condition100%3/3
exists $opts{"mode"} and not $opts{"mode"} =~ /^(business|standard)$/
!ll&&!rl&&r
4926571
MC/DC100%2/2
exists $opts{"mode"} and not $opts{"mode"} =~ /^(business|standard)$/
exists $opts{"mode"} not $opts{"mode"} =~ /^(business|standard)$/
Truth table100%3/3
A and not(B)
A= exists $opts{"mode"}B= not $opts{"mode"} =~ /^(business|standard)$/
ABresult
0X0
100
111
188 1 $$self{'err'} = "[set] Unknown value for mode: $opts{mode}";
189 1 return 1;
190 }
191 4983 ▶ if (exists $opts{'type'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"type"} and not $opts{"type"} =~ /^(exact|semi|estimated|approx)$/) 1 4982 4983
Condition100%3/3
exists $opts{"type"} and not $opts{"type"} =~ /^(exact|semi|estimated|approx)$/
!ll&&!rl&&r
4888941
MC/DC100%2/2
exists $opts{"type"} and not $opts{"type"} =~ /^(exact|semi|estimated|approx)$/
exists $opts{"type"} not $opts{"type"} =~ /^(exact|semi|estimated|approx)$/
Truth table100%3/3
A and not(B)
A= exists $opts{"type"}B= not $opts{"type"} =~ /^(exact|semi|estimated|approx)$/
ABresult
0X0
100
111
192 $opts{'type'} !~ /^(exact|semi|estimated|approx)$/) {
193 1 $$self{'err'} = "[set] Unknown value for type: $opts{type}";
194 1 return 1;
195 }
196  
197 4982 ▶ if ( (exists $opts{'business'}) +
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opts{'business'} + exists $opts{'standard'} + exists $opts{'mode'} > 1) { } 1 4981 4982
elsif (exists $opts{'business'}) { } 166 4815 4981
elsif (exists $opts{'standard'}) { } 8 4807 4815
198 (exists $opts{'standard'}) +
199 (exists $opts{'mode'})
200 > 1 ) {
201 1 $$self{'err'} = "[set] Mode set multiple times";
202 1 return 1;
203 } elsif (exists $opts{'business'}) {
204 166 $opts{'delta'} = $opts{'business'};
205 166 $opts{'mode'} = 'business';
206 } elsif (exists $opts{'standard'}) {
207 8 $opts{'delta'} = $opts{'standard'};
208 8 $opts{'mode'} = 'standard';
209 }
210  
211 # If we are setting delta/business/standard, we need to initialize
212 # all the parameters.
213  
214 4981 my @delta;
215 4981 ▶ if (exists $opts{'delta'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'delta'}) { } 4938 43 4981
216 4938 ▶ if (ref($opts{'delta'}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"delta"} ne "ARRAY") 1 4937 4938
217 1 $$self{'err'} = "[set] Option delta requires an array value";
218 1 return 1;
219 }
220  
221 # Init everything because we're setting an entire new delta
222 4937 $self->_init();
223 4937 @delta = @{ $opts{'delta'} };
224  
225 } else {
226 43 @delta = @{ $$self{'data'}{'delta'} };
227 }
228  
229 # Figure out the parameters. Include the nonorm/mode/type
230 # options.
231  
232 4980 my $err;
233 4980 my $dmt = $$self{'tz'};
234 4980 my $dmb = $$dmt{'base'};
235 4980 ▶ my $gotmode = (exists $opts{'mode'} ? 1 : $$self{'data'}{'gotmode'});
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'mode'} ? : 229 4751 4980
236 my $mode = (exists $opts{'mode'} ? $opts{'mode'} :
237 4980 ▶ $$self{'data'}{'mode'});
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'mode'} ? : 229 4751 4980
238 4980 ▶ my $nonorm = (exists $opts{'nonorm'} ? $opts{'nonorm'} : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 57 4923 4980
239  
240 4980 my ($type,$type_from);
241 4980 ▶ if (exists $opts{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'type'}) { } 94 4886 4980
242 94 $type = $opts{'type'};
243 94 $type_from = 'opt';
244 } else {
245 4886 $type = $$self{'data'}{'type'};
246 4886 $type_from = $$self{'data'}{'type_from'};
247 }
248  
249 # If we're setting individual fields, do that now
250  
251 {
252 4980 my $field_set = 0;
253  
254 # Check all individual fields
255 4980 foreach my $opt (qw(y M w d h m s)) {
256 34848 ▶ if (exists $opts{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$opt}) 8 34840 34848
257 8 ▶ if (ref($opts{$opt})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{$opt}) 1 7 8
258 1 $$self{'err'} = "[set] Option $opt requires a scalar value";
259 1 return 1;
260 }
261 7 my $val = $opts{$opt};
262 7 ▶ if (! $dmb->_is_num($val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dmb->_is_num($val)) 1 6 7
263 1 $$self{'err'} = "[set] Option $opt requires a numerical value";
264 1 return 1;
265 }
266 6 $delta[ $f{$opt} ] = $val;
267 6 $field_set = 1;
268 }
269 }
270  
271 # If none were set, than we're done with setting.
272 4978 ▶ last if (! $field_set);
Branch: 1 branch
BranchTrueFalseTotal
unless $field_set 4972 6 4978
273  
274 6 ▶ if ($$self{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err"}) 0 6 6
275 0 return 1;
276 }
277 }
278  
279 # Check that the type is consistent with @delta.
280  
281 4978 ($err,$type,$type_from) =
282 $dmb->_check_delta_type($mode,$type,$type_from,@delta);
283  
284 4978 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 11 4967 4978
285 11 $$self{'err'} = "[set] $err";
286 11 return 1;
287 }
288  
289 4967 my %o = ( 'source' => 'delta',
290 'nonorm' => $nonorm,
291 'type' => $type,
292 'sign' => -1,
293 'mode' => $mode,
294 );
295  
296 4967 ($err,@delta) = $dmb->_delta_fields( \%o, [@delta]);
297  
298 4967 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 2 4965 4967
299 2 $$self{'err'} = "[set] $err";
300 2 return 1;
301 }
302  
303 4965 $$self{'data'}{'delta'} = [ @delta ];
304 4965 $$self{'data'}{'mode'} = $mode;
305 4965 $$self{'data'}{'gotmode'} = $gotmode;
306 4965 $$self{'data'}{'type'} = $type;
307 4965 $$self{'data'}{'type_from'} = $type_from;
308 4965 $$self{'data'}{'normalized'} = 1-$nonorm;
309 4965 $$self{'data'}{'length'} = 'unknown';
310 4965 $$self{'data'}{'in'} = '';
311  
312 4965 return 0;
313 }
314 }
315  
316 2101 ▶sub _rx {
Subroutine _rx: called CC=5 SCAR=16.1
317 2101 my($self,$rx) = @_;
318 2101 my $dmt = $$self{'tz'};
319 2101 my $dmb = $$dmt{'base'};
320  
321 return $$dmb{'data'}{'rx'}{'delta'}{$rx}
322 2101 ▶ if (exists $$dmb{'data'}{'rx'}{'delta'}{$rx});
Branch: 1 branch
BranchTrueFalseTotal
if exists $dmb->{"data"}{"rx"}{"delta"}{$rx} 2017 84 2101
323  
324 84 ▶ if ($rx eq 'expanded') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rx eq 'expanded') { } 26 58 84
elsif ($rx eq 'mode') { } 32 26 58
elsif ($rx eq 'when') { } 26 0 26
325 26 my $sign = '[-+]?\s*';
326 26 my $sep = '(?:,\s*|\s+|$)';
327  
328 26 my $nth = $$dmb{'data'}{'rx'}{'nth'}[0];
329 26 my $yf = $$dmb{data}{rx}{fields}[1];
330 26 my $mf = $$dmb{data}{rx}{fields}[2];
331 26 my $wf = $$dmb{data}{rx}{fields}[3];
332 26 my $df = $$dmb{data}{rx}{fields}[4];
333 26 my $hf = $$dmb{data}{rx}{fields}[5];
334 26 my $mnf = $$dmb{data}{rx}{fields}[6];
335 26 my $sf = $$dmb{data}{rx}{fields}[7];
336 26 my $num = '(?:\d+(?:\.\d*)?|\.\d+)';
337  
338 26 my $y = "(?:(?:(?<y>$sign$num)|(?<y>$nth))\\s*(?:$yf)$sep)";
339 26 my $m = "(?:(?:(?<m>$sign$num)|(?<m>$nth))\\s*(?:$mf)$sep)";
340 26 my $w = "(?:(?:(?<w>$sign$num)|(?<w>$nth))\\s*(?:$wf)$sep)";
341 26 my $d = "(?:(?:(?<d>$sign$num)|(?<d>$nth))\\s*(?:$df)$sep)";
342 26 my $h = "(?:(?:(?<h>$sign$num)|(?<h>$nth))\\s*(?:$hf)$sep)";
343 26 my $mn = "(?:(?:(?<mn>$sign$num)|(?<mn>$nth))\\s*(?:$mnf)$sep)";
344 26 my $s = "(?:(?:(?<s>$sign$num)|(?<s>$nth))\\s*(?:$sf)?)";
345  
346 26 my $exprx = qr/^\s*$y?$m?$w?$d?$h?$mn?$s?\s*$/i;
347 26 $$dmb{'data'}{'rx'}{'delta'}{$rx} = $exprx;
348  
349 } elsif ($rx eq 'mode') {
350  
351 32 my $mode = qr/\b($$dmb{'data'}{'rx'}{'mode'}[0])\b/i;
352 32 $$dmb{'data'}{'rx'}{'delta'}{$rx} = $mode;
353  
354 } elsif ($rx eq 'when') {
355  
356 26 my $when = qr/\b($$dmb{'data'}{'rx'}{'when'}[0])\b/i;
357 26 $$dmb{'data'}{'rx'}{'delta'}{$rx} = $when;
358  
359 }
360  
361 84 return $$dmb{'data'}{'rx'}{'delta'}{$rx};
362 }
363  
364 773 ▶sub parse {
Subroutine parse: called CC=40 SCAR=43.7
Pod documented
365 773 my($self,$instring,@args) = @_;
366 773 $self->_init();
367  
368 773 my %opts;
369 773 ▶ if (ref($args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 0 773 773
370 0 %opts = %{ $args[0] };
371  
372 } else {
373 # *** DEPRECATED 7.0 ***
374  
375 773 my($business,$no_normalize);
376  
377 773 ▶ if (@args == 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (@args == 2) { } 0 773 773
elsif (@args == 1) { } 1 772 773
elsif (@args) { } 0 772 772
378 0 ($business,$no_normalize) = (lc($args[0]),lc($args[1]));
379 0 ▶ if ($business eq 'standard' || ! $business) {
Branch: 1 branch
BranchTrueFalseTotal
if ($business eq 'standard' or not $business) { } 0 0 0
Condition0%0/3
$business eq "standard" or not $business
l!l&&r!l&&!r
000
MC/DC0%0/2
$business eq "standard" or not $business
$business eq "standard" not $business
Truth table0%0/3
A or not(B)
A= $business eq "standard"B= not $business
ABresult
1X1
011
000
380 0 $opts{'mode'} = 'standard';
381 } else {
382 0 $opts{'mode'} = 'business';
383 }
384  
385 0 ▶ $opts{'nonorm'} = ($no_normalize ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$no_normalize ? : 0 0 0
386  
387 } elsif (@args == 1) {
388 1 my $arg = lc($args[0]);
389 1 ▶ if ($arg eq 'standard') {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg eq 'standard') { } 0 1 1
elsif ($arg eq 'business') { } 1 0 1
elsif ($arg eq 'nonormalize') { } 0 0 0
elsif ($arg) { } 0 0 0
390 0 $opts{'mode'} = 'standard';
391 } elsif ($arg eq 'business') {
392 1 $opts{'mode'} = 'business';
393 } elsif ($arg eq 'nonormalize') {
394 0 $opts{'nonorm'} = 1;
395 } elsif ($arg) {
396 0 $opts{'mode'} = 'business';
397 } else {
398 0 $opts{'mode'} = 'standard';
399 }
400  
401 } elsif (@args) {
402 0 $$self{'err'} = "[parse] Unknown arguments";
403 0 return 1;
404 }
405 }
406  
407 773 my $dmt = $$self{'tz'};
408 773 my $dmb = $$dmt{'base'};
409 773 $self->_init();
410  
411 773 ▶ if (! $instring) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instring) 0 773 773
412 0 $$self{'err'} = '[parse] Empty delta string';
413 0 return 1;
414 }
415  
416 #
417 # Parse the string
418 # $err : any error
419 # @delta : the delta parsed
420 # $mode : the mode string (if any) in the string
421 #
422  
423 773 my ($err,@delta,$mode);
424 773 $mode = '';
425 773 $$self{'err'} = '';
426 773 $instring =~ s/^\s*//;
427 773 $instring =~ s/\s*$//;
428  
429 PARSE: {
430  
431 # First, we'll try the standard format (without a mode string)
432  
433 773 ($err,@delta) = $dmb->_split_delta($instring);
434 773 ▶ last PARSE if (! $err);
Branch: 1 branch
BranchTrueFalseTotal
unless $err 260 513 773
435  
436 # Next, we'll need to get a list of all the encodings and look
437 # for (and remove) the mode string from each. We'll also recheck
438 # the standard format for each.
439  
440 513 my @strings = $dmb->_encoding($instring);
441 513 my $moderx = $self->_rx('mode');
442  
443 513 foreach my $string (@strings) {
444 1010 ▶ if ($string =~ s/\s*$moderx\s*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/\s*$moderx\s*//i) 174 836 1010
445 174 my $m = $1;
446 174 ▶ if ($$dmb{'data'}{'wordmatch'}{'mode'}{lc($m)} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmb->{'data'}{'wordmatch'}{'mode'}{lc $m} == 1) { } 1 173 174
447 1 $m = 'standard';
448 } else {
449 173 $m = 'business';
450 }
451 174 $mode = $m;
452  
453 174 ($err,@delta) = $dmb->_split_delta($string);
454 174 ▶ last PARSE if (! $err);
Branch: 1 branch
BranchTrueFalseTotal
unless $err 89 85 174
455 }
456 }
457  
458 # Now we'll check each string for an expanded form delta.
459  
460 424 foreach my $string (@strings) {
461 794 my $past = 0;
462  
463 794 my $whenrx = $self->_rx('when');
464 794 ▶ if ($string &&
Branch: 1 branch
BranchTrueFalseTotal
if ($string and $string =~ s/$whenrx//i) 50 744 794
Condition66%2/3
$string and $string =~ s/$whenrx//i
!ll&&!rl&&r
074450
MC/DC50%1/2
$string and $string =~ s/$whenrx//i
$string $string =~ s/$whenrx//i
Truth table66%2/3
A and B
A= $stringB= $string =~ s/$whenrx//i
ABresult
0X0
100
111
465 $string =~ s/$whenrx//i) {
466 50 my $when = $1;
467 50 ▶ if ($$dmb{'data'}{'wordmatch'}{'when'}{lc($when)} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmb->{"data"}{"wordmatch"}{"when"}{lc $when} == 1) 16 34 50
468 16 $past = 1;
469 }
470 }
471  
472 794 my $rx = $self->_rx('expanded');
473 794 ▶ if ($string &&
Branch: 1 branch
BranchTrueFalseTotal
if ($string and $string =~ /$rx/) 114 680 794
Condition66%2/3
$string and $string =~ /$rx/
!ll&&!rl&&r
0680114
MC/DC50%1/2
$string and $string =~ /$rx/
$string $string =~ /$rx/
Truth table66%2/3
A and B
A= $stringB= $string =~ /$rx/
ABresult
0X0
100
111
474 $string =~ $rx) {
475 114 @delta = @+{qw(y m w d h mn s)};
476 114 foreach my $f (@delta) {
477 798 ▶ if (! defined $f) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $f) { } 660 138 798
elsif (exists $dmb->{'data'}{'wordmatch'}{'nth'}{lc $f}) { } 4 134 138
478 660 $f = 0;
479 } elsif (exists $$dmb{'data'}{'wordmatch'}{'nth'}{lc($f)}) {
480 4 $f = $$dmb{'data'}{'wordmatch'}{'nth'}{lc($f)};
481 } else {
482 134 $f =~ s/\s//g;
483 }
484 }
485  
486 # if $past, reverse the signs
487 114 ▶ if ($past) {
Branch: 1 branch
BranchTrueFalseTotal
if ($past) 12 102 114
488 12 foreach my $v (@delta) {
489 84 $v *= -1;
490 }
491 }
492  
493 114 last PARSE;
494 }
495 }
496 }
497  
498 773 ▶ if (! @delta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@delta) 310 463 773
499 310 $$self{'err'} = "[parse] Invalid delta string";
500 310 return 1;
501 }
502  
503 # If the string contains a mode string and the mode was passed in
504 # as an option, they must be identical.
505  
506 463 ▶ if ($mode && exists($opts{'mode'}) && $mode ne $opts{'mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode and exists $opts{"mode"} and $mode ne $opts{"mode"}) 0 463 463
Condition50%3/6
$mode and exists $opts{"mode"} and $mode ne $opts{"mode"}
!ll&&!rl&&r
46300
$mode and exists $opts{"mode"}
!ll&&!rl&&r
3331300
MC/DC0%0/3
$mode and exists $opts{"mode"} and $mode ne $opts{"mode"}
$mode exists $opts{"mode"} $mode ne $opts{"mode"}
Truth table50%2/4
A and B and C
A= $modeB= exists $opts{"mode"}C= $mode ne $opts{"mode"}
ABCresult
0XX0
10X0
1100
1111
507 0 $$self{'err'} =
508 "[parse] Mode option conflicts with mode specified in string";
509 0 return 1;
510 }
511 463 ▶ $mode = $opts{'mode'} if (exists $opts{'mode'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"mode"} 1 462 463
512 463 ▶ $mode = 'standard' if (! $mode);
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 332 131 463
513  
514 # Figure out the type.
515  
516 my %o = ( 'source' => 'string',
517 463 ▶ 'nonorm' => (exists $opts{'nonorm'} ? $opts{'nonorm'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'nonorm'} ? : 0 463 463
518 'sign' => -1,
519 'mode' => $mode,
520 );
521  
522 463 ($err,@delta) = $dmb->_delta_fields( \%o, [@delta]);
523 463 my $type = $o{'type'};
524 463 my $type_from = $o{'type_from'};
525  
526 463 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 463 463
527 0 $$self{'err'} = "[parse] $err";
528 0 return 1;
529 }
530  
531 463 $$self{'data'}{'in'} = $instring;
532 463 $$self{'data'}{'delta'} = [@delta];
533 463 $$self{'data'}{'mode'} = $mode;
534 463 ▶ $$self{'data'}{'gotmode'} = ($mode || exists $opts{'mode'} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$mode || exists $opts{'mode'} ? : 463 0 463
Condition33%1/3
$mode or exists $opts{"mode"}
l!l&&r!l&&!r
46300
MC/DC0%0/2
$mode or exists $opts{"mode"}
$mode exists $opts{"mode"}
Truth table33%1/3
A or B
A= $modeB= exists $opts{"mode"}
ABresult
1X1
011
000
535 463 $$self{'data'}{'type'} = $type;
536 463 $$self{'data'}{'type_from'} = $type_from;
537 463 $$self{'data'}{'length'} = 'unknown';
538 463 ▶ $$self{'data'}{'normalized'} = ($opts{'nonorm'} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'nonorm'} ? : 0 463 463
539  
540 463 return 0;
541 }
542  
543 2350 ▶sub printf {
Subroutine printf: called CC=22 SCAR=32.2
Pod documented
544 2350 my($self,@in) = @_;
545 2350 ▶ if ($$self{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err"}) 0 2350 2350
546 0 carp "WARNING: [printf] Object must contain a valid delta";
547 0 return undef;
548 }
549  
550 2350 my($y,$M,$w,$d,$h,$m,$s) = @{ $$self{'data'}{'delta'} };
551  
552 2350 my @out;
553 2350 foreach my $in (@in) {
554 2352 my $out = '';
555 2352 ▶ while ($in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in) 2812 2352 5164
556 2812 ▶ if ($in =~ s/^([^%]+)//) {
Branch: 6 branches
BranchTrueFalseTotal
if ($in =~ s/^([^%]+)//) { } 268 2544 2812
elsif ($in =~ s/^%%//) { } 1 2543 2544
elsif ($in =~ s/^% (\+)? # sign ([<>0])? # pad (\d+)? # width ([yMwdhms]) # field v # type //ox) { } 18 2525 2543
elsif ($in =~ s/^(% (\+)? # sign ([<>0])? # pad (\d+)? # width (?:\.(\d+))? # precision ([yMwdhms]) # field ([yMwdhms]) # field0 ([yMwdhms]) # field1 )//ox) { } 2503 22 2525
elsif ($in =~ s/^% (\+)? # sign ([<>])? # pad (\d+)? # width Dt //ox) { } 6 16 22
elsif ($in =~ s/^(% (\+)? # sign ([<>])? # pad (\d+)? # width D ([yMwdhms]) # field0 ([yMwdhms]) # field1 )//ox) { } 16 0 16
557 268 $out .= $1;
558  
559 } elsif ($in =~ s/^%%//) {
560 1 $out .= "%";
561  
562 } elsif ($in =~ s/^%
563 (\+)? # sign
564 ([<>0])? # pad
565 (\d+)? # width
566 ([yMwdhms]) # field
567 v # type
568 //ox) {
569 18 my($sign,$pad,$width,$field) = ($1,$2,$3,$4);
570 18 $out .= $self->_printf_field($sign,$pad,$width,0,$field);
571  
572 } elsif ($in =~ s/^(%
573 (\+)? # sign
574 ([<>0])? # pad
575 (\d+)? # width
576 (?:\.(\d+))? # precision
577 ([yMwdhms]) # field
578 ([yMwdhms]) # field0
579 ([yMwdhms]) # field1
580 )//ox) {
581 2503 my($match,$sign,$pad,$width,$precision,$field,$field0,$field1) =
582 ($1,$2,$3,$4,$5,$6,$7,$8);
583  
584 # Get the list of fields we're expressing
585  
586 2503 my @field = qw(y M w d h m s);
587 2503 ▶ while (@field && $field[0] ne $field0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field and $field[0] ne $field0) 338 2503 2841
Condition66%2/3
@field and $field[0] ne $field0
!ll&&!rl&&r
02503338
MC/DC50%1/2
@field and $field[0] ne $field0
@field $field[0] ne $field0
Truth table66%2/3
A and B
A= @fieldB= $field[0] ne $field0
ABresult
0X0
100
111
588 338 shift(@field);
589 }
590 2503 ▶ while (@field && $field[$#field] ne $field1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field and $field[$#field] ne $field1) 342 2503 2845
Condition66%2/3
@field and $field[$#field] ne $field1
!ll&&!rl&&r
02503342
MC/DC50%1/2
@field and $field[$#field] ne $field1
@field $field[$#field] ne $field1
Truth table66%2/3
A and B
A= @fieldB= $field[$#field] ne $field1
ABresult
0X0
100
111
591 342 pop(@field);
592 }
593  
594 2503 ▶ if (! @field) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @field) { } 0 2503 2503
595 0 $out .= $match;
596 } else {
597 2503 $out .=
598 $self->_printf_field($sign,$pad,$width,$precision,$field,@field);
599 }
600  
601 } elsif ($in =~ s/^%
602 (\+)? # sign
603 ([<>])? # pad
604 (\d+)? # width
605 Dt
606 //ox) {
607 6 my($sign,$pad,$width) = ($1,$2,$3);
608 6 $out .= $self->_printf_delta($sign,$pad,$width,'y','s');
609  
610 } elsif ($in =~ s/^(%
611 (\+)? # sign
612 ([<>])? # pad
613 (\d+)? # width
614 D
615 ([yMwdhms]) # field0
616 ([yMwdhms]) # field1
617 )//ox) {
618 16 my($match,$sign,$pad,$width,$field0,$field1) = ($1,$2,$3,$4,$5,$6);
619  
620 # Get the list of fields we're expressing
621  
622 16 my @field = qw(y M w d h m s);
623 16 ▶ while (@field && $field[0] ne $field0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field and $field[0] ne $field0) 7 16 23
Condition66%2/3
@field and $field[0] ne $field0
!ll&&!rl&&r
0167
MC/DC50%1/2
@field and $field[0] ne $field0
@field $field[0] ne $field0
Truth table66%2/3
A and B
A= @fieldB= $field[0] ne $field0
ABresult
0X0
100
111
624 7 shift(@field);
625 }
626 16 ▶ while (@field && $field[$#field] ne $field1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@field and $field[$#field] ne $field1) 59 16 75
Condition66%2/3
@field and $field[$#field] ne $field1
!ll&&!rl&&r
01659
MC/DC50%1/2
@field and $field[$#field] ne $field1
@field $field[$#field] ne $field1
Truth table66%2/3
A and B
A= @fieldB= $field[$#field] ne $field1
ABresult
0X0
100
111
627 59 pop(@field);
628 }
629  
630 16 ▶ if (! @field) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @field) { } 0 16 16
631 0 $out .= $match;
632 } else {
633 16 $out .= $self->_printf_delta($sign,$pad,$width,$field[0],
634 $field[$#field]);
635 }
636  
637 } else {
638 0 $in =~ s/^(%[^%]*)//;
639 0 $out .= $1;
640 }
641 }
642 2352 push(@out,$out);
643 }
644  
645 2350 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 58 2292 2350
elsif (@out == 1) { } 2292 0 2292
646 58 return @out;
647 } elsif (@out == 1) {
648 2292 return $out[0];
649 }
650  
651 0 return ''
652 }
653  
654 22 ▶sub _printf_delta {
Subroutine _printf_delta: called CC=24 SCAR=31.8
655 22 my($self,$sign,$pad,$width,$field0,$field1) = @_;
656 22 my $dmt = $$self{'tz'};
657 22 my $dmb = $$dmt{'base'};
658 22 my @delta = @{ $$self{'data'}{'delta'} };
659 22 my $delta;
660 22 my %tmp = qw(y 0 M 1 w 2 d 3 h 4 m 5 s 6);
661  
662 # Add a sign to each field
663  
664 22 my $s = "+";
665 22 foreach my $f (@delta) {
666 154 ▶ if ($f < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f < 0) { } 13 141 154
elsif ($f > 0) { } 116 25 141
667 13 $s = "-";
668 } elsif ($f > 0) {
669 116 $s = "+";
670 116 $f *= 1;
671 116 $f = "+$f";
672 } else {
673 25 $f = "$s$f";
674 }
675 }
676  
677 # Split the delta into field sets containing only those fields to
678 # print.
679 #
680 # @set = ( [SETa] [SETb] ....)
681 # where [SETx] is a listref of fields from one set of fields
682  
683 22 my @set;
684 22 my $mode = $$self{'data'}{'mode'};
685  
686 22 my $f0 = $tmp{$field0};
687 22 my $f1 = $tmp{$field1};
688  
689 22 ▶ if ($field0 eq $field1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field0 eq $field1) { } 3 19 22
elsif ($mode eq 'business') { } 4 15 19
690 3 @set = ( [ $delta[$f0] ] );
691  
692 } elsif ($mode eq 'business') {
693  
694 4 ▶ if ($f0 <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 <= 1) 3 1 4
695 # if (field0 = y or M)
696 # add [y,M]
697 # if field1 = M
698 # done
699 # else
700 # field0 = w
701 3 push(@set, [ @delta[$f0..1] ]);
702 3 ▶ $f0 = ($f1 == 1 ? 7 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$f1 == 1 ? : 1 2 3
703 }
704  
705 4 ▶ if ($f0 == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 == 2) 2 2 4
706 # if (field0 = w)
707 # add [w]
708 # if field1 = w
709 # done
710 # else
711 # field0 = d
712 2 push(@set, [ $delta[2] ]);
713 2 ▶ $f0 = ($f1 == 2 ? 7 : 3);
Branch: 1 branch
BranchTrueFalseTotal
$f1 == 2 ? : 0 2 2
714 }
715  
716 4 ▶ if ($f0 <= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 <= 6) 3 1 4
717 3 push(@set, [ @delta[$f0..$f1] ]);
718 }
719  
720 } else {
721  
722 15 ▶ if ($f0 <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 <= 1) 14 1 15
723 # if (field0 = y or M)
724 # add [y,M]
725 # if field1 = M
726 # done
727 # else
728 # field0 = w
729 14 push(@set, [ @delta[$f0..1] ]);
730 14 ▶ $f0 = ($f1 == 1 ? 7 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$f1 == 1 ? : 2 12 14
731 }
732  
733 15 ▶ if ($f0 <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 <= 3) 13 2 15
734 # if (field0 = w or d)
735 # if (field1 = w or d)
736 # add [w ... [f1]]
737 # done
738 # else
739 # add [w,d]
740 # field0 = h
741 13 ▶ if ($f1 <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 <= 3) { } 7 6 13
742 7 push(@set, [ @delta[$f0..$f1] ]);
743 7 $f0 = 7;
744 } else {
745 6 push(@set, [ @delta[$f0..3] ]);
746 6 $f0 = 4;
747 }
748 }
749  
750 15 ▶ if ($f0 <= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f0 <= 6) 6 9 15
751 6 push(@set, [ @delta[$f0..$f1] ]);
752 }
753 }
754  
755 # If we're not forcing signs, remove signs from all fields
756 # except the first in each set.
757  
758 22 my @ret;
759  
760 22 foreach my $set (@set) {
761 44 my @f = @$set;
762  
763 44 ▶ if (defined($sign) && $sign eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign and $sign eq '+') { } 16 28 44
Condition66%2/3
defined $sign and $sign eq "+"
!ll&&!rl&&r
28016
MC/DC50%1/2
defined $sign and $sign eq "+"
defined $sign $sign eq "+"
Truth table66%2/3
A and B
A= defined $signB= $sign eq "+"
ABresult
0X0
100
111
764 16 push(@ret,@f);
765 } else {
766 28 push(@ret,shift(@f));
767 28 foreach my $f (@f) {
768 26 $f =~ s/[-+]//;
769 26 push(@ret,$f);
770 }
771 }
772 }
773  
774 # Width/pad
775  
776 22 my $ret = join(':',@ret);
777 22 ▶ if ($width && length($ret) < $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width and length $ret < $width) 3 19 22
Condition100%3/3
$width and length $ret < $width
!ll&&!rl&&r
1813
MC/DC100%2/2
$width and length $ret < $width
$width length $ret < $width
Truth table100%3/3
A and B
A= $widthB= length $ret < $width
ABresult
0X0
100
111
778 3 ▶ if (defined $pad && $pad eq ">") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pad and $pad eq '>') { } 1 2 3
Condition100%3/3
defined $pad and $pad eq ">"
!ll&&!rl&&r
111
MC/DC100%2/2
defined $pad and $pad eq ">"
defined $pad $pad eq ">"
Truth table100%3/3
A and B
A= defined $padB= $pad eq ">"
ABresult
0X0
100
111
779 1 $ret .= ' 'x($width-length($ret));
780 } else {
781 2 $ret = ' 'x($width-length($ret)) . $ret;
782 }
783 }
784  
785 22 return $ret;
786 }
787  
788 2521 ▶sub _printf_field {
Subroutine _printf_field: called CC=13 SCAR=26.0
789 2521 my($self,$sign,$pad,$width,$precision,$field,@field) = @_;
790  
791 2521 my $val = $self->_printf_field_val($field,@field);
792 2521 ▶ $pad = "<" if (! defined($pad));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pad 2483 38 2521
793  
794 # Strip off the sign.
795  
796 2521 my $s = '';
797  
798 2521 ▶ if ($val < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val < 0) { } 66 2455 2521
elsif ($sign) { } 16 2439 2455
799 66 $s = "-";
800 66 $val *= -1;
801 } elsif ($sign) {
802 16 $s = "+";
803 }
804  
805 # Handle the precision.
806  
807 2521 ▶ if (defined($precision)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $precision) { } 222 2299 2521
elsif (defined $width) { } 0 2299 2299
808 222 $val = sprintf("%.${precision}f",$val);
809  
810 } elsif (defined($width)) {
811 0 my $i = $s . int($val) . '.';
812 0 ▶ if (length($i) < $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $i < $width) 0 0 0
813 0 $precision = $width-length($i);
814 0 $val = sprintf("%.${precision}f",$val);
815 }
816 }
817  
818 # Handle padding.
819  
820 2521 ▶ if ($width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width) { } 38 2483 2521
821 38 ▶ if ($pad eq ">") {
Branch: 2 branches
BranchTrueFalseTotal
if ($pad eq '>') { } 8 30 38
elsif ($pad eq '<') { } 15 15 30
822 8 $val = "$s$val";
823 8 ▶ my $pad = ($width > length($val) ? $width - length($val) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$width > length $val ? : 6 2 8
824 8 $val .= ' 'x$pad;
825  
826 } elsif ($pad eq "<") {
827 15 $val = "$s$val";
828 15 ▶ my $pad = ($width > length($val) ? $width - length($val) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$width > length $val ? : 10 5 15
829 15 $val = ' 'x$pad . $val;
830  
831 } else {
832 15 ▶ my $pad = ($width > length($val)-length($s) ?
Branch: 1 branch
BranchTrueFalseTotal
$width > length($val) - length($s) ? : 11 4 15
833 $width - length($val) - length($s): 0);
834 15 $val = $s . '0'x$pad . $val;
835 }
836 } else {
837 2483 $val = "$s$val";
838 }
839  
840 2521 return $val;
841 }
842  
843 # $$self{'data'}{'f'}{X}{Y} is the value of field X expressed in terms of Y.
844 #
845 2521 ▶sub _printf_field_val {
Subroutine _printf_field_val: called CC=8 SCAR=20.8
846 2521 my($self,$field,@field) = @_;
847  
848 2521 ▶ if (! exists $$self{'data'}{'f'}{'y'} &&
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{"f"}{"y"} or exists $self->{"data"}{"f"}{"y"}{"y"}) 1516 1005 2521
Condition66%2/3
exists $self->{"data"}{"f"}{"y"} or exists $self->{"data"}{"f"}{"y"}{"y"}
l!l&&r!l&&!r
100501516
MC/DC50%1/2
exists $self->{"data"}{"f"}{"y"} or exists $self->{"data"}{"f"}{"y"}{"y"}
exists $self->{"data"}{"f"}{"y"} exists $self->{"data"}{"f"}{"y"}{"y"}
Truth table66%2/3
A or B
A= exists $self->{"data"}{"f"}{"y"}B= exists $self->{"data"}{"f"}{"y"}{"y"}
ABresult
1X1
011
000
849 ! exists $$self{'data'}{'f'}{'y'}{'y'}) {
850  
851 1516 my($yv,$Mv,$wv,$dv,$hv,$mv,$sv) = map { $_*1 } @{ $$self{'data'}{'delta'} };
852 1516 $$self{'data'}{'f'}{'y'}{'y'} = $yv;
853 1516 $$self{'data'}{'f'}{'M'}{'M'} = $Mv;
854 1516 $$self{'data'}{'f'}{'w'}{'w'} = $wv;
855 1516 $$self{'data'}{'f'}{'d'}{'d'} = $dv;
856 1516 $$self{'data'}{'f'}{'h'}{'h'} = $hv;
857 1516 $$self{'data'}{'f'}{'m'}{'m'} = $mv;
858 1516 $$self{'data'}{'f'}{'s'}{'s'} = $sv;
859 }
860  
861 # A single field
862  
863 2521 ▶ if (! @field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@field) 18 2503 2521
864 18 return $$self{'data'}{'f'}{$field}{$field};
865 }
866  
867 # Find the length of 1 unit of each field in terms of seconds.
868  
869 2503 ▶ if (! exists $$self{'data'}{'flen'}{'s'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{"flen"}{"s"}) 1510 993 2503
870 1510 my $mode = $$self{'data'}{'mode'};
871 1510 my $dmb = $self->base();
872 $$self{'data'}{'flen'} = { 's' => 1,
873 'm' => 60,
874 'h' => 3600,
875 'd' => $$dmb{'data'}{'len'}{$mode}{'dl'},
876 'w' => $$dmb{'data'}{'len'}{$mode}{'wl'},
877 'M' => $$dmb{'data'}{'len'}{$mode}{'ml'},
878 1510 'y' => $$dmb{'data'}{'len'}{$mode}{'yl'},
879 };
880 }
881  
882 # Calculate the value for each field.
883  
884 2503 my $val = 0;
885 2503 foreach my $f (@field) {
886  
887 # We want the value of $f expressed in terms of $field
888  
889 16841 ▶ if (! exists $$self{'data'}{'f'}{$f}{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{"f"}{$f}{$field}) 9372 7469 16841
890  
891 # Get the value of $f expressed in seconds
892  
893 9372 ▶ if (! exists $$self{'data'}{'f'}{$f}{'s'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{"f"}{$f}{"s"}) 9033 339 9372
894 $$self{'data'}{'f'}{$f}{'s'} =
895 9033 $$self{'data'}{'f'}{$f}{$f} * $$self{'data'}{'flen'}{$f};
896 }
897  
898 # Get the value of $f expressed in terms of $field
899  
900 $$self{'data'}{'f'}{$f}{$field} =
901 9372 $$self{'data'}{'f'}{$f}{'s'} / $$self{'data'}{'flen'}{$field};
902 }
903  
904 16841 $val += $$self{'data'}{'f'}{$f}{$field};
905 }
906  
907 2503 return $val;
908 }
909  
910 54 ▶sub type {
Subroutine type: called CC=5 SCAR=16.1
Pod documented
911 54 my($self,$op) = @_;
912 54 $op = lc($op);
913  
914 54 ▶ if ($op eq 'business' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "business" or $op eq "standard") 33 21 54
Condition100%3/3
$op eq "business" or $op eq "standard"
l!l&&r!l&&!r
26721
MC/DC100%2/2
$op eq "business" or $op eq "standard"
$op eq "business" $op eq "standard"
Truth table100%3/3
A or B
A= $op eq "business"B= $op eq "standard"
ABresult
1X1
011
000
915 $op eq 'standard') {
916 33 ▶ return ($$self{'data'}{'mode'} eq $op ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'data'}{'mode'} eq $op ? : 16 17 33
917 }
918  
919 21 ▶ return ($$self{'data'}{'type'} eq $op ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'data'}{'type'} eq $op ? : 7 14 21
920 }
921  
922 29 ▶sub calc {
Subroutine calc: called CC=6 SCAR=27.4
Pod documented
923 29 my($self,$obj,@args) = @_;
924 29 ▶ if ($$self{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err"}) 0 29 29
925 0 $$self{'err'} = "[calc] First object invalid (delta)";
926 0 return undef;
927 }
928  
929 29 ▶ if (ref($obj) eq 'Date::Manip::Date') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $obj eq 'Date::Manip::Date') { } 0 29 29
elsif (ref $obj eq 'Date::Manip::Delta') { } 29 0 29
930 0 ▶ if ($$obj{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"err"}) 0 0 0
931 0 $$self{'err'} = "[calc] Second object invalid (date)";
932 0 return undef;
933 }
934 0 return $obj->calc($self,@args);
935  
936 } elsif (ref($obj) eq 'Date::Manip::Delta') {
937 29 ▶ if ($$obj{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"err"}) 0 29 29
938 0 $$self{'err'} = "[calc] Second object invalid (delta)";
939 0 return undef;
940 }
941 29 return $self->_calc_delta_delta($obj,@args);
942  
943 } else {
944 0 $$self{'err'} = "[calc] Second object must be a Date/Delta object";
945 0 return undef;
946 }
947 }
948  
949 29 ▶sub __type_max {
Subroutine __type_max: called CC=5 SCAR=16.3
950 29 my($type1,$type2) = @_;
951 29 ▶ return $type1 if ($type1 eq $type2);
Branch: 1 branch
BranchTrueFalseTotal
if $type1 eq $type2 27 2 29
952 2 foreach my $type ('estimate','approx','semi') {
953 6 ▶ return $type if ($type1 eq $type || $type2 eq $type);
Branch: 1 branch
BranchTrueFalseTotal
if $type1 eq $type or $type2 eq $type 2 4 6
Condition66%2/3
$type1 eq $type or $type2 eq $type
l!l&&r!l&&!r
204
MC/DC50%1/2
$type1 eq $type or $type2 eq $type
$type1 eq $type $type2 eq $type
Truth table66%2/3
A or B
A= $type1 eq $typeB= $type2 eq $type
ABresult
1X1
011
000
954 }
955 0 return 'exact';
956 }
957  
958 29 ▶sub _calc_delta_delta {
Subroutine _calc_delta_delta: called CC=8 SCAR=21.6
959 29 my($self,$delta,@args) = @_;
960 29 my $dmt = $$self{'tz'};
961 29 my $dmb = $$dmt{'base'};
962 29 my $ret = $self->new_delta;
963  
964 29 my($subtract,$no_normalize);
965 29 ▶ if (@args > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 2) 0 29 29
966 0 $$ret{'err'} = "Unknown args in calc";
967 0 return $ret;
968 }
969  
970 29 ▶ if (@args == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 2) { } 0 29 29
elsif (@args == 1) { } 4 25 29
971 0 ($subtract,$no_normalize) = @args;
972 } elsif (@args == 1) {
973 4 ▶ if ($args[0] eq 'nonormalize') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq 'nonormalize') { } 0 4 4
974 0 $subtract = 0;
975 0 $no_normalize = 1;
976 } else {
977 4 $subtract = $args[0];
978 4 $no_normalize = 0;
979 }
980 } else {
981 25 $subtract = 0;
982 25 $no_normalize = 0;
983 }
984  
985 29 ▶ if ($$self{'data'}{'mode'} ne $$delta{'data'}{'mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"mode"} ne $delta->{"data"}{"mode"}) 0 29 29
986 0 $$ret{'err'} = "[calc] Delta/delta calculation objects must be of " .
987 'the same mode';
988 0 return $ret;
989 }
990  
991 29 my ($err,@delta);
992 29 ▶ for (my $i=0; $i<7; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 7) 203 29 232
993 203 ▶ if ($subtract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtract) { } 28 175 203
994 28 $delta[$i] = $$self{'data'}{'delta'}[$i] - $$delta{'data'}{'delta'}[$i];
995 } else {
996 175 $delta[$i] = $$self{'data'}{'delta'}[$i] + $$delta{'data'}{'delta'}[$i];
997 }
998 }
999  
1000 my $type = __type_max($$self{'data'}{'type'},
1001 29 $$delta{'data'}{'type'});
1002 my %o = ( 'source' => 'delta',
1003 'nonorm' => $no_normalize,
1004 'sign' => -1,
1005 'type' => $type,
1006 29 'mode' => $$self{'data'}{'mode'},
1007 );
1008  
1009 29 ($err,@delta) = $dmb->_delta_fields( \%o, [@delta]);
1010  
1011 29 $$ret{'data'}{'in'} = '';
1012 29 $$ret{'data'}{'delta'} = [@delta];
1013 29 $$ret{'data'}{'mode'} = $$self{'data'}{'mode'};
1014 29 $$ret{'data'}{'gotmode'} = 1;
1015 29 $$ret{'data'}{'type'} = $type;
1016 29 $$ret{'data'}{'type_from'} = 'det';
1017 29 $$ret{'data'}{'length'} = 'unknown';
1018 29 $$ret{'data'}{'normalized'} = 1-$no_normalize;
1019  
1020 29 return $ret;
1021 }
1022  
1023 62 ▶sub convert {
Subroutine convert: called CC=10 SCAR=23.1
Pod documented
1024 62 my($self,$to) = @_;
1025  
1026 62 my %mode_val = ( 'exact' => 0,
1027 'semi' => 1,
1028 'approx' => 2,
1029 'estimated' => 3,
1030 );
1031  
1032 62 my $from = $$self{'data'}{'type'};
1033 62 my $from_val = $mode_val{$from};
1034 62 my $to_val = $mode_val{$to};
1035  
1036 62 ▶ return if ($from_val == $to_val);
Branch: 1 branch
BranchTrueFalseTotal
if $from_val == $to_val 19 43 62
1037  
1038 #
1039 # Converting from exact to less exact
1040 #
1041  
1042 43 ▶ if ($from_val < $to_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_val < $to_val) 35 8 43
1043  
1044 35 $self->set( { 'nonorm' => 0,
1045 'type' => $to } );
1046 35 return;
1047 }
1048  
1049 #
1050 # Converting from less exact to more exact
1051 # *** DEPRECATE *** 7.00
1052 #
1053  
1054 8 my @fields;
1055 {
1056 170 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1057  
1058 8 my $dmb = $self->base();
1059 8 my $mode= $$self{'data'}{'mode'};
1060 8 my $yl = $$dmb{'data'}{'len'}{$mode}{'yl'};
1061 8 my $ml = $$dmb{'data'}{'len'}{$mode}{'ml'};
1062 8 my $wl = $$dmb{'data'}{'len'}{$mode}{'wl'};
1063 8 my $dl = $$dmb{'data'}{'len'}{$mode}{'dl'};
1064  
1065 # Convert it to seconds
1066  
1067 8 my($y,$m,$w,$d,$h,$mn,$s) = @{ $$self{'data'}{'delta'} };
1068 8 $s += $y*$yl + $m*$ml + $w*$wl + $d*$dl + $h*3600 + $mn*60;
1069  
1070 8 @fields = (0,0,0,0,0,0,$s);
1071  
1072 8 ▶ if ($mode eq 'business') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'business') { } 4 4 8
1073  
1074 4 ▶ if ($to eq 'estimated') {
Branch: 2 branches
BranchTrueFalseTotal
if ($to eq 'estimated') { } 0 4 4
elsif ($to eq 'approx' or $to eq 'semi') { } 1 3 4
1075 0 @fields = $dmb->_normalize_bus_est(@fields);
1076  
1077 ▶ } elsif ($to eq 'approx' ||
Condition66%2/3
$to eq "approx" or $to eq "semi"
l!l&&r!l&&!r
013
MC/DC50%1/2
$to eq "approx" or $to eq "semi"
$to eq "approx" $to eq "semi"
Truth table66%2/3
A or B
A= $to eq "approx"B= $to eq "semi"
ABresult
1X1
011
000
1078 $to eq 'semi') {
1079 1 @fields = $dmb->_normalize_bus_approx(@fields);
1080  
1081 } else {
1082 3 @fields = $dmb->_normalize_bus_exact(@fields);
1083 }
1084  
1085 } else {
1086  
1087 4 ▶ if ($to eq 'estimated') {
Branch: 2 branches
BranchTrueFalseTotal
if ($to eq 'estimated') { } 0 4 4
elsif ($to eq 'approx' or $to eq 'semi') { } 1 3 4
1088 0 @fields = $dmb->_normalize_est(@fields);
1089  
1090 ▶ } elsif ($to eq 'approx' ||
Condition66%2/3
$to eq "approx" or $to eq "semi"
l!l&&r!l&&!r
013
MC/DC50%1/2
$to eq "approx" or $to eq "semi"
$to eq "approx" $to eq "semi"
Truth table66%2/3
A or B
A= $to eq "approx"B= $to eq "semi"
ABresult
1X1
011
000
1091 $to eq 'semi') {
1092 1 @fields = $dmb->_normalize_approx(@fields);
1093  
1094 } else {
1095 3 @fields = $dmb->_normalize_exact(@fields);
1096 }
1097  
1098 }
1099 }
1100  
1101 8 $$self{'data'}{'delta'} = [ @fields ];
1102 8 $$self{'data'}{'gotmode'} = 1;
1103 8 $$self{'data'}{'type'} = $to;
1104 8 $$self{'data'}{'type_from'} = 'opt';
1105 8 $$self{'data'}{'normalized'} = 1;
1106 8 $$self{'data'}{'length'} = 'unknown';
1107 }
1108  
1109 3 ▶sub cmp {
Subroutine cmp: called CC=7 SCAR=21.3
Pod documented
1110 3 my($self,$delta) = @_;
1111  
1112 3 ▶ if ($$self{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"err"}) 0 3 3
1113 0 carp "WARNING: [cmp] Arguments must be valid deltas: delta1";
1114 0 return undef;
1115 }
1116  
1117 3 ▶ if (! (ref($delta) eq 'Date::Manip::Delta')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $delta eq "Date::Manip::Delta") 0 3 3
1118 0 carp "WARNING: [cmp] Argument must be a Date::Manip::Delta object";
1119 0 return undef;
1120 }
1121 3 ▶ if ($$delta{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta->{"err"}) 0 3 3
1122 0 carp "WARNING: [cmp] Arguments must be valid deltas: delta2";
1123 0 return undef;
1124 }
1125  
1126 3 ▶ if ($$self{'data'}{'mode'} ne $$delta{'data'}{'mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"mode"} ne $delta->{"data"}{"mode"}) 0 3 3
1127 0 carp "WARNING: [cmp] Deltas must both be business or standard";
1128 0 return undef;
1129 }
1130  
1131 3 my $mode = $$self{'data'}{'mode'};
1132 3 my $dmb = $self->base();
1133 3 my $yl = $$dmb{'data'}{'len'}{$mode}{'yl'};
1134 3 my $ml = $$dmb{'data'}{'len'}{$mode}{'ml'};
1135 3 my $wl = $$dmb{'data'}{'len'}{$mode}{'wl'};
1136 3 my $dl = $$dmb{'data'}{'len'}{$mode}{'dl'};
1137  
1138 3 ▶ if ($$self{'data'}{'length'} eq 'unknown') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"length"} eq "unknown") 3 0 3
1139 3 my($y,$m,$w,$d,$h,$mn,$s) = @{ $$self{'data'}{'delta'} };
1140  
1141 170 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1142 3 $$self{'data'}{'length'} = int($y*$yl + $m*$ml + $w*$wl +
1143 $d*$dl + $h*3600 + $mn*60 + $s);
1144 }
1145  
1146 3 ▶ if ($$delta{'data'}{'length'} eq 'unknown') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta->{"data"}{"length"} eq "unknown") 3 0 3
1147 3 my($y,$m,$w,$d,$h,$mn,$s) = @{ $$delta{'data'}{'delta'} };
1148  
1149 170 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1150 3 $$delta{'data'}{'length'} = int($y*$yl + $m*$ml + $w*$wl +
1151 $d*$dl + $h*3600 + $mn*60 + $s);
1152 }
1153  
1154 3 return ($$self{'data'}{'length'} <=> $$delta{'data'}{'length'});
1155 }
1156  
1157 170 1;
1158 # Local Variables:
1159 # mode: cperl
1160 # indent-tabs-mode: nil
1161 # cperl-indent-level: 3
1162 # cperl-continued-statement-offset: 2
1163 # cperl-continued-brace-offset: 0
1164 # cperl-brace-offset: 0
1165 # cperl-brace-imaginary-offset: 0
1166 # cperl-label-offset: 0
1167 # End:
« lib/Date/Manip/DM6.pm ↑ Summary lib/Date/Manip/Base.pm »