Unix cal command

X

xshell

I tried mimicking UNIX Cal command in PowerShell. Please feel free to modify
it and suggest me any better implementation.



#Unix Cal Command ( Get-Cal.ps1 )
Param(
[Int]$Year=(Get-Date).Year,
[Int]$Month=(Get-Date).Month,
[Switch] $Next,
[Switch] $Previous
)
$script:err=$false
if ($Next)
{
$Month=((Get-Date).Month+1)
if ($Month -eq 13)
{
$Year=(Get-Date).Year+1
$Month=1
}
}
if ($Previous)
{
$Month=((Get-Date).Month-1)
if ($Month -eq 0)
{
$Year=(Get-Date).Year-1
$Month=12;
}
}
$Space=" "
trap { Write-Host -f red "Exception occurred while parsing month/year
parameters.";$script:err=$true;continue}
$StartDate=new-object System.DateTime $Year,$Month,1
if ($script:err)
{
return;
}
$MonthLabel="$($StartDate.ToString('MMMM')) $($StartDate.Year)"
$DOWLabel=[Enum]::GetValues([DayOfWeek]) | % {
$_.ToString().SubString(0,2) }
$AlignCenter=[Math]::Ceiling((20-$MonthLabel.Length)/2)
Write-Host
"{0}{1}" -f ($Space * $AlignCenter),$MonthLabel
Write-Host
"$DOWLabel"
$NextDate=$StartDate
$SpaceCount=(([int]$StartDate.DayOfWeek))*3
Write-Host $($Space * $SpaceCount) -No
While ($NextDate.Month -eq $StartDate.Month)
{
if ($NextDate.ToString("MMddyyyy") -eq $(get-date).ToString("MMddyyyy"))
{
Write-Host ("{0,2}" -f $NextDate.Day) -no -fore yellow
}
else
{
Write-Host ("{0,2}" -f $NextDate.Day) -no
}
Write-Host " " -No
$NextDate=$NextDate.AddDays(1)
if ($NextDate.DayOfWeek -eq [DayOfWeek]::Sunday)
{
Write-Host
}

}
Write-Host


---- Output ------------

PS > .\Get-Cal.ps1

September 2006

Su Mo Tu We Th Fr Sa
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

PS >
 

My Computer

A

Adam Milazzo

xshell wrote:
> I tried mimicking UNIX Cal command in PowerShell. Please feel free to modify
> it and suggest me any better implementation.


Nice output. I remember I often used the 'cal' command in *nix.

I'd like to see a -3 option that shows three months side-by-side (with
the current/selected month in the center). :-)
 

My Computer

?

=?Utf-8?B?ZHJlZXNjaGtpbmQ=?=

Jean also did a nice calendar gadget some time ago:

http://groups.google.com/group/microsoft.public.fr.scripting/msg/112cabd0e318dc9a?hl=fr

--
greetings
dreeschkind

"Adam Milazzo" wrote:

> xshell wrote:
> > I tried mimicking UNIX Cal command in PowerShell. Please feel free to modify
> > it and suggest me any better implementation.

>
> Nice output. I remember I often used the 'cal' command in *nix.
>
> I'd like to see a -3 option that shows three months side-by-side (with
> the current/selected month in the center). :-)
>
 

My Computer

S

Staffan Gustafsson

Hi,

Using [globalization.datetimeformatinfo]::CurrentInfo.FirstDayOfWeek and
[globalization.datetimeformatinfo]::CurrentInfo.ShortestDayNames would make
for a better localization story.

Regards,

/Staffan

