blib/lib/Time/Interval.pm

filter: Statementstmt 87.1% 108 / 124 Branchbran 74.0% 37 / 50 Conditioncond 44.8% 13 / 29 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 72.2% 167 / 231 CC 48 SCAR 29.8
CC
48
Cov
78%
coalesce
30.1
parseInterval
15.4
getInterval
13.9
CRAP 19.7
↑ Summary
1 ###################################################
2 ## Interval.pm (Time::Interval)
3 ## Andrew N. Hicox <andrew@hicox.com>
4 ## http://www.hicox.com
5 ##
6 ## a module for dealing with time intervals
7 ###################################################
8  
9  
10 ## Global Stuff ###################################
11 package Time::Interval;
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 require Exporter;
14  
15 #class global vars ...
16 1 ▶use vars qw($VERSION @EXPORT @ISA %intervals);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 @ISA = qw(Exporter);
18 1 @EXPORT = qw(&parseInterval &convertInterval &getInterval &coalesce);
19 1 $VERSION = 1.234;
20 #what everything is worth in seconds
21 1 %intervals = (
22 'days' => ((60**2) * 24),
23 'hours' => (60 **2),
24 'minutes' => 60,
25 'seconds' => 1
26 );
27  
28  
29 ## getInterval ####################################
30 1 ▶sub getInterval {
Subroutine getInterval: called CC=8 SCAR=26.3
Pod undocumented
31 1 my $date1 = shift();
32 1 my $date2 = shift();
33 1 ▶ my $string = shift() || "";
Condition50%1/2
shift() || ''
l!l
01
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
34 1 ▶ if ( (! $date1) || (! $date2) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($date1 and $date2) 0 1 1
Condition33%1/3
$date1 and $date2
!ll&&!rl&&r
001
MC/DC0%0/2
$date1 and $date2
$date1 $date2
Truth table33%1/3
A and B
A= $date1B= $date2
ABresult
0X0
100
111
35 0 warn ("two dates are required for the getInterval method");
36 0 return (undef);
37 }
38 1 require Date::Parse;
39 1 foreach ($date1, $date2){
40 2 ▶ $_ = Date::Parse::str2time($_) || do {
Condition33%1/3
Date::Parse::str2time($_) || do { warn "failed to parse date: $!\n"; return undef }
l!l&&r!l&&!r
200
MC/DC0%0/2
Date::Parse::str2time($_) || do { warn "failed to parse date: $!\n"; return undef }
Date::Parse::str2time($_) do { warn "failed to parse date: $!\n"; return undef }
Truth table33%1/3
A || B
A= Date::Parse::str2time($_)B= do { warn "failed to parse date: $!\n"; return undef }
ABresult
1X1
011
000
41 0 warn ("failed to parse date: $!\n");
42 0 return (undef);
43 };
44 }
45  
46 1 my %args = ( seconds => abs($date1 - $date2) );
47 1 ▶ if ($string =~/^small/i){
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ /^small/i) { } 0 1 1
elsif (not $string =~ /^\s*$/) { } 0 1 1
48 0 $args{'Small'} = 1;
49 }elsif($string !~/^\s*$/){
50 0 $args{'String'} = 1;
51 }
52 1 my $data = parseInterval(%args);
53 1 return ($data);
54 }
55  
56  
57 ## convertInterval ################################
58 #'days' => $num,
59 #'hours' => $num,
60 #'minutes' => $num,
61 #'seconds' => $num,
62 #'ConvertTo' => 'days'|'hours'|'minutes'|'seconds'
63 14 ▶sub convertInterval {
Subroutine convertInterval: called CC=6 SCAR=17.9
Pod undocumented
64 14 my %p = @_;
65 #ConvertTo, default is seconds
66 14 ▶ exists($p{'ConvertTo'}) || do {
Branch: 1 branch
BranchTrueFalseTotal
exists $p{"ConvertTo"} or do { $p{"ConvertTo"} = "seconds"; warn "convertInterval: using default ConvertTo (seconds)" if $p{"Debug"} } 4 10 14
67 10 $p{'ConvertTo'} = "seconds";
68 10 ▶ warn ("convertInterval: using default ConvertTo (seconds)") if $p{'Debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"Debug"} 0 10 10
69 };
70 #convert everything to seconds
71 14 my $seconds = 0;
72 14 foreach ("days","hours","minutes","seconds"){
73  
74 #new 1.233 hotness: quantize seconds.
75 56 ▶ if ($_ eq "seconds"){ $p{$_} = int($p{$_}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "seconds") 14 42 56
76  
77 #as it were
78 56 ▶ if (exists($p{$_})){ $seconds += ($intervals{$_} * $p{$_}); }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{$_}) 32 24 56
79 }
80 #send it back out into the desired output
81 14 return (($seconds/$intervals{$p{'ConvertTo'}}));
82 }
83  
84  
85 ## parseInterval ##################################
86 #'days' => $num,
87 #'hours' => $num,
88 #'minutes' => $num,
89 #'seconds' => $num,
90 10 ▶sub parseInterval {
Subroutine parseInterval: called CC=15 SCAR=27.4
Pod undocumented
91 10 my %p = @_;
92 #convert everything to seconds
93 10 my $seconds = convertInterval(%p);
94  
95 #new 1.233 hotness: quantize seconds.
96 10 $seconds = int($seconds);
97  
98 #do the thang
99 10 my %time = (
100 'days' => 0,
101 'hours' => 0,
102 'minutes' => 0,
103 'seconds' => 0
104 );
105 10 ▶ while ($seconds > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($seconds > 0) 26213 10 26223
106 26213 foreach ("days","hours","minutes","seconds"){
107 27020 ▶ if ($seconds >= $intervals{$_}){
Branch: 1 branch
BranchTrueFalseTotal
if ($seconds >= $Time::Interval::intervals{$_}) 26213 807 27020
108 26213 $time{$_} ++;
109 26213 $seconds -= $intervals{$_};
110 26213 last;
111 }
112 }
113 }
114 #return data
115 10 ▶ if ($p{'Small'} && $p{'Small'} != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p{'Small'} and $p{'Small'} != 0) { } 5 5 10
elsif ($p{'String'} and $p{'String'} != 0) { } 4 1 5
Condition66%2/3
$p{"Small"} and $p{"Small"} != 0
!ll&&!rl&&r
505
MC/DC50%1/2
$p{"Small"} and $p{"Small"} != 0
$p{"Small"} $p{"Small"} != 0
Truth table66%2/3
A and B
A= $p{"Small"}B= $p{"Small"} != 0
ABresult
0X0
100
111
116 #return a string?
117 5 my @temp = ();
118 5 foreach ("days","hours","minutes","seconds"){
119 20 ▶ if ($time{$_} > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($time{$_} > 0) 10 10 20
120 10 push (@temp, "$time{$_}".substr($_,0,1));
121 }
122 }
123 5 ▶ return join (" ", @temp) || "0s";
Condition50%1/2
join(' ', @temp) || '0s'
l!l
50
MC/DC0%0/1
join(' ', @temp) || '0s'
join(' ', @temp)
Truth table50%1/2
A
A= join(' ', @temp)
Aresult
11
00
124 ▶ }elsif ($p{'String'} && $p{'String'} != 0){
Condition66%2/3
$p{"String"} and $p{"String"} != 0
!ll&&!rl&&r
104
MC/DC50%1/2
$p{"String"} and $p{"String"} != 0
$p{"String"} $p{"String"} != 0
Truth table66%2/3
A and B
A= $p{"String"}B= $p{"String"} != 0
ABresult
0X0
100
111
125 #return a string?
126 4 my @temp = ();
127 4 foreach ("days","hours","minutes","seconds"){
128 16 ▶ if ($time{$_} > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($time{$_} > 0) 9 7 16
129 9 ▶ if ($time{$_} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time{$_} == 1) { } 0 9 9
130 0 push (@temp, "$time{$_} ".substr($_,0,-1));
131 } else {
132 9 push (@temp, "$time{$_} $_");
133 }
134 }
135 }
136 4 ▶ return (join (", ", @temp)) || "0 seconds";
Condition50%1/2
join(', ', @temp) || '0 seconds'
l!l
40
MC/DC0%0/1
join(', ', @temp) || '0 seconds'
join(', ', @temp)
Truth table50%1/2
A
A= join(', ', @temp)
Aresult
11
00
137 }else{
138 #return a data structure
139 1 return (\%time);
140 }
141 }
142  
143  
144 ## coalesce #######################################
145 #coalesce([ [$start1, $end1], [$start2, $end2] ... ])
146 1 ▶sub coalesce {
Subroutine coalesce: called CC=22 SCAR=34.0
Pod undocumented
147 1 require Date::Parse;
148 1 ▶ my $intervals = shift() || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
149 1 my %epoch_map = ();
150 1 my ($flag, $repeat) = (0,1);
151  
152 #convert each start / end to an epoch pair and stash 'em in epoch_map
153 1 foreach my $int (@{$intervals}) {
154 6 foreach (@{$int}){
155  
156 ## only convert if it's not already epoch time
157 12 my $epoch = "";
158 12 ▶ if ($_ =~/^(\d{10})$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^(\d{10})$/) { } 0 12 12
159 0 $epoch = $1;
160 }else{
161 12 $epoch = Date::Parse::str2time($_);
162 }
163 12 $epoch_map{$epoch} = $_;
164 12 $_ = $epoch;
165 }
166 }
167  
168 #sort 'em by start time
169 1 @{$intervals} = sort { $a->[0] <=> $b->[0] } @{$intervals};
170  
171 #flatten 'em
172 1 ▶ while ($repeat == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repeat == 1) 4 1 5
173 4 @{$intervals} = sort {
174 #if it's not an array ref, it's been destructo'd
175 10 ▶ if ( (ref($a) eq "ARRAY") && (ref($b) eq "ARRAY") ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'ARRAY' and ref $b eq 'ARRAY') { } 5 5 10
Condition66%2/3
ref $a eq "ARRAY" and ref $b eq "ARRAY"
!ll&&!rl&&r
505
MC/DC50%1/2
ref $a eq "ARRAY" and ref $b eq "ARRAY"
ref $a eq "ARRAY" ref $b eq "ARRAY"
Truth table66%2/3
A and B
A= ref $a eq "ARRAY"B= ref $b eq "ARRAY"
ABresult
0X0
100
111
176  
177 #if b is inside a
178 5 ▶ if (($b->[0] >= $a->[0]) && ($b->[0] <= $a->[1])){
Branch: 2 branches
BranchTrueFalseTotal
if ($Time::Interval::b->[0] >= $Time::Interval::a->[0] and $Time::Interval::b->[0] <= $Time::Interval::a->[1]) { } 5 0 5
elsif ($Time::Interval::a->[0] >= $Time::Interval::b->[0] and $Time::Interval::a->[0] <= $Time::Interval::b->[1]) { } 0 0 0
Condition33%1/3
$Time::Interval::b->[0] >= $Time::Interval::a->[0] and $Time::Interval::b->[0] <= $Time::Interval::a->[1]
!ll&&!rl&&r
005
MC/DC0%0/2
$Time::Interval::b->[0] >= $Time::Interval::a->[0] and $Time::Interval::b->[0] <= $Time::Interval::a->[1]
$Time::Interval::b->[0] >= $Time::Interval::a->[0] $Time::Interval::b->[0] <= $Time::Interval::a->[1]
Truth table33%1/3
A and B
A= $Time::Interval::b->[0] >= $Time::Interval::a->[0]B= $Time::Interval::b->[0] <= $Time::Interval::a->[1]
ABresult
0X0
100
111
179 #if b's end time is greater than a's, update a's end time
180 5 ▶ if ($b->[1] > $a->[1]){ $a->[1] = $b->[1]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Interval::b->[1] > $Time::Interval::a->[1]) 4 1 5
181 #destructo b
182 5 $b = ();
183 5 $flag = 1;
184 5 return (1);
185 #if a is inside b
186 ▶ }elsif (($a->[0] >= $b->[0]) && ($a->[0] <= $b->[1])){
Condition0%0/3
$Time::Interval::a->[0] >= $Time::Interval::b->[0] and $Time::Interval::a->[0] <= $Time::Interval::b->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$Time::Interval::a->[0] >= $Time::Interval::b->[0] and $Time::Interval::a->[0] <= $Time::Interval::b->[1]
$Time::Interval::a->[0] >= $Time::Interval::b->[0] $Time::Interval::a->[0] <= $Time::Interval::b->[1]
Truth table0%0/3
A and B
A= $Time::Interval::a->[0] >= $Time::Interval::b->[0]B= $Time::Interval::a->[0] <= $Time::Interval::b->[1]
ABresult
0X0
100
111
187 #if a's end time is greater than b's, update b's end time
188 0 ▶ if ($a->[1] > $b->[1]){ $b->[1] = $a->[1]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Interval::a->[1] > $Time::Interval::b->[1]) 0 0 0
189 #destructo a
190 0 $a = ();
191 0 $flag = 1;
192 0 return (0);
193 }else{
194 0 return (0);
195 }
196  
197 }else{
198 5 return (1);
199 }
200  
201 4 } @{$intervals};
202  
203 #weed out null elements
204 4 my $i = 0;
205 4 foreach (@{$intervals}){
206 7 ▶ if ( ref($_) ne "ARRAY" ){ splice (@{$intervals}, $i, 1); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ ne "ARRAY") 5 2 7
207 7 $i ++;
208 }
209  
210 #decide wether or not to repeat
211 4 ▶ if ($flag == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 1) { } 3 1 4
212 3 $repeat = 1;
213 3 $flag = 0;
214 }else{
215 1 $repeat = 0;
216 }
217 }
218  
219 #replace the epoch's with their time string equivalents
220 1 foreach my $pair (@{$intervals}){
221 1 ▶ if ( ref($pair) eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pair eq "ARRAY") 1 0 1
222 1 foreach (@{$pair}){ $_ = $epoch_map{$_}; }
223 }
224 }
225  
226 #weed out any remaining bum elements
227 1 my $i = 0;
228 1 foreach (@{$intervals}){
229 0 ▶ unless (ref($_) eq "ARRAY"){ splice (@{$intervals}, $i, 1); }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_ eq "ARRAY") 0 1 1
230 1 $i ++;
231 }
232 1 return ($intervals);
233 }
↑ Summary