Here's the function library:
# monthutils.pl my @dow = qw( Sun Mon Tue Wed Thu Fri Sat ); my @dom = (31,28,31,30,31,30,31,31,30,31,30,31); my %feb; # for caching feburary dates sub getFirstNonWeekend { my ($month, $year) = @_; 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); } sub partitionWeekdays { my ($month, $year, $dom, $dow) = @_; my $last = lastDay($month,$year); my @weeks; # week is completed in the month while ($dom + 4 <= $last) { push @weeks, [ $dom .. ($dom + 5 - $dow) ]; $dom += 8 - $dow; $dow = 1; } # month ends before week push @weeks, [ $dom .. $last ] if $dom <= $last; return @weeks; } sub lastDay { my ($m,$y) = @_; return $dom[$m-1] if $m != 2; return $feb{$y} if $feb{$y}; return $feb{$y} = 29 if $y % 4 == 0 and ($y % 100 or $y % 400 == 0); return $feb{$y} = 28; } 1;
And here's the program:
#!/usr/bin/perl -w use Time::Local; use strict; require "monthutils.pl"; # above my ($month, $year) = @ARGV or die << "USAGE"; usage: week-partition MONTH YEAR MONTH: ex. 3 (for March) YEAR: ex. 2001 USAGE print "Partition for $month/$year\n\n"; print " M T W T F\n"; my @weeks = partitionWeekdays( $month, $year, (getFirstNonWeekend($month, $year))[1,2] ); unshift @{ $weeks[0] }, ("") x (5 - @{ $weeks[0] }); push @{ $weeks[-1] }, ("") x (5 - @{ $weeks[-1] }); printf "%2s %2s %2s %2s %2s\n", @$_ for @weeks;

In reply to Week Partitioner by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.