"xshell" <[email protected]> wrote in message
news:[email protected]
>I tried mimicking UNIX Cal command in PowerShell. Please feel free to
>modify it and suggest me any better implementation.
>
>
>
> #Unix Cal Command ( Get-Cal.ps1 )
> Param(
> [Int]$Year=(Get-Date).Year,
> [Int]$Month=(Get-Date).Month,
> [Switch] $Next,
> [Switch] $Previous
> )
> $script:err=$false
> if ($Next)
> {
> $Month=((Get-Date).Month+1)
> if ($Month -eq 13)
> {
> $Year=(Get-Date).Year+1
> $Month=1
> }
> }
> if ($Previous)
> {
> $Month=((Get-Date).Month-1)
> if ($Month -eq 0)
> {
> $Year=(Get-Date).Year-1
> $Month=12;
> }
> }
> $Space=" "
> trap { Write-Host -f red "Exception occurred while parsing month/year
> parameters.";$script:err=$true;continue}
> $StartDate=new-object System.DateTime $Year,$Month,1
> if ($script:err)
> {
> return;
> }
> $MonthLabel="$($StartDate.ToString('MMMM')) $($StartDate.Year)"
> $DOWLabel=[Enum]::GetValues([DayOfWeek]) | % {
> $_.ToString().SubString(0,2) }
> $AlignCenter=[Math]::Ceiling((20-$MonthLabel.Length)/2)
> Write-Host
> "{0}{1}" -f ($Space * $AlignCenter),$MonthLabel
> Write-Host
> "$DOWLabel"
> $NextDate=$StartDate
> $SpaceCount=(([int]$StartDate.DayOfWeek))*3
> Write-Host $($Space * $SpaceCount) -No
> While ($NextDate.Month -eq $StartDate.Month)
> {
> if ($NextDate.ToString("MMddyyyy") -eq $(get-date).ToString("MMddyyyy"))
> {
> Write-Host ("{0,2}" -f $NextDate.Day) -no -fore yellow
> }
> else
> {
> Write-Host ("{0,2}" -f $NextDate.Day) -no
> }
> Write-Host " " -No
> $NextDate=$NextDate.AddDays(1)
> if ($NextDate.DayOfWeek -eq [DayOfWeek]::Sunday)
> {
> Write-Host
> }
>
> }
> Write-Host
>
>
> ---- Output ------------
>
> PS > .\Get-Cal.ps1
>
> September 2006
>
> Su Mo Tu We Th Fr Sa
> 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
>
> PS >
>
 

My Computer

J

Jacques Barathon [MS]

"Staffan Gustafsson" <[email protected]> a écrit dans le
message de news: [email protected]...
> Hi,
>
> Using [globalization.datetimeformatinfo]::CurrentInfo.FirstDayOfWeek and
> [globalization.datetimeformatinfo]::CurrentInfo.ShortestDayNames would
> make for a better localization story.


This version should work better with non-US cultures (also includes a couple
of other minor formatting changes) - not thoroughly tested, let me know if
you find a bug with your culture:

--- script begins here ---
# Unix Cal Command ( Get-Cal.ps1 )

Param(
[Int]$Year=(Get-Date).Year,
[Int]$Month=(Get-Date).Month,
[Switch] $Next,
[Switch] $Previous
)
$script:err=$false
if ($Next)
{
$Month=((Get-Date).Month+1)
if ($Month -eq 13)
{
$Year=(Get-Date).Year+1
$Month=1
}
}
if ($Previous)
{
$Month=((Get-Date).Month-1)
if ($Month -eq 0)
{
$Year=(Get-Date).Year-1
$Month=12;
}
}
$Space=" "
# trap { Write-Host -f red "Exception occurred while parsing month/year
parameters.";$script:err=$true;continue}
$StartDate=new-object System.DateTime $Year,$Month,1
if ($script:err)
{
return;
}
$MonthLabel="$($StartDate.ToString('MMMM')) $($StartDate.Year)"
$AlignCenter=[Math]::Ceiling((20-$MonthLabel.Length)/2)
write-host "`n$($Space * $AlignCenter)$MonthLabel`n"

# [culture-aware]
$DOWLabel0 =
[System.Globalization.CultureInfo]::get_CurrentCulture().datetimeformat.shortestdaynames
$i = $first =
[int][System.Globalization.CultureInfo]::get_CurrentCulture().datetimeformat.firstdayofweek
$DowLabel = @()
1..7 | foreach {
$DowLabel += $DowLabel0[$i]
$i = ($i+1) % 7
}
# [/culture-aware]

Write-Host "$DOWLabel"
$NextDate = $StartDate

# [culture-aware]
$SpaceCount = (([int]$StartDate.DayOfWeek - $first))*3
if ($SpaceCount -lt 0) {
$SpaceCount += 21
}
# [/culture-aware]

Write-Host $($Space * $SpaceCount) -No
While ($NextDate.Month -eq $StartDate.Month)
{
if ($NextDate.ToString("MMddyyyy") -eq $(get-date).ToString("MMddyyyy"))
{
Write-Host ("{0,2}" -f $NextDate.Day) -no -fore yellow
}
else
{
Write-Host ("{0,2}" -f $NextDate.Day) -no
}
Write-Host " " -No
$NextDate=$NextDate.AddDays(1)

# [culture-aware]
if ([int]$NextDate.DayOfWeek -eq $first)
# [/culture-aware]
{
Write-Host
}
}
write-host "`n"
--- script ends here ---

Jacques
 

My Computer

Top