Convert-Time.psm1
function Get-SystemTimeZone { <# .Synopsis Get installed timezones .DESCRIPTION This command allows you get the list of installed timezones. .EXAMPLE Get-SystemTimeZone ID DisplayName BaseUtcOffset SupportsDaylightSavingTime -- ----------- ------------- -------------------------- Dateline Standard Time (UTC-12:00) International Date Line West -12:00:00 False UTC-11 (UTC-11:00) Coordinated Universal Time-11 -11:00:00 False Aleutian Standard Time (UTC-10:00) Aleutian Islands -10:00:00 True Hawaiian Standard Time (UTC-10:00) Hawaii -10:00:00 False Marquesas Standard Time (UTC-09:30) Marquesas Islands -09:30:00 False Alaskan Standard Time (UTC-09:00) Alaska -09:00:00 True #> Update-FormatData -AppendPath (Join-Path -Path $PSScriptRoot -ChildPath "Convert-Time.ps1xml") -ErrorAction SilentlyContinue Return [System.TimeZoneInfo]::GetSystemTimeZones() } function Convert-Time { <# .Synopsis This command allows you to convert the time between the time zones .DESCRIPTION This command allows you to input any standard time zone name and it's local time as an input and allows you to convert the time to a different time zone. The script will take care about the day light savings on & off. .EXAMPLE Convert-Time -InputTimeZone "GMT Standard Time" -InputTime "1/11/2018 10:20 AM" -TargetTimeZone "India Standard Time" InputTimeZone InputTime TargetTimeZone ConvertedTime ------------- --------- -------------- ------------- GMT Standard Time 1/11/2018 10:20:00 AM India Standard Time 1/11/2018 3:50:00 PM #> Param( [Parameter(Mandatory=$true)][String]$InputTimeZone, [Parameter(Mandatory=$true)][DateTime]$InputTime, [Parameter(Mandatory=$true)][String]$TargetTimeZone ) BEGIN { $Error.Clear() $C_Sharp_Code = @" using System; namespace CloudComputee.Utilities { public class Time { public static DateTime ConvertTime(DateTime LoalTime, String TargetTimeZone) { DateTime ConvertedTime; DateTime LocalTime_in_UTC = LoalTime.ToUniversalTime(); try { System.TimeZoneInfo _TargetTimeZone = System.TimeZoneInfo.FindSystemTimeZoneById(TargetTimeZone); ConvertedTime = System.TimeZoneInfo.ConvertTimeFromUtc(LocalTime_in_UTC, _TargetTimeZone); } catch (TimeZoneNotFoundException TZNF) { throw TZNF; } catch (InvalidTimeZoneException ITZ) { throw ITZ; } return ConvertedTime; } } } "@ Add-Type -TypeDefinition $C_Sharp_Code $ErrorActionPreference = "Stop" try { $InputTimeZone_res = [System.TimeZoneInfo]::FindSystemTimeZoneById($InputTimeZone) } catch { Write-Error "The time zone Id '$InputTimeZone' was not found" break } try { $TargetTimeZone_res = [System.TimeZoneInfo]::FindSystemTimeZoneById($TargetTimeZone) } catch { Write-Error "The time zone Id '$TargetTimeZone' was not found" break } $ErrorActionPreference = "Continue" } PROCESS { if([System.TimeZone]::CurrentTimeZone.ToString() -ne $InputTimeZone) { $localTime = Get-Date $InputTime $Non_local_Time = [CloudComputee.Utilities.Time]::ConvertTime($localTime,$InputTimeZone) $diff = ((Get-Date $localTime) - (Get-Date $Non_local_Time)) $hrs = $diff.Hours $min = $diff.Minutes $Converted_Local_Time = (Get-Date $InputTime).AddMinutes($min).AddHours($hrs) $Result = [CloudComputee.Utilities.Time]::ConvertTime($Converted_Local_Time,$TargetTimeZone) } else { $Result = [CloudComputee.Utilities.Time]::ConvertTime($InputTime,$TargetTimeZone) } $Hash = [Ordered]@{ InputTimeZone = $InputTimeZone InputTime = $InputTime TargetTimeZone = $TargetTimeZone ConvertedTime = $Result } } END { return (New-Object PSObject -Property $Hash) } } |