Wednesday 1 September 2010

Get-PersianCalendar.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays details of a Persian calendar in PowerShell 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN C# sample. 
  6. .NOTES 
  7.     File Name  : Get-PersianCalendar.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/09/get-persiancalendarps1.html
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-PersianCalendar.ps1
  17.     ................. Today .......................... 
  18.     Today is: 
  19.        Wednesday, 9/1/2010 03:25:17 PM in the Gregorian calendar. 
  20.        Wednesday, 6/10/1389 15:25:17 in the Persian calendar. 
  21.     ............... Fields ............................ 
  22.     PersianEra = 1 
  23.     ............... Properties ........................ 
  24.     Eras: 
  25.      era = 1 
  26.     Gregorian Date Range Supported by the Persian Calendar: 
  27.        From 3/21/0622 12:00:00 AM 
  28.        To   12/31/9999 11:59:59 PM 
  29.     TwoDigitYearMax = 99 
  30.     ............ Selected Methods ...................... 
  31.     GetDayOfYear: day       = 165 
  32.     GetDaysInMonth: days    = 30 
  33.     GetDaysInYear: days     = 366 
  34.     GetLeapMonth:  month    = 0 
  35.     GetMonthsInYear: months = 12 
  36.     IsLeapDay:              = False 
  37.     IsLeapMonth:            = False 
  38.     IsLeapYear: 1370 is a leap year = True 
  39.     ToFourDigitYear: 
  40.       If TwoDigitYearMax = 99, ToFourDigitYear(99) = 99 
  41.       If TwoDigitYearMax = 2010, ToFourDigitYear(99) = 1999 
  42. #> 
  43. # Get today's date. 
  44. $Jc       = new-object system.Globalization.PersianCalendar 
  45. $ThisDate = [System.DateTime]::Now 
  46.  
  47. # Display the current date using the Gregorian and Persian calendars. 
  48. "Today is:" 
  49. "   {0:dddd}, {0} in the Gregorian calendar." -f $ThisDate 
  50. "   {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar." -f $jc.GetDayOfWeek($thisDate),  
  51.                         $jc.GetMonth($thisDate),  
  52.                         $jc.GetDayOfMonth($thisDate),  
  53.                         $jc.GetYear($thisDate),  
  54.                         $jc.GetHour($thisDate),  
  55.                         $jc.GetMinute($thisDate),  
  56.                         $jc.GetSecond($thisDate
  57.  
  58. # Fields 
  59. "............... Fields ............................" 
  60. "PersianEra = {0}" -f [System.Globalization.PersianCalendar]::PersianEra 
  61.  
  62. # Properties 
  63. "............... Properties ........................" 
  64. "Eras:" 
  65. foreach ($era in $jc.Eras){ 
  66.          " era = {0}" -f $era 
  67. "Gregorian Date Range Supported by the Persian Calendar:" 
  68. "   From {0:G}" -f $jc.MinSupportedDateTime 
  69. "   To   {0:G}" -f $jc.MaxSupportedDateTime 
  70. "TwoDigitYearMax = {0}" -f $jc.TwoDigitYearMax 
  71.  
  72. # Methods 
  73. "............ Selected Methods ......................" 
  74. "GetDayOfYear: day       = {0}"    -f $jc.GetDayOfYear($thisDate
  75. "GetDaysInMonth: days    = {0}" -f $jc.GetDaysInMonth($thisDate.Year, $thisDate.Month,  
  76.                                    [System.Globalization.PersianCalendar]::PersianEra) 
  77. "GetDaysInYear: days     = {0}" -f $jc.GetDaysInYear($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra) 
  78. "GetLeapMonth:  month    = {0}" -f $jc.GetLeapMonth($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra) 
  79. "GetMonthsInYear: months = {0}" -f $jc.GetMonthsInYear($thisDate.Year,[System.Globalization.PersianCalendar]::PersianEra) 
  80. "IsLeapDay:              = {0}" -f $jc.IsLeapDay($thisDate.Year, $thisDate.Month, $thisDate.Day,  
  81.                         [System.Globalization.PersianCalendar]::PersianEra) 
  82. "IsLeapMonth:            = {0}" -f $jc.IsLeapMonth($thisDate.Year, $thisDate.Month,  
  83.                         [System.Globalization.PersianCalendar]::PersianEra) 
  84. "IsLeapYear: 1370 is a leap year = {0}" -f $jc.IsLeapYear(1370, [System.Globalization.PersianCalendar]::PersianEra) 
  85.  
  86. # Get the 4-digit year for a year whose last two digits are 99. The 4-digit year  
  87. # depends on the current value of the TwoDigitYearMax property. 
  88. "ToFourDigitYear:" 
  89. "  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99) 
  90. $jc.TwoDigitYearMax = $thisDate.Year 
  91. "  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99)   

No comments: