Solved

Calculating Business days in Perl

Posted on 2000-04-27
5
678 Views
Last Modified: 2012-08-13
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
Comment
Question by:lsellers
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2758123
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
 
LVL 84

Expert Comment

by:ozo
ID: 2758159
You'll also need a configuration file defining your holidays.
0
 
LVL 3

Accepted Solution

by:
tgoetze earned 125 total points
ID: 2758313
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
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2763167
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
 

Author Comment

by:lsellers
ID: 2766416
Thank you very much!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now