Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Unix cal command

Reply
 
Old 09-18-2006   #1 (permalink)
xshell


 
 

Unix cal command

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 System SpecsSystem Spec
Old 09-18-2006   #2 (permalink)
Adam Milazzo


 
 

Re: Unix cal command

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 System SpecsSystem Spec
Old 09-18-2006   #3 (permalink)
=?Utf-8?B?ZHJlZXNjaGtpbmQ=?=


 
 

Re: Unix cal command

Jean also did a nice calendar gadget some time ago:

http://groups.google.com/group/micro...e318dc9a?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 System SpecsSystem Spec
Old 09-18-2006   #4 (permalink)
Staffan Gustafsson


 
 

Re: Unix cal command

Hi,

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

Regards,

/Staffan

"xshell" <xshell@nospam> wrote in message
news:u7Z4jcu2GHA.3508@TK2MSFTNGP03.phx.gbl...
>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 System SpecsSystem Spec
Old 09-18-2006   #5 (permalink)
Jacques Barathon [MS]


 
 

Re: Unix cal command

"Staffan Gustafsson" <staffan._less_spam_gustafsson@home.se> a écrit dans le
message de news: Ocdr0rw2GHA.2176@TK2MSFTNGP04.phx.gbl...
> 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 System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Equivalent of unix 'which' command with PSH? PowerShell
Unix Cal command in PoSh PowerShell
Any command like the tail -f from unix/linux to open log-files PowerShell
Equivalent to UNIX 'find' command PowerShell
Equivalent of Unix nohup command? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46