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

I've been asked to create code which would ask for a $start and an $end_date, then work through all the weekdays (preferably US business days) between those two dates.

I have little experience of Date modules, and there are so many. Which one will be best for me?

The person I'm coding this for is on Windows and is remote from me. Date::Calc, which looks like the right sort of thing, has a number of dependencies and uses a C library, is that right? I'm concerned there will be installation issues.



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Which Date Module?
by ikegami (Patriarch) on Jan 20, 2006 at 05:48 UTC

    The following does what you want (safely) using only core modules and core functions:

    use POSIX qw( strftime ); use Time::Local qw( timelocal timelocal_nocheck ); my $start = timelocal(0, 0, 0, (localtime)[3,4,5]); # Today my $end = timelocal(0, 0, 0, 1, 3-1, 2006); # March 1st, 2006 my $date = $start; while ($date <= $end) { my @date = localtime($date); my @tomorrow = @date; $tomorrow[3]++; $date = timelocal_nocheck(@tomorrow); my $wday = $date[6]; next if $wday == 0 || $wday == 6; # Weekend # Date in locale-specific format. print(strftime('%x', @date), "\n"); }

    Output on my system:

    References:
    Time::Local
    POSIX
    localtime

    The code could be optimized to only call localtime and timelocal to find the last day of the month and the first day of next month. However, the added complexity to the code is not likely to be worth the savings.

Re: Which Date Module?
by helphand (Pilgrim) on Jan 20, 2006 at 05:41 UTC

    There shouldn't be any problem installing Date::Calc using ActiveState's ppm, the binary stuff is precompiled and works well on the Windows platform.

    Scott

Re: Which Date Module?
by ambrus (Abbot) on Jan 20, 2006 at 10:20 UTC
Re: Which Date Module?
by pKai (Priest) on Jan 20, 2006 at 11:35 UTC
    Concerning your reservations with respect to Date::Calc: There also is Date::Pcalc which offers the same interface, but is pure perl.
Re: Which Date Module?
by SamCG (Hermit) on Jan 20, 2006 at 15:00 UTC
    I use Date::Calc on Windows XP all the time (perl 5.8) and I work in a pretty restrictive environment (I have to ftp download modules and then install them locally, ppm doesn't work at all for me).
Re: Which Date Module?
by Cody Pendant (Prior) on Jan 20, 2006 at 23:06 UTC
    Thanks everyone. Very useful stuff.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Which Date Module?
by jesuashok (Curate) on Jan 20, 2006 at 05:33 UTC
    Hi

    Use the followings :-

    Date::Calendar Date::Simple Time::Piece

    "Keep pouring your ideas"