/[Time-Available]/Available.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Available.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by dpavlin, Sun Oct 5 19:06:44 2003 UTC revision 1.5 by dpavlin, Sun Oct 5 20:55:19 2003 UTC
# Line 63  sub new { Line 63  sub new {
63    
64          # calc start and stop seconds          # calc start and stop seconds
65          my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3);          my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3);
66            print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);
67          my $s = $hh * 3600 || die("need at least hour specified for start time");          my $s = $hh * 3600 || die("need at least hour specified for start time");
68          $s += $mm * 60 if ($mm);          $s += $mm * 60 if ($mm);
69          $s += $ss if ($ss);          $s += $ss if ($ss);
# Line 71  sub new { Line 72  sub new {
72          die("need end time") if (! $self->{ARGS}->{end});          die("need end time") if (! $self->{ARGS}->{end});
73    
74          ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3);          ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3);
75            print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);
76          $s = $hh * 3600 || die("need at least hour specified for end time");          $s = $hh * 3600 || die("need at least hour specified for end time");
77          $s += $mm * 60 if ($mm);          $s += $mm * 60 if ($mm);
78          $self->{end} = $s;          $self->{end} = $s;
# Line 126  sub _dayOk($) { Line 128  sub _dayOk($) {
128  sub uptime {  sub uptime {
129          my $self = shift;          my $self = shift;
130    
131          my $time = shift || die "need uptime timestamp to calcualte 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 = localtime($time);
# Line 193  sub fmt_interval { Line 195  sub fmt_interval {
195          return $out;          return $out;
196  }  }
197    
198    #
199    # this function will calculate uptime for some interval
200    #
201    
202    sub interval {
203            my $self = shift;
204            my $from = shift || die "need start time for interval";
205            my $to = shift || die "need end time for interval";
206    
207            print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug);
208            print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug);
209    
210            my $total = 0;
211    
212            # calc first day availability
213            $total += $self->uptime($from);
214    
215            print STDERR "total: $total\n" if ($debug);
216    
217            # add all whole days
218    
219            my $sec_in_day = $self->sec_in_interval;
220            my $day = 86400;        # 24*60*60
221    
222            my $loop_start_time = int($from/$day)*$day + $day;
223            my $loop_end_time = int($to/$day)*$day - $day;
224    
225            print STDERR "loop (start - end): $loop_start_time - $loop_end_time\n" if ($debug);
226    
227            for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) {
228                    print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug);
229                    $total += $sec_in_day if ($self->day_in_interval($t));
230                    print STDERR "total: $total\n" if ($debug);
231            }
232    
233            # add rest of last day
234            $total -= $self->utpime($to);
235            print STDERR "total: $total (final)\n" if ($debug);
236    
237            return $total;
238    }
239    
240    #
241    # this function will check if day falls into interval
242    #
243    
244    sub day_in_interval {
245            my $self = shift;
246    
247            my $time = shift || die "need timestamp to check if day is in interval";
248    
249            my @lt = localtime($time);
250            return $self->_dayOk($lt[6]);
251    }
252    
253    #
254    # return seconds in defined interval
255    #
256    
257    sub sec_in_interval {
258            my $self = shift;
259    
260            # over midnight?
261            if ($self->{start} > $self->{end}) {
262                    return(86400 - $self->{start} + $self->{end});
263            } else {
264                    return($self->{end} - $self->{start});
265            }
266    }
267    
268  1;  1;
269  __END__  __END__
270    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.26