Scripting Games, Advanced Event 4, Image is Everything

Display a calendar for the specified month (…tidy formatting counts!)

Years’ ago one of the of the tasks we were set at uni was to write a compiler and an intermediate-code interpreter for a subset of Pascal.  When my solution was up a running one of the test programs I complied and ran produced a calendar in a similar way (although I’ve long since lost the source, unfortunately, thanks to frequent occurrences of media obsolescence; maybe XML saved to the web will stand the test of time!)

Back then, calculating the day of the week for a given date involved using an algorithm known as Zellar’s congruence.  These days just use the .Net .DayOfWeek property:-)

  1 $month, $year=(Read-Host "Enter month/year").split('/')
  3 $days=[datetime]::DaysInMonth($year,$month)
  5 " "; get-date("$year/$month") -f "MMMM yyyy"; " "
  6 "Sun  Mon  Tue  Wed  Thu  Fri  Sat"
  8 $line="     "*[int](get-date("$year/$month/1")).dayofweek
  9 for ($i=1; $i -le $days; $i++) {
 10   $line+="{0,3:##}  " -f $i
 11   if ($line.Length -ge 35){$line; $line=""}
 12 }
 13 if ($line) {$line}

All standard stuff.  Line 1 reads the month and year ("3/2008") in the format specified.  5-6 display a couple of header lines.

The only line of note is line 8.  This takes the day of the week for the 1st of the month in question (Sunday..Saturday as an integer 0..6) and uses this value to calculate in which column day 1 should appear.  A number of spaces are multiplied by the ordinal of the day of the week to give the correct offset.

Lines 9-11 then produce output lines, adding on days until the end of the month.

The formatting for a two digit integer displayed in a space 3 character wide (0,3:##) is vaguely reminiscent of COBOL (anyone else remember those PICTURE entries in the DATA DIVISION?  Shiver!)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s