131 |
my $time = shift || die "need uptime timestamp to calculate uptime"; |
my $time = shift || die "need uptime timestamp to calculate uptime"; |
132 |
|
|
133 |
# calculate offset -- that is number of seconds since midnight |
# calculate offset -- that is number of seconds since midnight |
134 |
my @lt = localtime($time); |
my @lt = gmtime($time); |
135 |
|
|
136 |
|
# check if day falls into dayMask |
137 |
|
return 0 if (! $self->_dayOk($lt[6]) ); |
138 |
|
|
139 |
my $offset = $lt[2]; # hour |
my $offset = $lt[2]; # hour |
140 |
$offset *= 60; # convert to minutes |
$offset *= 60; # convert to minutes |
141 |
$offset += $lt[1]; # minutes |
$offset += $lt[1]; # minutes |
142 |
$offset *= 60; # convert to seconds |
$offset *= 60; # convert to seconds |
143 |
$offset += $lt[0]; |
$offset += $lt[0]; |
144 |
|
|
|
# check if day falls into dayMask |
|
|
return 0 if (! $self->_dayOk($lt[6]) ); |
|
|
|
|
145 |
my $s=0; |
my $s=0; |
146 |
|
|
147 |
my $start = $self->{start}; |
my $start = $self->{start}; |
148 |
my $end = $self->{end}; |
my $end = $self->{end}; |
149 |
|
|
150 |
print STDERR "start: $start end: $end time: $offset\n" if ($debug); |
print STDERR "start: $start end: $end time: $offset [$lt[2]:$lt[1]:$lt[0]]\n" if ($debug); |
151 |
|
|
152 |
if ( $end > $start ) { |
if ( $end > $start ) { |
153 |
if ($offset < $start) { |
if ($offset < $start) { |
175 |
} |
} |
176 |
|
|
177 |
# |
# |
178 |
|
# this will return number of seconds that service is available if passed |
179 |
|
# downtime of service |
180 |
|
# |
181 |
|
|
182 |
|
sub downtime { |
183 |
|
my $self = shift; |
184 |
|
|
185 |
|
my $time = shift || die "need downtime timestamp to calculate uptime"; |
186 |
|
|
187 |
|
# calculate offset -- that is number of seconds since midnight |
188 |
|
my @lt = gmtime($time); |
189 |
|
|
190 |
|
# check if day falls into dayMask |
191 |
|
return 0 if (! $self->_dayOk($lt[6]) ); |
192 |
|
|
193 |
|
my $offset = $lt[2]; # hour |
194 |
|
$offset *= 60; # convert to minutes |
195 |
|
$offset += $lt[1]; # minutes |
196 |
|
$offset *= 60; # convert to seconds |
197 |
|
$offset += $lt[0]; |
198 |
|
|
199 |
|
my $s=0; |
200 |
|
|
201 |
|
my $start = $self->{start}; |
202 |
|
my $end = $self->{end}; |
203 |
|
|
204 |
|
print STDERR "start: $start end: $end time: $offset [$lt[2]:$lt[1]:$lt[0]]\n" if ($debug); |
205 |
|
|
206 |
|
if ( $end > $start ) { |
207 |
|
if ($offset > $start && $offset <= $end) { |
208 |
|
$s = $end - $offset; |
209 |
|
} elsif ($offset < $start) { |
210 |
|
$s = $end - $start; |
211 |
|
} |
212 |
|
} elsif ( $start > $end ) { # over midnight |
213 |
|
if ( $offset < $end ) { |
214 |
|
if ( $offset < $start) { |
215 |
|
$s = $offset; |
216 |
|
} else { |
217 |
|
$s = 0; |
218 |
|
} |
219 |
|
} else { |
220 |
|
if ( $offset < $start ) { |
221 |
|
$s = SEC_PER_DAY - $end; |
222 |
|
} else { |
223 |
|
$s = SEC_PER_DAY - $end + $start - $offset; |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
return $s; |
229 |
|
} |
230 |
|
|
231 |
|
# |
232 |
# this auxillary function will pretty-format interval in [days]d hh:mm:ss |
# this auxillary function will pretty-format interval in [days]d hh:mm:ss |
233 |
# |
# |
234 |
|
|
265 |
my $total = 0; |
my $total = 0; |
266 |
|
|
267 |
# calc first day availability |
# calc first day availability |
268 |
|
print STDERR "t:\t$from\t",scalar gmtime($from),"\n" if ($debug); |
269 |
$total += $self->uptime($from); |
$total += $self->uptime($from); |
270 |
|
|
271 |
print STDERR "total: $total\n" if ($debug); |
print STDERR "total: $total (first)\n" if ($debug); |
272 |
|
|
273 |
# add all whole days |
# add all whole days |
274 |
|
|
283 |
for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) { |
for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) { |
284 |
print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug); |
print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug); |
285 |
$total += $sec_in_day if ($self->day_in_interval($t)); |
$total += $sec_in_day if ($self->day_in_interval($t)); |
286 |
print STDERR "total: $total\n" if ($debug); |
print STDERR "total: $total (loop)\n" if ($debug); |
287 |
} |
} |
288 |
|
|
289 |
# add rest of last day |
# add rest of last day |
290 |
$total -= $self->utpime($to); |
print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug); |
291 |
|
|
292 |
|
$total -= $self->downtime($to); |
293 |
print STDERR "total: $total (final)\n" if ($debug); |
print STDERR "total: $total (final)\n" if ($debug); |
294 |
|
|
295 |
return $total; |
return $total; |
304 |
|
|
305 |
my $time = shift || die "need timestamp to check if day is in interval"; |
my $time = shift || die "need timestamp to check if day is in interval"; |
306 |
|
|
307 |
my @lt = localtime($time); |
my @lt = gmtime($time); |
308 |
return $self->_dayOk($lt[6]); |
return $self->_dayOk($lt[6]); |
309 |
} |
} |
310 |
|
|