/[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.5 by dpavlin, Sun Oct 5 20:55:19 2003 UTC revision 1.7 by dpavlin, Mon Oct 6 09:40:52 2003 UTC
# Line 3  package Time::Available; Line 3  package Time::Available;
3  use 5.001;  use 5.001;
4  use strict;  use strict;
5  use warnings;  use warnings;
6    use Carp;
7    
8  require Exporter;  require Exporter;
9    
# Line 59  sub new { Line 60  sub new {
60          $self->{ARGS} = {@_};          $self->{ARGS} = {@_};
61          $debug = $self->{ARGS}->{DEBUG};          $debug = $self->{ARGS}->{DEBUG};
62    
63          die("need start time") if (! $self->{ARGS}->{start});          croak("need start time") if (! $self->{ARGS}->{start});
64    
65          # calc start and stop seconds          # calc start and stop seconds
66          my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3);          my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3);
67          print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);          print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);
68          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");
69          $s += $mm * 60 if ($mm);          $s += $mm * 60 if ($mm);
70          $s += $ss if ($ss);          $s += $ss if ($ss);
71          $self->{start} = $s;          $self->{start} = $s;
72    
73          die("need end time") if (! $self->{ARGS}->{end});          croak("need end time") if (! $self->{ARGS}->{end});
74    
75          ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3);          ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3);
76          print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);          print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug);
77          $s = $hh * 3600 || die("need at least hour specified for end time");          $s = $hh * 3600 || croak("need at least hour specified for end time");
78          $s += $mm * 60 if ($mm);          $s += $mm * 60 if ($mm);
79          $self->{end} = $s;          $self->{end} = $s;
80    
81          die("need dayMask specified") if (! $self->{ARGS}->{dayMask});          croak("need dayMask specified") if (! $self->{ARGS}->{dayMask});
82    
83          $self->{dayMask} = $self->{ARGS}->{dayMask};          $self->{dayMask} = $self->{ARGS}->{dayMask};
84    
# Line 128  sub _dayOk($) { Line 129  sub _dayOk($) {
129  sub uptime {  sub uptime {
130          my $self = shift;          my $self = shift;
131    
132          my $time = shift || die "need uptime timestamp to calculate uptime";          my $time = shift || croak "need uptime timestamp to calculate uptime";
133    
134          # calculate offset -- that is number of seconds since midnight          # calculate offset -- that is number of seconds since midnight
135          my @lt = localtime($time);          my @lt = gmtime($time);
136    
137            # check if day falls into dayMask
138            return 0 if (! $self->_dayOk($lt[6]) );
139    
140          my $offset = $lt[2];    # hour          my $offset = $lt[2];    # hour
141          $offset *= 60;          # convert to minutes          $offset *= 60;          # convert to minutes
142          $offset += $lt[1];      # minutes          $offset += $lt[1];      # minutes
143          $offset *= 60;          # convert to seconds          $offset *= 60;          # convert to seconds
144          $offset += $lt[0];          $offset += $lt[0];
145    
         # check if day falls into dayMask  
         return 0 if (! $self->_dayOk($lt[6]) );  
   
146          my $s=0;          my $s=0;
147    
148          my $start = $self->{start};          my $start = $self->{start};
149          my $end = $self->{end};          my $end = $self->{end};
150    
151          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);
152    
153          if ( $end > $start ) {          if ( $end > $start ) {
154                  if ($offset < $start) {                  if ($offset < $start) {
# Line 174  sub uptime { Line 176  sub uptime {
176  }  }
177    
178  #  #
179    # this will return number of seconds that service is available if passed
180    # downtime of service
181    #
182    
183    sub downtime {
184            my $self = shift;
185    
186            my $time = shift || croak "need downtime timestamp to calculate uptime";
187    
188            # calculate offset -- that is number of seconds since midnight
189            my @lt = gmtime($time);
190    
191            # check if day falls into dayMask
192            return 0 if (! $self->_dayOk($lt[6]) );
193    
194            my $offset = $lt[2];    # hour
195            $offset *= 60;          # convert to minutes
196            $offset += $lt[1];      # minutes
197            $offset *= 60;          # convert to seconds
198            $offset += $lt[0];
199    
200            my $s=0;
201    
202            my $start = $self->{start};
203            my $end = $self->{end};
204    
205            print STDERR "start: $start end: $end time: $offset [$lt[2]:$lt[1]:$lt[0]]\n" if ($debug);
206    
207            if ( $end > $start ) {
208                    if ($offset > $start && $offset <= $end) {
209                            $s = $end - $offset;
210                    } elsif ($offset < $start) {
211                            $s = $end - $start;
212                    }
213            } elsif ( $start > $end ) {     # over midnight
214                    if ( $offset < $end ) {
215                            if ( $offset < $start) {
216                                    $s = $offset;
217                            } else {
218                                    $s = 0;
219                            }
220                    } else {
221                            if ( $offset < $start ) {
222                                    $s = SEC_PER_DAY - $end;
223                            } else {
224                                    $s = SEC_PER_DAY - $end + $start - $offset;
225                            }
226                    }
227            }
228                    
229            return $s;
230    }
231    
232    #
233  # 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
234  #  #
235    
# Line 201  sub fmt_interval { Line 257  sub fmt_interval {
257    
258  sub interval {  sub interval {
259          my $self = shift;          my $self = shift;
260          my $from = shift || die "need start time for interval";          my $from = shift || croak "need start time for interval";
261          my $to = shift || die "need end time for interval";          my $to = shift || croak "need end time for interval";
262    
263          print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug);          print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug);
264          print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug);          print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug);
# Line 210  sub interval { Line 266  sub interval {
266          my $total = 0;          my $total = 0;
267    
268          # calc first day availability          # calc first day availability
269            print STDERR "t:\t$from\t",scalar gmtime($from),"\n" if ($debug);
270          $total += $self->uptime($from);          $total += $self->uptime($from);
271    
272          print STDERR "total: $total\n" if ($debug);          print STDERR "total: $total (first)\n" if ($debug);
273    
274          # add all whole days          # add all whole days
275    
# Line 227  sub interval { Line 284  sub interval {
284          for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) {          for (my $t = $loop_start_time; $t <= $loop_end_time; $t += $day) {
285                  print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug);                  print STDERR "t:\t$t\t",scalar gmtime($t),"\n" if ($debug);
286                  $total += $sec_in_day if ($self->day_in_interval($t));                  $total += $sec_in_day if ($self->day_in_interval($t));
287                  print STDERR "total: $total\n" if ($debug);                  print STDERR "total: $total (loop)\n" if ($debug);
288          }          }
289    
290          # add rest of last day          # add rest of last day
291          $total -= $self->utpime($to);          print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug);
292    
293            $total = abs($total - $self->downtime($to));
294          print STDERR "total: $total (final)\n" if ($debug);          print STDERR "total: $total (final)\n" if ($debug);
295    
296          return $total;          return $total;
# Line 244  sub interval { Line 303  sub interval {
303  sub day_in_interval {  sub day_in_interval {
304          my $self = shift;          my $self = shift;
305    
306          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";
307    
308          my @lt = localtime($time);          my @lt = gmtime($time);
309          return $self->_dayOk($lt[6]);          return $self->_dayOk($lt[6]);
310  }  }
311    
# Line 371  Original version; based somewhat on Time Line 430  Original version; based somewhat on Time
430  =over 8  =over 8
431    
432  =item *  =item *
 Use croak and not die in module for better error handling  
   
 =item *  
433  Allow arbitary (array?) of holidays to be included.  Allow arbitary (array?) of holidays to be included.
434    
435  =back  =back

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

  ViewVC Help
Powered by ViewVC 1.1.26