--- Available.pm 2003/10/03 14:19:50 1.1 +++ Available.pm 2003/10/03 16:40:00 1.3 @@ -1,6 +1,6 @@ package Time::Available; -use 5.008001; +use 5.001; use strict; use warnings; @@ -8,13 +8,6 @@ our @ISA = qw(Exporter); -# 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. our %EXPORT_TAGS = ( 'days' => [ qw( DAY_MONDAY @@ -27,14 +20,16 @@ DAY_WEEKDAY DAY_WEEKEND DAY_EVERYDAY - ) ] + ) ], + 'fmt_interval' => [ qw(fmt_interval) ] ); -our @EXPORT_OK = ( @{ $EXPORT_TAGS{'days'} } ); +our @EXPORT_OK = ( + @{ $EXPORT_TAGS{'days'} }, + @{ $EXPORT_TAGS{'fmt_interval'} } + ); -our @EXPORT = qw( - -); +our @EXPORT; # don't export anything by default! our $VERSION = '0.01'; @@ -93,9 +88,11 @@ # this sub (originally from Time::Avail) will return if day is applicable # -sub _dayOk($$) { +sub _dayOk($) { + my $self = shift; + my $day = shift || return; - my( $dayMask, $day ) = @_; # get parameters + my $dayMask = $self->{dayMask}; my $dayOk = 0; @@ -131,31 +128,42 @@ my $time = shift || die "need uptime timestamp to calcualte uptime"; + # calculate offset -- that is number of seconds since midnight + my @lt = localtime($time); + my $offset = $lt[2]; # hour + $offset *= 60; # convert to minutes + $offset += $lt[1]; # minutes + $offset *= 60; # convert to seconds + $offset += $lt[0]; + + # check if day falls into dayMask + return 0 if (! $self->_dayOk($lt[6]) ); + my $s=0; my $start = $self->{start}; my $end = $self->{end}; - print STDERR "start: $start end: $end time: $time\n" if ($debug); + print STDERR "start: $start end: $end time: $offset\n" if ($debug); - if( ( $end > $start ) && ( $time < $end ) ) { - if ($time < $start) { + if ( $end > $start ) { + if ($offset < $start) { $s = $end - $start; - } else { - $s = $end - $time; + } elsif ($offset < $end) { + $s = $end - $offset; } - } elsif( $start > $end ) { # over midnight - if ( $time < $end ) { - if ( $time < $start) { - $s = SEC_PER_DAY - $start + $end - $time; + } elsif ( $start > $end ) { # over midnight + if ( $offset < $end ) { + if ( $offset < $start) { + $s = SEC_PER_DAY - $start + $end - $offset; } else { $s = SEC_PER_DAY - $start + $end; } } else { - if ( $time < $start ) { + if ( $offset < $start ) { $s = SEC_PER_DAY - $start; } else { - $s = SEC_PER_DAY - $time; + $s = SEC_PER_DAY - $offset; } } } @@ -163,7 +171,27 @@ return $s; } +# +# this auxillary function will pretty-format interval in [days]d hh:mm:ss +# +sub fmt_interval { + my $s = shift || 0; + my $out = ""; + + my $d = int($s/(24*60*60)); + $s = $s % (24*60*60); + my $h = int($s/(60*60)); + $s = $s % (60*60); + my $m = int($s/60); + $s = $s % 60; + + $out .= $d."d " if ($d > 0); + + $out .= sprintf("%02d:%02d:%02d",$h,$m,$s); + + return $out; +} 1; __END__ @@ -191,6 +219,10 @@ # calculate availablity in seconds from interval of uptime print $interval->interval($utime1,$utime2); + # pretty print interval data (this will produce output '1d 11:11:11') + use Time::Available qw(:fmt_interval); + print fmt_interval(126671); + =head1 DESCRIPTION Time::Available is used to calculate availability of some resource if start @@ -242,10 +274,15 @@ =head2 EXPORT -None by default. If you specify B<:days>, Time::Available will export all +None by default. + +If you specify B<:days>, Time::Available will export all DAY_* constraints to your enviroment (causing possible pollution of name space). You have been warned. +With B<:fmt_interval> it will include function B which will +pretty-format interval into [days]d hh:mm:ss. + =head1 HISTORY @@ -276,7 +313,7 @@ this module was born. More information about this module might be found on -http://www.rot13.org/~dpavlin/perl.html#cpan +http://www.rot13.org/~dpavlin/projects.html#cpan =head1 AUTHOR