--- Available.pm 2003/10/05 20:55:19 1.5 +++ Available.pm 2003/10/05 22:26:54 1.6 @@ -131,22 +131,23 @@ my $time = shift || die "need uptime timestamp to calculate uptime"; # calculate offset -- that is number of seconds since midnight - my @lt = localtime($time); + my @lt = gmtime($time); + + # check if day falls into dayMask + return 0 if (! $self->_dayOk($lt[6]) ); + my $offset = $lt[2]; # hour $offset *= 60; # convert to minutes $offset += $lt[1]; # minutes $offset *= 60; # convert to seconds $offset += $lt[0]; - # check if day falls into dayMask - return 0 if (! $self->_dayOk($lt[6]) ); - my $s=0; my $start = $self->{start}; my $end = $self->{end}; - 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); if ( $end > $start ) { if ($offset < $start) { @@ -174,6 +175,60 @@ } # +# this will return number of seconds that service is available if passed +# downtime of service +# + +sub downtime { + my $self = shift; + + my $time = shift || die "need downtime timestamp to calculate uptime"; + + # calculate offset -- that is number of seconds since midnight + my @lt = gmtime($time); + + # check if day falls into dayMask + return 0 if (! $self->_dayOk($lt[6]) ); + + my $offset = $lt[2]; # hour + $offset *= 60; # convert to minutes + $offset += $lt[1]; # minutes + $offset *= 60; # convert to seconds + $offset += $lt[0]; + + my $s=0; + + my $start = $self->{start}; + my $end = $self->{end}; + + print STDERR "start: $start end: $end time: $offset [$lt[2]:$lt[1]:$lt[0]]\n" if ($debug); + + if ( $end > $start ) { + if ($offset > $start && $offset <= $end) { + $s = $end - $offset; + } elsif ($offset < $start) { + $s = $end - $start; + } + } elsif ( $start > $end ) { # over midnight + if ( $offset < $end ) { + if ( $offset < $start) { + $s = $offset; + } else { + $s = 0; + } + } else { + if ( $offset < $start ) { + $s = SEC_PER_DAY - $end; + } else { + $s = SEC_PER_DAY - $end + $start - $offset; + } + } + } + + return $s; +} + +# # this auxillary function will pretty-format interval in [days]d hh:mm:ss # @@ -210,9 +265,10 @@ my $total = 0; # calc first day availability + print STDERR "t:\t$from\t",scalar gmtime($from),"\n" if ($debug); $total += $self->uptime($from); - print STDERR "total: $total\n" if ($debug); + print STDERR "total: $total (first)\n" if ($debug); # add all whole days @@ -227,11 +283,13 @@ for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) { print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug); $total += $sec_in_day if ($self->day_in_interval($t)); - print STDERR "total: $total\n" if ($debug); + print STDERR "total: $total (loop)\n" if ($debug); } # add rest of last day - $total -= $self->utpime($to); + print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug); + + $total -= $self->downtime($to); print STDERR "total: $total (final)\n" if ($debug); return $total; @@ -246,7 +304,7 @@ my $time = shift || die "need timestamp to check if day is in interval"; - my @lt = localtime($time); + my @lt = gmtime($time); return $self->_dayOk($lt[6]); }