CC: | jesse [...] cpan.org |
Subject: | Modification to Business::Hours (added sub_seconds()) |
Date: | Wed, 30 May 2007 11:44:35 -0700 |
To: | bug-business-hours [...] rt.cpan.org |
From: | Gene LeDuc <gleduc [...] mail.sdsu.edu> |
Not a bug, but a suggested addition to the Business::Hours package.
I needed to be able to count backwards from a specific date instead of just
forwards and couldn't figure out a way to do it using the Business::Hours
module, so I used the add_seconds() method as a model and wrote its
complement, sub_seconds(). The role it fills is in allowing me to
determine whether I'm within $x business-seconds of a future due date. I
use it in a cron script to decide whether it's OK to nag the owner/adminccs
when an RT ticket is approaching its due date rather than wait for the
ticket to become overdue.
I've never written a package, class, or anything remotely complex in perl,
so it might not be quite right (but it works, so I think it is).
Anyway, here it is if you'd like to add it to Business::Hours.
Usage is:
use Business::Hours;
my $hours = Business::Hours->new();
### $nagdate will be {$seconds} business seconds before {$start}
my $nagdate = $hours->sub_seconds($start, $seconds);
===== begin code
{ package Business::Hours;
=head2 sub_seconds START, SECONDS
Returns a time SECONDS business seconds before START.
START should be specified in Seconds since the Epoch.
If it can't find any business hours within thirty days, returns -1.
=cut
sub sub_seconds {
my $self = shift;
my $start = shift;
my $seconds = shift;
# the maximum time after which we stop searching for business hours
my $MAXTIME = (30 * 24 * 60 * 60); # 30 days
my $first;
my $period = (24 * 60 * 60);
my $begin = $start - $period;
my $hours = new Set::IntSpan;
while ($hours->empty or $self->between($hours->first, $start) <=
$seconds)
{
if ($begin <= $start - $MAXTIME) {
return -1;
}
$hours = $self->for_timespan(Start => $begin, End => $start);
$begin -= $period;
}
my @elements = reverse elements $hours;
$first = $elements[$seconds];
return $first;
}
===== end code
Regards,
Gene
--
Gene LeDuc, GSEC
Security Analyst
San Diego State University