CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
unable to frame code with Business:Hours module with example

 



giridhar276
Novice

Jun 18, 2013, 1:09 AM

Post #1 of 4 (261 views)
unable to frame code with Business:Hours module with example Can't Post

Hi all ,

There are two datetimes in the below format

$datetime1="2007-12-31 16:50:00";
$datetime2="2008-1-1 04:24:00";

I need to calculate time in minutes only in between the office hours.(08:00 to 18:00 ). in PERL.

For the above requirement I found a module Business::Hours that calculates the time in seconds after which I can calculate in minutes or hours.

But I am struggling to frame the code using the above module.

Could anyone please explain the module with relevant example?

Thanks in advance....


Code
 use Business::Hours; 
$hours = Business::Hours->new();
# Get a Set::IntSpan of all the business hours in the next week.
# use the default business hours of 9am to 6pm localtime.

my %hours = (
0 => { Name => 'Sunday',
Start => '08:00',
End => '18:00' },

1 => { Name => 'Monday',
Start => '08:00',
End => '18:00' },

2 => { Name => 'Tuesday',
Start => '08:00',
End => '18:00' },

3 => { Name => 'Wednesday',
Start => '08:00',
End => '18:00' },

4 => { Name => 'Thursday',
Start => '08:00',
End => '18:00' },

5 => { Name => 'Friday',
Start => '08:00',
End => '18:00' },

6 => { Name => 'Saturday',
Start => '08:00',
End => '18:00' },
,
);


print $hours->for_timespan( Start => time(), End => time()+(86400*7) );


print $hours->{$0};

print $hours->for_timespan(%hours);


link for the above module in cpan


Code
http://search.cpan.org/dist/Business-Hours/lib/Business/Hours.pm#___top



BillKSmith
Veteran

Jun 18, 2013, 7:40 PM

Post #2 of 4 (245 views)
Re: [giridhar276] unable to frame code with Business:Hours module with example [In reply to] Can't Post

Use the method between. The hardest part is getting start and stop into the required format.

Code
use strict; 
use warnings;
use Business::Hours;
use Time::Local;

my $datetime1="2007-12-31 16:50:00";
my $datetime2="2008-1-1 04:24:00";
my %hours = (
0 => { Name => 'Sunday', Start => '08:00', End => '18:00' },
1 => { Name => 'Monday', Start => '08:00', End => '18:00' },
2 => { Name => 'Tuesday', Start => '08:00', End => '18:00' },
3 => { Name => 'Wednesday', Start => '08:00', End => '18:00' },
4 => { Name => 'Thursday', Start => '08:00', End => '18:00' },
5 => { Name => 'Friday', Start => '08:00', End => '18:00' },
6 => { Name => 'Saturday', Start => '08:00', End => '18:00' },
,
);
my $hours = Business::Hours->new();
$hours->business_hours(%hours);


print $hours->between(
start_end( $datetime1 ),
start_end( $datetime2 ),
)/60;

sub start_end {
# parse start and end strings
my ($string) = @_;
my $parse = qr/^(\d\d\d\d)-(\d\d?)-(\d\d?)\s(\d\d):(\d\d):(\d\d)$/;
my @time = reverse $string=~$parse;
$time[4] -= 1;
$time[5] -= 1900;
return timelocal(@time);
}

Good Luck,
Bill


giridhar276
Novice

Jun 18, 2013, 7:46 PM

Post #3 of 4 (243 views)
Re: [BillKSmith] unable to frame code with Business:Hours module with example [In reply to] Can't Post

Tons of thanks Smith....

COuld you please modify to code for only working days only.
i.e., from monday to friday..

For example

Code
my $datetime1="2013-06-07 17:00:00";   
my $datetime2="2008-06-10 10:00:00";

then output should be only 3 hours(1 hour on friday and 2 hours on monday).

Once again thanks ....



In Reply To



BillKSmith
Veteran

Jun 18, 2013, 8:09 PM

Post #4 of 4 (241 views)
Re: [giridhar276] unable to frame code with Business:Hours module with example [In reply to] Can't Post

The document that you reference in your first post tells how to do this. See the paragraph that describes the hash %hours. Refer perldoc -f undef for instructions on how to use undef.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives