/[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.1 by dpavlin, Fri Oct 3 14:19:50 2003 UTC revision 1.4 by dpavlin, Sun Oct 5 19:06:44 2003 UTC
# Line 1  Line 1 
1  package Time::Available;  package Time::Available;
2    
3  use 5.008001;  use 5.001;
4  use strict;  use strict;
5  use warnings;  use warnings;
6    
# Line 8  require Exporter; Line 8  require Exporter;
8    
9  our @ISA = qw(Exporter);  our @ISA = qw(Exporter);
10    
 # Items to export into callers namespace by default. Note: do not export  
 # names by default without a very good reason. Use EXPORT_OK instead.  
 # Do not simply export all your public functions/methods/constants.  
   
 # This allows declaration       use Time::Available ':all';  
 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK  
 # will save memory.  
11  our %EXPORT_TAGS = (  our %EXPORT_TAGS = (
12          'days' => [ qw(          'days' => [ qw(
13                  DAY_MONDAY                  DAY_MONDAY
# Line 27  our %EXPORT_TAGS = ( Line 20  our %EXPORT_TAGS = (
20                  DAY_WEEKDAY                  DAY_WEEKDAY
21                  DAY_WEEKEND                  DAY_WEEKEND
22                  DAY_EVERYDAY                  DAY_EVERYDAY
23          ) ]          ) ],
24            'fmt_interval' => [ qw(fmt_interval) ]
25  );  );
26    
27  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'days'} } );  our @EXPORT_OK = (
28            @{ $EXPORT_TAGS{'days'} },
29            @{ $EXPORT_TAGS{'fmt_interval'} }
30            );
31    
32  our @EXPORT = qw(  our @EXPORT;    # don't export anything by default!
           
 );  
