/[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.6 by dpavlin, Sun Oct 5 22:26:54 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 = gmtime($time);          my @lt = gmtime($time);
# Line 182  sub uptime { Line 183  sub uptime {
183  sub downtime {  sub downtime {
184          my $self = shift;          my $self = shift;
185    
186          my $time = shift || die "need downtime timestamp to calculate uptime";          my $time = shift || croak "need downtime timestamp to calculate uptime";
187    
188          # calculate offset -- that is number of seconds since midnight          # calculate offset -- that is number of seconds since midnight
189          my @lt = gmtime($time);          my @lt = gmtime($time);
# Line 256  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 289  sub interval { Line 290  sub interval {
290          # add rest of last day          # add rest of last day
291          print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug);          print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug);
292    
293          $total -= $self->downtime($to);          $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 302  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 = gmtime($time);          my @lt = gmtime($time);
309          return $self->_dayOk($lt[6]);          return $self->_dayOk($lt[6]);
# Line 429  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.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.26