Sunday, February 24, 2008

2008 Scripting Games, Solution 4

# Advanced Event 4: Image is Everything

# Being non-native English speaking, I wanted to turn this
# into a generic calendar, that could be used anywhere
# Consequently, I created a function that by default uses
# the default culture and then specified en-US when calling
# it to comply with event rules

function Show-Calendar {

if ($Culture) {
# Use supplied culture
$culture=[globalization.cultureinfo] $Culture
else {
# Use default culture

# Get first day of week, the calender must be show correct
# e.g. in US Sunday is the first day of week, in Denmark Monday is the first
# Get the weekdays
# Calculate the width or the weekdays to right-adjust values
$width=($weekdays | measure-object -maximum length).maximum

# If first day of week is not 0, shift the order
if ([int]$FirstDayOfWeek) {
$ShiftedWeekdays=$weekdays[$FirstDayOfWeek..($WeekDays.count)] + $weekdays[0..($FirstDayOfWeek-1)]
else {

# Make sure we start the first day in the month
$StartDate=New-Object datetime $StartDate.year,$StartDate.month,1

# Working date

# Header

# Process scriptblock and pipe output to Format-Table
while ($date.Month -eq $StartDate.Month) {
# Week-break code
if ([int]$date.DayOfWeek -eq $firstDayOfWeek -and $week) {
# Create week object with days as properties
if (!$week) {
$week=New-Object Object
$ShiftedWeekdays | % {
Add-Member -inputObject $week NoteProperty $_ $null
# Get the day e.g. the property name
# Assign value to property
$week.$dayOfWeek="{0,$width}" -f $ # Right adjust values
# Values must be right-aligned as the days will $null-values will
# set off Format-Table left-aligning those columns

# Next day
if ($week) {
# Print out final week, if any
} | Format-Table -autosize # Pick up the alias properties and format it


# Prompt user
$month=read-host "Enter month in month/year format"
# Split into month and year
# And call function with en-us culture
Show-Calendar (New-Object Datetime $year,$month,1) "en-us"

No comments: