# Calendar-Generator # # J. Nagasaki, Jan. 2006 # Last change: 2007-04-07 # # Predefines $dash = "-------------------------"; $ddash = "========================="; $space = " "; $width = 10; # min = 5 @ultimo = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @dayname = ("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"); @month1 = ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"); @month2 = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $args = @ARGV; print "\n Calendar Calculation\n"; print "======================\n\n"; print "J. Nagasaki, Jan. 2006\n\n"; # ======= check arguments ======== if ($args < 1) { print "Usage:\n"; print "perl calendar.pl <2YYY> \n\n"; print " = cloumn width (min. 5)\n\n"; print "Year must be greater than 2000 and less than 2200.\n\n"; exit (1); } $width = $ARGV[0]; if (($width < 5) || ($width > 25)) { print "Please give clumn width greater than 4 and smaller than 26.\n\n"; exit(1); } $year = $ARGV[1]; if ((!($year =~ /2[0-1][0-9][0-9]/)) || ($year < 2001)) { print "Please use year greater than 2000 and number format 2YYY for year number!\n\n"; exit (1); } $filename = $ARGV[2]; print "Calendar for $year will be generated ...\n\n"; # Usage of month name or abbreviation according to column width if ($width > 10) { @month = @month2; } else { @month = @month1; } # Calculation of format of column width to center month name ontop table head $dstr = substr($dash, 0, $width); # width for "----- ..." $ddstr = substr($ddash, 0, $width); # width for "===== ..." @prmonat = ""; # Space place holder field of month names (@month) $i = 0; # place holder left and right of month name and name to @prmonat-strings foreach (@month) { $sp1 = int($width / 2 - length($month[$i]) / 2); $sp2 = $width - length($month[$i]) - $sp1; $prmonat[$i] = substr($space, 0, $sp1).$month[$i].substr($space, 0, $sp2); $i++; } # ========= calendar precalculations =========== # calculation of leap-years $leapdays = 0; for ($fyear = 2001; $fyear <= ($year - 1); $fyear++) { $leapdays++ if (($fyear / 4) == int($fyear / 4)); $leapdays-- if (($fyear / 400) == int($fyear / 400)); #$leapdays = 0 if ($fyear == 2000); # 2000 NO leap-year! } # calculation of day (name) of 1st January (2001-01-01 was a Monday == 0) $primo[1] = (($year - 2001) + $leapdays) % 7; # calculation if actual year is a leap-year $leapdays = 0; $fyear = $year; $leapdays++ if (($fyear / 4) == int($fyear / 4)); $leapdays-- if (($fyear / 400) == int($fyear / 400)); $ultimo[2] = $ultimo[2] + $leapdays; # adapt February # calculation of first days of weak for the remaining months for ($i = 2; $i <= 12; $i++) { $primo[$i] = ($primo[$i - 1] + $ultimo[$i - 1]) % 7; } # ========== Output of Calendar Table ========== @outfile = ""; for ($l = 0; $l < 2; $l++) { push (@outfile, "\n"); $indent = substr($space, 0, (3 * $width - 5)); if ($l == 0) { $halfyear = "1st"; } else { $halfyear = "2nd"; } push (@outfile, "$indent$halfyear HALFYEAR $year\n\n"); push (@outfile, ".$dstr.$dstr.$dstr.$dstr.$dstr.$dstr.\n"); if (!$l) { push (@outfile, "|$prmonat[0]|$prmonat[1]|$prmonat[2]|$prmonat[3]|$prmonat[4]|$prmonat[5]|\n"); } else { push (@outfile, "|$prmonat[6]|$prmonat[7]|$prmonat[8]|$prmonat[9]|$prmonat[10]|$prmonat[11]|\n"); } push (@outfile, "|$ddstr|$ddstr|$ddstr|$ddstr|$ddstr|$ddstr|\n"); for ($dayname = 0; $dayname <= 30; $dayname++) { push (@outfile, "|"); for ($j = (($l * 6) + 1); $j <= (($l * 6) + 6); $j++) { $k = ($primo[$j] + $dayname) % 7; if (($dayname + 1) <= $ultimo[$j]) { push (@outfile, "$dayname[$k] "); push (@outfile, " ") if (($dayname + 1) < 10); push (@outfile, ($dayname + 1)); $sp = substr($space, 0, $width - 5); push (@outfile, "$sp|"); } else { $sp = substr($space, 0, $width); push (@outfile, "$sp|"); } } push (@outfile, "\n"); if ($dayname == 30) { push (@outfile, "'"); } else { push (@outfile, "|"); } for ($j = (($l * 6) + 1); $j <= (($l * 6) + 6); $j++) { $k = ($primo[$j] + $dayname) % 7; if ((($dayname + 1) <= $ultimo[$j]) && ($k == 6)) { push (@outfile, "$ddstr"); } else { push (@outfile, "$dstr"); } if ($dayname == 30) { push (@outfile, "'"); } else { push (@outfile, "|"); } } push (@outfile, "\n"); } push (@outfile, "\n\n\n"); } print "Done!\n\n"; open(FILE, ">$filename"); print FILE @outfile; close(FILE);