Sunday, 29 August 2010

Get-Time.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script converts time to different time zones.  
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell 
  6. .NOTES 
  7.     File Name  : Get-Time.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/08/get-time.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-Time.ps1 
  17.     Time in GMT Daylight Time zone: 8/29/2010 11:40:50 AM 
  18.        UTC Time: 8/29/2010 10:40:50 AM 
  19.     Time in Tokyo Daylight Time zone: 8/29/2010 07:40:50 PM 
  20.        UTC Time: 8/29/2010 10:40:50 AM 
  21. #> 
  22.  
  23. # Get local time 
  24. $thisTime = [system.DateTime]::Now 
  25.  
  26. if ([System.TimeZoneInfo]::Local.IsDaylightSavingTime($thisTime)) { 
  27.     $tzn = [System.TimeZoneInfo]::Local.DaylightName } 
  28. else
  29.     $tzn = [System.TimeZoneInfo]::Local.StandardName 
  30. # Display local Time 
  31. "Time in {0} zone: {1}" -f $tzn, $thisTime 
  32. "   UTC Time: {0}" -f [system.TimeZoneInfo]::ConvertTimeToUtc($thisTime, [TimeZoneInfo]::Local) 
  33.  
  34. # Get Tokyo Standard Time zone 
  35. $tst = [system.TimeZoneInfo]::FindSystemTimeZoneById("Tokyo Standard Time"
  36. $tstTime = [system.TimeZoneInfo]::ConvertTime($thisTime, [TimeZoneInfo]::local, $tst
  37. $tstzn = if ( [System.TimeZoneInfo]::Local.IsDaylightSavingTime($tstTime)) { 
  38.          $tst.DaylightName} else {$tst.StandardName} 
  39.  
  40. # Display Tokyo Time Zone 
  41. "Time in {0} zone: {1}" -f $tstzn,$tstTime 
  42. "   UTC Time: {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($tstTime, $tst

No comments: