--- Available.pm 2003/10/05 20:55:19 1.5 +++ Available.pm 2003/10/06 09:40:52 1.7 @@ -3,6 +3,7 @@ use 5.001; use strict; use warnings; +use Carp; require Exporter; @@ -59,25 +60,25 @@ $self->{ARGS} = {@_}; $debug = $self->{ARGS}->{DEBUG}; - die("need start time") if (! $self->{ARGS}->{start}); + croak("need start time") if (! $self->{ARGS}->{start}); # calc start and stop seconds my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3); print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); - my $s = $hh * 3600 || die("need at least hour specified for start time"); + my $s = $hh * 3600 || croak("need at least hour specified for start time"); $s += $mm * 60 if ($mm); $s += $ss if ($ss); $self->{start} = $s; - die("need end time") if (! $self->{ARGS}->{end}); + croak("need end time") if (! $self->{ARGS}->{end}); ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3); print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); - $s = $hh * 3600 || die("need at least hour specified for end time"); + $s = $hh * 3600 || croak("need at least hour specified for end time"); $s += $mm * 60 if ($mm); $self->{end} = $s; - die("need dayMask specified") if (! $self->{ARGS}->{dayMask}); + croak("need dayMask specified") if (! $self->{ARGS}->{dayMask}); $self->{dayMask} = $self->{ARGS}->{dayMask}; @@ -128,25 +129,26 @@ sub uptime { my $self = shift; - my $time = shift || die "need uptime timestamp to calculate uptime"; + my $time = shift || croak "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 +176,60 @@ } # +# this will return number of seconds that service is available if passed +# downtime of service +# + +sub downtime { + my $self = shift; + + my $time = shift || croak "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 # @@ -201,8 +257,8 @@ sub interval { my $self = shift; - my $from = shift || die "need start time for interval"; - my $to = shift || die "need end time for interval"; + my $from = shift || croak "need start time for interval"; + my $to = shift || croak "need end time for interval"; print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug); print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug); @@ -210,9 +266,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 +284,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 = abs($total - $self->downtime($to)); print STDERR "total: $total (final)\n" if ($debug); return $total; @@ -244,9 +303,9 @@ sub day_in_interval { my $self = shift; - my $time = shift || die "need timestamp to check if day is in interval"; + my $time = shift || croak "need timestamp to check if day is in interval"; - my @lt = localtime($time); + my @lt = gmtime($time); return $self->_dayOk($lt[6]); } @@ -371,9 +430,6 @@ =over 8 =item * -Use croak and not die in module for better error handling - -=item * Allow arbitary (array?) of holidays to be included. =back