| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
################################################### |
|
2
|
|
|
|
|
|
|
## Interval.pm (Time::Interval) |
|
3
|
|
|
|
|
|
|
## Andrew N. Hicox |
|
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
|
|
|
1
|
|
8804
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
50
|
|
|
13
|
|
|
|
|
|
|
require Exporter; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#class global vars ... |
|
16
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION @EXPORT @ISA %intervals); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1483
|
|
|
17
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
18
|
|
|
|
|
|
|
@EXPORT = qw(&parseInterval &convertInterval &getInterval &coalesce); |
|
19
|
|
|
|
|
|
|
$VERSION = 1.233; |
|
20
|
|
|
|
|
|
|
#what everything is worth in seconds |
|
21
|
|
|
|
|
|
|
%intervals = ( |
|
22
|
|
|
|
|
|
|
'days' => ((60**2) * 24), |
|
23
|
|
|
|
|
|
|
'hours' => (60 **2), |
|
24
|
|
|
|
|
|
|
'minutes' => 60, |
|
25
|
|
|
|
|
|
|
'seconds' => 1 |
|
26
|
|
|
|
|
|
|
); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
## getInterval #################################### |
|
30
|
|
|
|
|
|
|
sub getInterval { |
|
31
|
1
|
|
|
1
|
0
|
129
|
my $date1 = shift(); |
|
32
|
1
|
|
|
|
|
2
|
my $date2 = shift(); |
|
33
|
1
|
|
50
|
|
|
9
|
my $string = shift() || ""; |
|
34
|
1
|
50
|
33
|
|
|
7
|
if ( (! $date1) || (! $date2) ){ |
|
35
|
0
|
|
|
|
|
0
|
warn ("two dates are required for the getInterval method"); |
|
36
|
0
|
|
|
|
|
0
|
return (undef); |
|
37
|
|
|
|
|
|
|
} |
|
38
|
1
|
|
|
|
|
967
|
require Date::Parse; |
|
39
|
1
|
|
|
|
|
7326
|
foreach ($date1, $date2){ |
|
40
|
2
|
|
33
|
|
|
460
|
$_ = Date::Parse::str2time($_) || do { |
|
41
|
|
|
|
|
|
|
warn ("failed to parse date: $!\n"); |
|
42
|
|
|
|
|
|
|
return (undef); |
|
43
|
|
|
|
|
|
|
}; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
190
|
my %args = ( seconds => abs($date1 - $date2) ); |
|
47
|
1
|
50
|
|
|
|
8
|
if ($string =~/^small/i){ |
|
|
|
50
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
0
|
$args{'Small'} = 1; |
|
49
|
|
|
|
|
|
|
}elsif($string !~/^\s*$/){ |
|
50
|
0
|
|
|
|
|
0
|
$args{'String'} = 1; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
1
|
|
|
|
|
6
|
my $data = parseInterval(%args); |
|
53
|
1
|
|
|
|
|
5
|
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
|
|
|
|
|
|
|
sub convertInterval { |
|
64
|
14
|
|
|
14
|
0
|
265
|
my %p = @_; |
|
65
|
|
|
|
|
|
|
#ConvertTo, default is seconds |
|
66
|
14
|
100
|
|
|
|
42
|
exists($p{'ConvertTo'}) || do { |
|
67
|
10
|
|
|
|
|
20
|
$p{'ConvertTo'} = "seconds"; |
|
68
|
10
|
50
|
|
|
|
25
|
warn ("convertInterval: using default ConvertTo (seconds)") if $p{'Debug'}; |
|
69
|
|
|
|
|
|
|
}; |
|
70
|
|
|
|
|
|
|
#convert everything to seconds |
|
71
|
14
|
|
|
|
|
18
|
my $seconds = 0; |
|
72
|
14
|
|
|
|
|
24
|
foreach ("days","hours","minutes","seconds"){ |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#new 1.233 hotness: quantize seconds. |
|
75
|
56
|
100
|
|
|
|
108
|
if ($_ eq "seconds"){ $p{$_} = int($p{$_}); } |
|
|
14
|
|
|
|
|
27
|
|
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#as it were |
|
78
|
56
|
100
|
|
|
|
109
|
if (exists($p{$_})){ $seconds += ($intervals{$_} * $p{$_}); } |
|
|
32
|
|
|
|
|
71
|
|
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
#send it back out into the desired output |
|
81
|
14
|
|
|
|
|
53
|
return (($seconds/$intervals{$p{'ConvertTo'}})); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
## parseInterval ################################## |
|
86
|
|
|
|
|
|
|
#'days' => $num, |
|
87
|
|
|
|
|
|
|
#'hours' => $num, |
|
88
|
|
|
|
|
|
|
#'minutes' => $num, |
|
89
|
|
|
|
|
|
|
#'seconds' => $num, |
|
90
|
|
|
|
|
|
|
sub parseInterval { |
|
91
|
10
|
|
|
10
|
0
|
891
|
my %p = @_; |
|
92
|
|
|
|
|
|
|
#convert everything to seconds |
|
93
|
10
|
|
|
|
|
37
|
my $seconds = convertInterval(%p); |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#new 1.233 hotness: quantize seconds. |
|
96
|
10
|
|
|
|
|
31
|
$seconds = int($seconds); |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
#do the thang |
|
99
|
10
|
|
|
|
|
82
|
my %time = ( |
|
100
|
|
|
|
|
|
|
'days' => 0, |
|
101
|
|
|
|
|
|
|
'hours' => 0, |
|
102
|
|
|
|
|
|
|
'minutes' => 0, |
|
103
|
|
|
|
|
|
|
'seconds' => 0 |
|
104
|
|
|
|
|
|
|
); |
|
105
|
10
|
|
|
|
|
24
|
while ($seconds > 0){ |
|
106
|
26213
|
|
|
|
|
29926
|
foreach ("days","hours","minutes","seconds"){ |
|
107
|
27020
|
100
|
|
|
|
45552
|
if ($seconds >= $intervals{$_}){ |
|
108
|
26213
|
|
|
|
|
25406
|
$time{$_} ++; |
|
109
|
26213
|
|
|
|
|
25676
|
$seconds -= $intervals{$_}; |
|
110
|
26213
|
|
|
|
|
41389
|
last; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
#return data |
|
115
|
10
|
100
|
66
|
|
|
96
|
if ($p{'Small'} && $p{'Small'} != 0) { |
|
|
|
100
|
66
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#return a string? |
|
117
|
5
|
|
|
|
|
14
|
my @temp = (); |
|
118
|
5
|
|
|
|
|
10
|
foreach ("days","hours","minutes","seconds"){ |
|
119
|
20
|
100
|
|
|
|
55
|
if ($time{$_} > 0){ |
|
120
|
10
|
|
|
|
|
52
|
push (@temp, "$time{$_}".substr($_,0,1)); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
} |
|
123
|
5
|
|
50
|
|
|
52
|
return join (" ", @temp) || "0s"; |
|
124
|
|
|
|
|
|
|
}elsif ($p{'String'} && $p{'String'} != 0){ |
|
125
|
|
|
|
|
|
|
#return a string? |
|
126
|
4
|
|
|
|
|
10
|
my @temp = (); |
|
127
|
4
|
|
|
|
|
10
|
foreach ("days","hours","minutes","seconds"){ |
|
128
|
16
|
100
|
|
|
|
39
|
if ($time{$_} > 0){ |
|
129
|
9
|
50
|
|
|
|
21
|
if ($time{$_} == 1) { |
|
130
|
0
|
|
|
|
|
0
|
push (@temp, "$time{$_} ".substr($_,0,-1)); |
|
131
|
|
|
|
|
|
|
} else { |
|
132
|
9
|
|
|
|
|
33
|
push (@temp, "$time{$_} $_"); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
} |
|
136
|
4
|
|
50
|
|
|
40
|
return (join (", ", @temp)) || "0 seconds"; |
|
137
|
|
|
|
|
|
|
}else{ |
|
138
|
|
|
|
|
|
|
#return a data structure |
|
139
|
1
|
|
|
|
|
5
|
return (\%time); |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
## coalesce ####################################### |
|
145
|
|
|
|
|
|
|
#coalesce([ [$start1, $end1], [$start2, $end2] ... ]) |
|
146
|
|
|
|
|
|
|
sub coalesce { |
|
147
|
1
|
|
|
1
|
0
|
270
|
require Date::Parse; |
|
148
|
1
|
|
50
|
|
|
6
|
my $intervals = shift() || []; |
|
149
|
1
|
|
|
|
|
4
|
my %epoch_map = (); |
|
150
|
1
|
|
|
|
|
3
|
my ($flag, $repeat) = (0,1); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
#convert each start / end to an epoch pair and stash 'em in epoch_map |
|
153
|
1
|
|
|
|
|
2
|
foreach my $int (@{$intervals}) { |
|
|
1
|
|
|
|
|
4
|
|
|
154
|
6
|
|
|
|
|
9
|
foreach (@{$int}){ |
|
|
6
|
|
|
|
|
13
|
|
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
## only convert if it's not already epoch time |
|
157
|
12
|
|
|
|
|
17
|
my $epoch = ""; |
|
158
|
12
|
50
|
|
|
|
42
|
if ($_ =~/^(\d{10})$/){ |
|
159
|
0
|
|
|
|
|
0
|
$epoch = $1; |
|
160
|
|
|
|
|
|
|
}else{ |
|
161
|
12
|
|
|
|
|
36
|
my $epoch = Date::Parse::str2time($_); |
|
162
|
|
|
|
|
|
|
} |
|
163
|
12
|
|
|
|
|
2329
|
$epoch_map{$epoch} = $_; |
|
164
|
12
|
|
|
|
|
37
|
$_ = $epoch; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
#sort 'em by start time |
|
169
|
1
|
|
|
|
|
3
|
@{$intervals} = sort { $a->[0] <=> $b->[0] } @{$intervals}; |
|
|
1
|
|
|
|
|
6
|
|
|
|
9
|
|
|
|
|
22
|
|
|
|
1
|
|
|
|
|
43
|
|
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
#flatten 'em |
|
172
|
1
|
|
|
|
|
5
|
while ($repeat == 1) { |
|
173
|
4
|
|
|
|
|
9
|
@{$intervals} = sort { |
|
174
|
|
|
|
|
|
|
#if it's not an array ref, it's been destructo'd |
|
175
|
10
|
100
|
66
|
|
|
38
|
if ( (ref($a) eq "ARRAY") && (ref($b) eq "ARRAY") ){ |
|
|
4
|
|
|
|
|
11
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
#if b is inside a |
|
178
|
5
|
50
|
33
|
|
|
28
|
if (($b->[0] >= $a->[0]) && ($b->[0] <= $a->[1])){ |
|
|
|
0
|
0
|
|
|
|
|
|
179
|
|
|
|
|
|
|
#if b's end time is greater than a's, update a's end time |
|
180
|
5
|
50
|
|
|
|
14
|
if ($b->[1] > $a->[1]){ $a->[1] = $b->[1]; } |
|
|
0
|
|
|
|
|
0
|
|
|
181
|
|
|
|
|
|
|
#destructo b |
|
182
|
5
|
|
|
|
|
6
|
$b = (); |
|
183
|
5
|
|
|
|
|
7
|
$flag = 1; |
|
184
|
5
|
|
|
|
|
9
|
return (1); |
|
185
|
|
|
|
|
|
|
#if a is inside b |
|
186
|
|
|
|
|
|
|
}elsif (($a->[0] >= $b->[0]) && ($a->[0] <= $b->[1])){ |
|
187
|
|
|
|
|
|
|
#if a's end time is greater than b's, update b's end time |
|
188
|
0
|
0
|
|
|
|
0
|
if ($a->[1] > $b->[1]){ $b->[1] = $a->[1]; } |
|
|
0
|
|
|
|
|
0
|
|
|
189
|
|
|
|
|
|
|
#destructo a |
|
190
|
0
|
|
|
|
|
0
|
$a = (); |
|
191
|
0
|
|
|
|
|
0
|
$flag = 1; |
|
192
|
0
|
|
|
|
|
0
|
return (0); |
|
193
|
|
|
|
|
|
|
}else{ |
|
194
|
0
|
|
|
|
|
0
|
return (0); |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
}else{ |
|
198
|
5
|
|
|
|
|
6
|
return (1); |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
4
|
|
|
|
|
590
|
} @{$intervals}; |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
#weed out null elements |
|
204
|
4
|
|
|
|
|
6
|
my $i = 0; |
|
205
|
4
|
|
|
|
|
4
|
foreach (@{$intervals}){ |
|
|
4
|
|
|
|
|
8
|
|
|
206
|
7
|
100
|
|
|
|
13
|
if ( ref($_) ne "ARRAY" ){ splice (@{$intervals}, $i, 1); } |
|
|
5
|
|
|
|
|
6
|
|
|
|
5
|
|
|
|
|
9
|
|
|
207
|
7
|
|
|
|
|
11
|
$i ++; |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
#decide wether or not to repeat |
|
211
|
4
|
100
|
|
|
|
9
|
if ($flag == 1){ |
|
212
|
3
|
|
|
|
|
3
|
$repeat = 1; |
|
213
|
3
|
|
|
|
|
9
|
$flag = 0; |
|
214
|
|
|
|
|
|
|
}else{ |
|
215
|
1
|
|
|
|
|
3
|
$repeat = 0; |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
} |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
#replace the epoch's with their time string equivalents |
|
220
|
1
|
|
|
|
|
2
|
foreach my $pair (@{$intervals}){ |
|
|
1
|
|
|
|
|
2
|
|
|
221
|
1
|
50
|
|
|
|
6
|
if ( ref($pair) eq "ARRAY"){ |
|
222
|
1
|
|
|
|
|
1
|
foreach (@{$pair}){ $_ = $epoch_map{$_}; } |
|
|
1
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
6
|
|
|
223
|
|
|
|
|
|
|
} |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
#weed out any remaining bum elements |
|
227
|
1
|
|
|
|
|
3
|
my $i = 0; |
|
228
|
1
|
|
|
|
|
1
|
foreach (@{$intervals}){ |
|
|
1
|
|
|
|
|
3
|
|
|
229
|
1
|
50
|
|
|
|
4
|
unless (ref($_) eq "ARRAY"){ splice (@{$intervals}, $i, 1); } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
230
|
1
|
|
|
|
|
2
|
$i ++; |
|
231
|
|
|
|
|
|
|
} |
|
232
|
1
|
|
|
|
|
5
|
return ($intervals); |
|
233
|
|
|
|
|
|
|
} |