33    
34  our $VERSION = '0.01';  our $VERSION = '0.01';
35    
# Line 93  sub new { Line 88  sub new {
88  # this sub (originally from Time::Avail) will return if day is applicable  # this sub (originally from Time::Avail) will return if day is applicable
89  #  #
90    
91  sub _dayOk($$) {  sub _dayOk($) {
92            my $self = shift;
93            my $day = shift || return;
94    
95          my( $dayMask, $day ) = @_;      # get parameters          my $dayMask = $self->{dayMask};
96    
97          my $dayOk = 0;          my $dayOk = 0;
98    
# Line 131  sub uptime { Line 128  sub uptime {
128    
129          my $time = shift || die "need uptime timestamp to calcualte uptime";          my $time = shift || die "need uptime timestamp to calcualte uptime";
130    
131            # calculate offset -- that is number of seconds since midnight
132            my @lt = localtime($time);
133            my $offset = $lt[2];    # hour
134            $offset *= 60;          # convert to minutes
135            $offset += $lt[1];      # minutes
136            $offset *= 60;          # convert to seconds
137            $offset += $lt[0];
138    
139            # check if day falls into dayMask
140            return 0 if (! $self->_dayOk($lt[6]) );
141    
142          my $s=0;          my $s=0;
143    
144          my $start = $self->{start};          my $start = $self->{start};
145          my $end = $self->{end};          my $end = $self->{end};
146    
147          print STDERR "start: $start end: $end time: $time\n" if ($debug);          print STDERR "start: $start end: $end time: $offset\n" if ($debug);
148    
149          if( ( $end > $start ) && ( $time < $end ) ) {          if ( $end > $start ) {
150                  if ($time < $start) {                  if ($offset < $start) {
151                          $s = $end - $start;                          $s = $end - $start;
152                  } else {                  } elsif ($offset < $end) {
153                          $s = $end - $time;                          $s = $end - $offset;
154                  }                  }
155          } elsif( $start > $end ) {      # over midnight          } elsif ( $start > $end ) {     # over midnight
156                  if ( $time < $end ) {                  if ( $offset < $end ) {
157                          if ( $time < $start) {                          if ( $offset < $start) {
158                                  $s = SEC_PER_DAY - $start + $end - $time;                                  $s = SEC_PER_DAY - $start + $end - $offset;
159                          } else {                          } else {
160                                  $s = SEC_PER_DAY - $start + $end;                                  $s = SEC_PER_DAY - $start + $end;
161                          }                          }
162                  } else {                  } else {
163                          if ( $time < $start ) {                          if ( $offset < $start ) {
164                                  $s = SEC_PER_DAY - $start;                                  $s = SEC_PER_DAY - $start;
165                          } else {                          } else {
166                                  $s = SEC_PER_DAY - $time;                                  $s = SEC_PER_DAY - $offset;
167                          }                          }
168                  }                  }
169          }          }
# Line 163  sub uptime { Line 171  sub uptime {
171          return $s;          return $s;
172  }  }
173    
174    #
175    # this auxillary function will pretty-format interval in [days]d hh:mm:ss
176    #
177    
178    sub fmt_interval {
179            my $s = shift || 0;
180            my $out = "";
181    
182            my $d = int($s/(24*60*60));
183            $s = $s % (24*60*60);
184            my $h = int($s/(60*60));
185            $s = $s % (60*60);
186            my $m = int($s/60);
187            $s = $s % 60;
188            
189            $out .= $d."d " if ($d > 0);
190    
191            $out .= sprintf("%02d:%02d:%02d",$h,$m,$s);
192    
193            return $out;
194    }
195    
196  1;  1;
197  __END__  __END__
# Line 191  Time::Available - Perl extension to calc Line 219  Time::Available - Perl extension to calc
219    # calculate availablity in seconds from interval of uptime    # calculate availablity in seconds from interval of uptime
220    print $interval->interval($utime1,$utime2);    print $interval->interval($utime1,$utime2);
221    
222      # pretty print interval data (this will produce output '1d 11:11:11')
223      use Time::Available qw(:fmt_interval);
224      print fmt_interval(126671);
225    
226  =head1 DESCRIPTION  =head1 DESCRIPTION
227    
228  Time::Available is used to calculate availability of some resource if start  Time::Available is used to calculate availability of some resource if start
# Line 207  the following dayMask constants: Line 239  the following dayMask constants:
239  =over 4  =over 4
240    
241  =item *  =item *
242  Time::Avail::DAY_MONDAY  Time::Available::DAY_MONDAY
243    
244  =item *  =item *
245  Time::Avail::DAY_TUESDAY  Time::Available::DAY_TUESDAY
246    
247  =item *  =item *
248  Time::Avail::DAY_WEDNESDAY  Time::Available::DAY_WEDNESDAY
249    
250  =item *  =item *
251  Time::Avail::DAY_THURSDAY  Time::Available::DAY_THURSDAY
252    
253  =item *  =item *
254  Time::Avail::DAY_FRIDAY  Time::Available::DAY_FRIDAY
255    
256  =item *  =item *
257  Time::Avail::DAY_SATURDAY  Time::Available::DAY_SATURDAY
258    
259  =item *  =item *
260  Time::Avail::DAY_SUNDAY  Time::Available::DAY_SUNDAY
261    
262  =item *  =item *
263  Time::Avail::DAY_WEEKDAY  Time::Available::DAY_WEEKDAY
264    
265  =item *  =item *
266  Time::Avail::DAY_WEEKEND  Time::Available::DAY_WEEKEND
267    
268  =item *  =item *
269  Time::Avail::DAY_EVERYDAY  Time::Available::DAY_EVERYDAY
270    
271  =back  =back
272    
# Line 242  FIXME Line 274  FIXME
274    
275  =head2 EXPORT  =head2 EXPORT
276    
277  None by default. If you specify B<:days>, Time::Available will export all  None by default.
278    
279    If you specify B<:days>, Time::Available will export all
280  DAY_* constraints to your enviroment (causing possible pollution of name  DAY_* constraints to your enviroment (causing possible pollution of name
281  space). You have been warned.  space). You have been warned.
282    
283    With B<:fmt_interval> it will include function B<fmt_interval> which will
284    pretty-format interval into [days]d hh:mm:ss.
285    
286    
287  =head1 HISTORY  =head1 HISTORY
288    
# Line 276  calculating of availability of some inte Line 313  calculating of availability of some inte
313  this module was born.  this module was born.
314    
315  More information about this module might be found on  More information about this module might be found on
316  http://www.rot13.org/~dpavlin/perl.html#cpan  http://www.rot13.org/~dpavlin/projects.html#cpan
317    
318  =head1 AUTHOR  =head1 AUTHOR
319    

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

  ViewVC Help
Powered by ViewVC 1.1.26