# get-timezone.ps1
# Sample recoded in PowerShell
# Thomas Lee - tfl@psp.co.uk
# Setup
[string] $dataFmt = "{0,-30}{1}"
[string] $timeFmt = "{0,-30}{1:yyyy-MM-dd HH:mm}"
# write some nice output:
"This example of selected TimeZone class elements generates the following "
"output, which varies depending on the time zone in which it is run."
""
# Get local time
$LocalZone = [System.TimeZone]::CurrentTimeZone;
$currentDate = [System.DateTime]::Now
$currentYear = $currentDate.Year
# Display the names for standard time and daylight saving
# time for the local time zone.
$dataFmt -f "Standard time name:",$localZone.StandardName
$dataFmt -f "Daylight saving time name:",$localZone.DaylightName
""
# Display the current date and time and show if they occur
# in daylight saving time.
$timeFmt -f "Current date and time:",$currentDate
$dataFmt, "Daylight saving time?", $localZone.IsDaylightSavingTime($currentDate )
# Get the current Coordinated Universal Time (UTC) and UTC
# offset.
$currentUTC = $localZone.ToUniversalTime($currentDate)
$currentOffset = $localZone.GetUtcOffset($currentDate)
$timeFmt -f "Coordinated Universal Time:",$currentUTC
$dataFmt -f "UTC offset:", $currentOffset
""
# Get the DaylightTime object for the current year.
$daylight = $localZone.GetDaylightChanges( $currentYear )
# Display the daylight saving time range for the current year.
"Daylight saving time for year {0}:" -f $currentYear
$dstfmt = "{0:yyyy-MM-dd HH:mm} to {1:yyyy-MM-dd HH:mm} delta: {2}"
$dstfmt -f $daylight.Start, $daylight.End, $daylight.Delta
This script produced the following output when run on my machine:
PSH [C:\foo]: .\get-timezone.ps1
This example of selected TimeZone class elements generates the following
output, which varies depending on the time zone in which it is run.
Standard time name: GMT Standard Time
Daylight saving time name: GMT Daylight Time
Current date and time: 2008-04-06 12:25
{0,-30}{1}
Daylight saving time?
True
Coordinated Universal Time: 2008-04-06 11:25
UTC offset: 01:00:00
Daylight saving time for year 2008:
2008-03-30 01:00 to 2008-10-26 02:00 delta: 01:00:00