[rt-users] Business Hours help please
Gene LeDuc
gleduc at mail.sdsu.edu
Tue Dec 4 12:40:26 EST 2007
Hi Roy,
I needed a way to subtract business hours from a timestamp so I added my
own sub_seconds() method to the package. I'm not sure if I ever posted it
to the wiki, so here it is. Just include this code at the end of your
program and call it like you would the add_seconds() method. I sent the
code to the business::hours authors but I never heard anything back so I
guess it was not included.
For your purposes I think you would just assign the larger of the
timestamps to a Business::Hours object and then subtract the smaller
timestamp from it using this method.
### Need to add this to Business::Hours so I can count backwards from due date
{ package Business::Hours;
sub sub_seconds {
### This method is hacked version of add_seconds(), written by Gene
LeDuc
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;
}
1; #this line is important and will help the module return a true value
}
Regards,
Gene
At 08:25 AM 12/4/2007, Roy El-Hames wrote:
>Hi Mark;
>
>Thanks for your reply, I am looking for the business hours/ or seconds
>between 2 time stamps I had a look at the cpan page before I emailed the
>list but unfortuantely I either did not understand it or missing something
>.. I also tried the between routine and can't figure it out,
>I was hoping someone have actually used this module similar to the
>calculation I need .. which is basically how long in working hours did the
>ticket last from open to close ..
>
>Regards;
>Roy
>
>
>Roedel, Mark wrote:
>>You don't say exactly what you're looking for as output. As noted in
>>the Business::Hours documentation, for_timespan returns a Set::IntSpan,
>>which is what you're seeing in your dump. Just eyeballing things, it
>>looks like the 'edges' array contains Unix timestamps corresponding to
>>the beginnings and ends of the ranges of time between your start and end
>>which fall within the business hours you defined.
>>http://search.cpan.org/dist/Set-IntSpan/IntSpan.pm has more information
>>on working with a Set::IntSpan.
>>
>>If you're just trying to get the number of business hours that occur
>>within your range, Business::Hours does also have a "between" subroutine
>>that returns the number of business seconds between two timestamps.
>>(See http://search.cpan.org/dist/Business-Hours/lib/Business/Hours.pm
>>for full information on Business::Hours.)
>>
>>
>>--
>>Mark Roedel
>>Senior Programmer / Analyst
>>LeTourneau University
>>Longview, Texas USA
>>
>>
>>
>>-----Original Message-----
>>From: rt-users-bounces at lists.bestpractical.com
>>[mailto:rt-users-bounces at lists.bestpractical.com] On Behalf Of Roy
>>El-Hames
>>Sent: Tuesday, December 04, 2007 7:27 AM
>>To: RT Users
>>Subject: [rt-users] Business Hours help please
>>
>>Hi ;
>>
>>Can anyone help me with the code of how to determine the Business Hours
>>between 2 dates:
>>What I got :
>>my $hours = Business::Hours->new();
>>$hours->business_hours(
>> 0 => { Name => 'Sunday', Start => undef, End => undef },
>> 1 => { Name => 'Monday', Start => '09:00', End => '17:30' },
>> 2 => { Name => 'Tuesday', Start => '09:00', End => '17:30' },
>> 3 => { Name => 'Wednesday', Start => '09:00', End => '17:30' },
>> 4 => { Name => 'Thursday', Start => '09:00', End => '17:30' },
>> 5 => { Name => 'Friday', Start => '09:00', End => '17:30' },
>> 6 => { Name => 'Saturday', Start => undef, End => undef }
>> );
>>my $wtime = $hours->for_timespan(Start => time(), End =>
>>time()+(86400*7));
>>my $splosh = Dumper($wtime) ;
>>
>>print "$splosh nnn\n";
>>
>>I get :
>>$VAR1 = bless( {
>> 'negInf' => 0,
>> 'edges' => [
>> 1196774745,
>> 1196789399,
>> 1196845199,
>> 1196875799,
>> 1196931599,
>> 1196962199,
>> 1197017999,
>> 1197048599,
>> 1197277199,
>> 1197307799,
>> 1197363599,
>> 1197379546
>> ],
>> 'posInf' => 0,
>> 'empty_string' => \'-'
>> }, 'Set::IntSpan' );
>> nnn
>>
>>
>>
>
>_______________________________________________
>http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-users
>
>SAVE THOUSANDS OF DOLLARS ON RT SUPPORT:
>
>If you sign up for a new RT support contract before December 31, we'll take
>up to 20 percent off the price. This sale won't last long, so get in touch
>today. Email us at sales at bestpractical.com or call us at +1 617 812 0745.
>
>
>Community help: http://wiki.bestpractical.com
>Commercial support: sales at bestpractical.com
>
>
>Discover RT's hidden secrets with RT Essentials from O'Reilly Media. Buy a
>copy at http://rtbook.bestpractical.com
--
Gene LeDuc, GSEC
Security Analyst
San Diego State University
More information about the rt-users
mailing list