[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

Calculating Business days in Perl

Can someone please help me with the code in Perl to calculate the number of business days (excluding weekends) between two days entered by the user?
Any help would be appreciated.

Thank you.
0
lsellers
Asked:
lsellers
1 Solution
 
Kim RyanIT ConsultantCommented:
You will need to download and install a CPAN module called Date::Manip for www.cpan.org

use Date::Manip;
$date1=&ParseDate($string1);
$date2=&ParseDate($string2);
$mode =2; # business mode
$diff=&DateCalc($date1,$date2\$err,$mode]);

0
 
ozoCommented:
You'll also need a configuration file defining your holidays.
0
 
tgoetzeCommented:
With no error checking and ignoring holidays, here is a solution:

$from = "4/24/2000";
$to = "4/27/2001";

$num_business_days = business_days($from, $to);
print "There are $num_business_days business days between $from and $to\n";

sub business_days {
    @from = parse_date($_[0]);
    @to = parse_date($_[1]);
    $days_apart = ($to[9] - $from[9])/(60*60*24);
    if ($from[6] > $to[6]) {
        $days_apart -= 2 * ((int $days_apart/7) + 1);
    } else {
        $days_apart -= 2 * (int $days_apart/7);
    }
    return $days_apart;
}

sub parse_date {
    my @F = split(/\//, $_[0]);
    my $f = timelocal(0, 0, 0, $F[1], $F[0] - 1, $F[2] - 1900);
    return localtime($f), $f;
}
0
 
Kim RyanIT ConsultantCommented:
Date::Manip is a very comprehensive module. By using it you are making use of years of testing and development. Modules save you time by not having to reinvent the wheel.
0
 
lsellersAuthor Commented:
Thank you very much!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now