in reply to Getting Weekly Dates

Eww. I don't think you should use Date::Calc or Date::Manip for this. I think you should use Time::Local.
use Time::Local; sub getFirstNonWeekend { my ($month, $year) = @_; # (7,2001) for July 2001 # noon on the 1st of the month my $first = timelocal(0,0,12, 1, $month-1, $year-1900); my $shift = ((localtime $first)[6] + 1) % 7; if ($shift < 2) { $first += 86400 * (2 - $shift) if $shift < 2; return ($first, 3 - $shift, 1); } return ($first, 1, $shift - 1); }
That will give you the time for (around) noon on the first day of a given month that is NOT a weekend, the day of the month, and a number representing the day of the week (1 = Monday, 5 = Friday). That's probably the hardest part.

What's left is the partitioning of this month and the next. Since you know the day of the month and the day of the week, this shouldn't be too hard. I wouldn't mind showing you the code for it though, if you ask.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Getting Weekly Dates
by petral (Curate) on Mar 09, 2001 at 06:05 UTC
    Or, as a 'one-liner':
    perl -MTime::Local -wle ' for ($x = timelocal(0,0,0,1,0,2001); $x < timelocal(0,0,0,1,0,2002) +; $x += 24*60*60) { (localtime($x))[6] % 6 and print substr(localtime($x),0,10) }'
    Very neat!

    p
      Wow. Succinct, beautiful, and far less involving. I'm impressed. But you might want to use (0,0,12) instead of (0,0,0) for the time -- just in case you run into the daylight savings time switch.

      japhy -- Perl and Regex Hacker
        So _that's_ why you used noon. Good point.
        (You were trying to actually be useful, always a complicating factor.)

        p
Re: Re: Getting Weekly Dates
by japhy (Canon) on Mar 09, 2001 at 02:02 UTC
    Under request of mothra, I'm posting my code to show the partitioning of a month into weekday weeks. It is not the entire solution to the problem, since mothra wants two-month spans, but that can be done with little effort. It's in the Code Catacombs, Week Partitioner.

    japhy -- Perl and Regex Hacker