#!/usr/local/bin/perl -w

# HTML Calendar by -Sneex-  :]  Of course...
# Copyright (C) 2000 by WCJones; All Rights Reserved...
# Version 0.01A -- Turns month of execution into an HTML Table...

use strict;
use diagnostics;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; # 0-8 = 9 elements...
# Returns: seconds, minutes, hour, Day of Month, Month (0-11; Jan=0), 
# Year (in 00 format; add 1900 to get actual 4 digits), 
# Day of Week (0-6; Su=0), Day of Year, and Is Daylight Savings Time in effect?

# Project start Year (2000) was a leap year...
my @days    = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturday /;
my @months  = qw/ January February March April May June July August September October November December /;
my @mlimits = qw/      31       29    31    30  31   30   31     31        30      31       30       31 /;

# Force a few tests;
$mon  = 6;
$mday = 1;
$wday = 6;

my $cal = "$days[$wday] $months[$mon] $mday, " . ($year + 1900) . " \@ ";
$cal   .= sprintf("%02d:%02d:%02d", $hour, $min, $sec);
$cal   .= ($hour < 12) ? " AM": " PM";

my $first = $wday if ($mday == 1); # The First of this Month is on Week Day...

my $indx0 = $mday;
my $indx1 = $wday;
unless ($mday == 1) {
      while ($indx0-- != 1) {
           $indx1 = 6 if (--$indx1 < 0); # If we were on Sun, reset it as Sat...
      }

      $first = $indx1; # Then the First of this Month is on this Week Day...
}

my $filename = $months[$mon] . ($year + 1900);
open (oFile, ">$filename.html") or die $!;

print oFile "\<TABLE BORDER=1 ALIGN=LEFT\>\n<TR><TD BGCOLOR=#ffff66 COLSPAN=7 ALIGN=CENTER>$cal</TD></TR>\n";

print oFile<<_WeekDays_;
<TR ALIGN=RIGHT>
<TD BGCOLOR=#ffcc33> &nbsp;Su </TD>
<TD BGCOLOR=#ffffff> &nbsp;Mo </TD>
<TD BGCOLOR=#ffffff> &nbsp;Tu </TD>
<TD BGCOLOR=#ffffff> &nbsp;We </TD>
<TD BGCOLOR=#ffffff> &nbsp;Th </TD>
<TD BGCOLOR=#ffffff> &nbsp;Fr </TD>
<TD BGCOLOR=#ffcc33> &nbsp;Sa </TD>
</TR>
<TR><TD COLSPAN=7 BGCOLOR=#000000>&nbsp;</TD></TR>
_WeekDays_

my ($Sunday, $Monday, $Tuesday, $Wednesday, $Thursday, $Friday, $Saturday);
$indx0 = $mlimits[$mon];
$indx1 = $first;

my $ctr = 1; # Count out the days of yore...

while (1) {

     if (($indx1 == 0) && 
         ($ctr <= $mlimits[$mon])) { $Sunday    = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Sunday    = ""; }

     if (($indx1 == 1) && 
         ($ctr <= $mlimits[$mon])) { $Monday    = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Monday    = ""; }
 
     if (($indx1 == 2) && 
         ($ctr <= $mlimits[$mon])) { $Tuesday   = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Tuesday   = ""; }

     if (($indx1 == 3) && 
         ($ctr <= $mlimits[$mon])) { $Wednesday = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Wednesday = ""; }

     if (($indx1 == 4) && 
         ($ctr <= $mlimits[$mon])) { $Thursday  = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Thursday  = ""; }

     if (($indx1 == 5) && 
         ($ctr <= $mlimits[$mon])) { $Friday    = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Friday    = ""; }

     if (($indx1 == 6) && 
         ($ctr <= $mlimits[$mon])) { $Saturday  = $ctr++; $indx1 = 0 if (++$indx1 > 6); } else { $Saturday  = ""; }

print oFile<<_EachWeek_;
<TR ALIGN=RIGHT>
<TD BGCOLOR=#ffcc33> &nbsp;$Sunday </TD>
<TD BGCOLOR=#ffffff> &nbsp;$Monday</TD>
<TD BGCOLOR=#ffffff> &nbsp;$Tuesday</TD>
<TD BGCOLOR=#ffffff> &nbsp;$Wednesday</TD>
<TD BGCOLOR=#ffffff> &nbsp;$Thursday</TD>
<TD BGCOLOR=#ffffff> &nbsp;$Friday</TD>
<TD BGCOLOR=#ffcc33> &nbsp;$Saturday</TD>
</TR>
_EachWeek_

# Keep:  $indx1 = 0 if (++$indx1 > 6); # Inverse: If we were on Sat, reset it as Sun...

  $indx0 -= 7;
  last if ($indx0 <= 0);
} # End of EachWeek while()...


print oFile "</TABLE>\n";


__END__

The month I wrote this looked like:

    September 2000
 S  M Tu  W Th  F  S
                1  2
 3  4  5  6  7 [8] 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

