--- Available.pm 2003/10/05 19:06:44 1.4 +++ Available.pm 2003/10/05 20:55:19 1.5 @@ -63,6 +63,7 @@ # 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"); $s += $mm * 60 if ($mm); $s += $ss if ($ss); @@ -71,6 +72,7 @@ die("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 += $mm * 60 if ($mm); $self->{end} = $s; @@ -126,7 +128,7 @@ sub uptime { my $self = shift; - my $time = shift || die "need uptime timestamp to calcualte uptime"; + my $time = shift || die "need uptime timestamp to calculate uptime"; # calculate offset -- that is number of seconds since midnight my @lt = localtime($time); @@ -193,6 +195,76 @@ return $out; } +# +# this function will calculate uptime for some interval +# + +sub interval { + my $self = shift; + my $from = shift || die "need start time for interval"; + my $to = shift || die "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); + + my $total = 0; + + # calc first day availability + $total += $self->uptime($from); + + print STDERR "total: $total\n" if ($debug); + + # add all whole days + + my $sec_in_day = $self->sec_in_interval; + my $day = 86400; # 24*60*60 + + my $loop_start_time = int($from/$day)*$day + $day; + my $loop_end_time = int($to/$day)*$day - $day; + + print STDERR "loop (start - end): $loop_start_time - $loop_end_time\n" if ($debug); + + 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); + } + + # add rest of last day + $total -= $self->utpime($to); + print STDERR "total: $total (final)\n" if ($debug); + + return $total; +} + +# +# this function will check if day falls into interval +# + +sub day_in_interval { + my $self = shift; + + my $time = shift || die "need timestamp to check if day is in interval"; + + my @lt = localtime($time); + return $self->_dayOk($lt[6]); +} + +# +# return seconds in defined interval +# + +sub sec_in_interval { + my $self = shift; + + # over midnight? + if ($self->{start} > $self->{end}) { + return(86400 - $self->{start} + $self->{end}); + } else { + return($self->{end} - $self->{start}); + } +} + 1; __END__