bar10der has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am developing a web based tool using CGI-Application and Html:Tempalte and Oracle database. One of my requirement is to count number of working days between two given dates. I want to know if there any perl module which I can use to calculate above. Using sql, I can calculate number of days between two given dates but don't know how to avoid weekends. Any help / guidance will be greatly appreciated.

Thanks

Replies are listed 'Best First'.
Re: counting number of working days
by edan (Curate) on Feb 16, 2004 at 10:09 UTC
      I am new to perl programming so with my little knowledge I tried to use Date::Calendar->delta_workdays() but I am getting errors which I am not able to usnderstand. In first case when I use following code -
      ----

      #!perl -w
      use Date::Calendar;
      use Date::Calendar::Profiles qw( $Profiles );
      my $calendar = Date::Calendar->new();
      #use strict;
      use warnings;
      my $date1='16-02-2004';
      my $date2='23-02-2004';
      my $days = $calendar->delta_workdays($date1,$date2,0,0); print "count is $days\n";
      ----
      --
      I am getting following error -
      Date::Calendar::delta_workdays(): not enough input parameters for a date at test.pl line 9

      When I read the document, it seemsthe format in which I am supplying vars are valid

        I have never used the modules I mentioned - I just know that other people have suggested using them for their Date/Time processing needs. But since you asked, I installed them, and threw the following together, which works:

        #!/usr/bin/perl -l use Date::Calendar::Profiles qw( $Profiles ); use Date::Calendar; my $cal = Date::Calendar->new( $Profiles->{US} ) or die "no calendar\n"; my $days = $cal->delta_workdays( 2004, 2, 11, # first date 2004, 2, 18, # second date 1, # include first date 0); # exclude second date print "days: $days";

        The example prints the number of US workdays from Feb. 11, 2004 to Feb. 18, 2004, which is 4 (Presidents Day!).

        HTH
        --
        edan (formerly known as 3dan)

Re: counting number of working days
by Corion (Patriarch) on Feb 16, 2004 at 10:08 UTC

    For getting at work days, you will need a list of holidays as well. If you already have Oracle (or any other database), I suggest setting up a table that lists the work-free days (for your business definition of work-free), and then use SQL to get at the number of holidays:

    select count(*) from holidays where (day >= ?) and (day < ?)

    passing in the start and end date of the range. The number of workdays in the range is then the difference of the number of days and the number of holidays in the range.

    Of course, you will also need to insert the weekends into that table, but DateTime or any other simple Perl script looking at the return values from Time::Local should do that for you.

    If you want to avoid the DATE type (and for Oracle, depending how much control you have over the database setup, you might want to), be sure to use strings or numbers for the date that compare well, preferably using the format as returned by strftime('%Y%m%d', localtime).

Re: counting number of working days
by dws (Chancellor) on Feb 16, 2004 at 23:00 UTC
    One of my requirement is to count number of working days between two given dates.

    Since "working days" can be a substantial headache to define algorithmically, one brute-force method is to create a table (in Oracle, in your case) that stores attributes for dates. It's ugly, but it works, and it lets you do things like

    SELECT COUNT(*) FROM dates WHERE dates.ymd > ? AND dates.ymd < ? AND dates.isWorkingDay = 1
    This approach lets you have columns for stuff like
    • isBankHolidayUS
    • isPostalHolidayUS # since this might be different
    • isBankHolidayUK
    • isPostalHolidayUK
    • ... etc.

    The downside of this approach is performance. The upside is that the table is fairly small, and once you've got it populated, everyone who cares gets consistent results. It's also possible to extend the table by adding columns.

    Those who've done any data warehousing (quick intro here) will recognize this as a dimension table.

Re: counting number of working days
by bar10der (Beadle) on Feb 16, 2004 at 14:31 UTC
    Edan, it was really very nice of you and I appreciate your help. It does work. Since I am in uk, I need to create my own holiday profile but in principle I do understand this part of the module and sure I can find my way out now.

    Thanks again

      No problem.

      You might want to look at the GB profile (e.g. $Profiles->{GB})

      I hope it works for you, or you succeed in 'rolling your own'.

      --
      edan (formerly known as 3dan)