LocalToUtc.psm1

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function Convert-UtcToLocal
{
<#
.Synopsis
    Converts UTC time to the system local time.
.Description
    Converts the current UTC time to the local system time, using the local system's timezone.
    To override the UTC Time, enter an ISO 8601 string (ex: "2017-08-01 09:20:00") as a parameter.
    To override the Time Zone use any of the time zone strings listed in https://msdn.microsoft.com/en-us/library/cc749073.aspx
.Example
    Convert-UtcToLocal -AddHours -3 -UtcTime "2017-08-01 03:00:00"
.Example
    Convert-UtcToLocal -TimeZone "Pacific Standard Time"
#>

    [CmdletBinding()]
    
    param(
    [parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
    [DateTime] $Time,
    [String] $TimeZone,
    [Int] $AddDays,
    [Int] $AddHours,
    [Int] $AddMinutes
    )

    Process {
        $tzone = if ($TimeZone) { $TimeZone } else { Invoke-GetTimeZone }

        $t = Get-InputTime -Time:$Time -AddDays:$AddDays -AddHours:$AddHours -AddMinutes:$AddMinutes
        $result = Convert-TimeZone -Time:$t -ToTimeZone $tzone -FromTimeZone "UTC" -Verbose
        
        if (IsVerbose $Verbose) {
            $converted = New-Object psobject -Property @{
                UtcTime=$result.Time;
                LocalTime=$result.ToTime;
                TimeZone=$result.ToTimeZone
            }
            return $converted
        }

        return $result.ToTime
    }
}

function Convert-LocalToUtc
{
<#
.Synopsis
    Converts the local system time to UTC time
.Description
    Converts the current local system time to UTC time, using the local system's timezone.
    To override the local time, enter an ISO 8601 string (ex: "2017-08-01 09:20:00") as a parameter.
    To override the Time Zone use any of the strings listed in https://msdn.microsoft.com/en-us/library/cc749073.aspx
.Example
    Convert-LocalToUtc -AddHours 5
.Example
    Convert-LocalToUtc -TimeZone "Pacific Standard Time"
#>

    [CmdletBinding()]

    param(
    [parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
    [DateTime] $Time,
    [String] $TimeZone,
    [Int] $AddDays,
    [Int] $AddHours,
    [Int] $AddMinutes
    )

    Process {
        $tzone = if ($TimeZone) { $TimeZone } else { Invoke-GetTimeZone }

        $local = if ($Time) { $Time } else { Get-LocalTime (Get-UtcTime) }
        $local = Get-InputTime -Time:$local -AddDays:$AddDays -AddHours:$AddHours -AddMinutes:$AddMinutes
        
        # If a time is not defined, but a timezone is, then treat the
        # current local system time as the local time of the specified timezone
        if (! $Time -and $TimeZone) {
            $FromTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById($tzone)
            $ToTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById($TimeZone)
            $local = [TimeZoneInfo]::ConvertTime($local, $FromTimeZone, $ToTimeZone)
        }

        $result = Convert-TimeZone -Time $local -FromTimeZone $tzone -ToTimeZone "UTC" -Verbose
        if (IsVerbose $Verbose) {
            $converted = New-Object psobject -Property @{
                UtcTime=$result.ToTime;
                LocalTime=$result.Time;
                TimeZone=$result.FromTimeZone
            }
                
            return $converted
        }

        return $result.ToTime
    }
}

function Convert-TimeZone
{
    [CmdletBinding()]
    param(
        [parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [DateTime] $Time,
        [String] $ToTimeZone,
        [String] $FromTimeZone
    )

    Process {
        # always start with the current utc time if $Time is not set
        $fromTimeUtc = $inputTime = if ($Time) { $Time } else { Get-UtcTime }

        # if from time zone is not specified, use the current system timezone
        $fromTz = Invoke-GetTimeZone
        if ($FromTimeZone) {
            $fromTz = [TimeZoneInfo]::FindSystemTimeZoneById($FromTimeZone)
        } else {
            $fromTz = [TimeZoneInfo]::FindSystemTimeZoneById($fromTz)
        }

        if (! $ToTimeZone) { return }
        $toTz = [TimeZoneInfo]::FindSystemTimeZoneById($ToTimeZone)

        # convert the input time to utc using the FromTimeZone
        if ($Time) {
            $fromTimeKind = [System.DateTime]::SpecifyKind($Time, [System.DateTimeKind]::Unspecified)
            $fromTimeUtc = [TimeZoneInfo]::ConvertTimeToUTC($fromTimeKind, $fromTz)
        }

        # use the current utc time to find the time in the destination timezone
        $toTimeKind = [System.DateTime]::SpecifyKind($fromTimeUtc, [System.DateTimeKind]::Unspecified)
        $toTime = [TimeZoneInfo]::ConvertTimeFromUTC($toTimeKind, $toTz)

        if (IsVerbose $Verbose) {
            $result = New-Object psobject -Property @{
                Time=$inputTime;
                FromTimeZone=$fromTz;
                ToTimeZone=$toTz;
                ToTime=$toTime }
            return $result
        }

        return $toTime
    }
}

function IsVerbose ([Switch] $Verbose) {
    $IsVerbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
    return $IsVerbose -eq $true
}

function Get-InputTime {
    Param (
        [String] $Time,
        [Int] $AddDays,
        [Int] $AddHours,
        [Int] $AddMinutes
    )
    if ($Time) { $t = Get-Date $Time }
    if ($AddDays) { $t = $t.AddDays($AddDays) }
    if ($AddHours) { $t = $t.AddHours($AddHours) }
    if ($AddMinutes) { $t = $t.AddMinutes($AddMinutes) }
    return $t
}

# overloaded to help with testing
function Get-UtcTime { return [System.DateTime]::UtcNow }
function Get-LocalTime ($time) { return $time.ToLocalTime() }
function Invoke-GetTimeZone {
    if (Get-Command Get-TimeZone -errorAction SilentlyContinue) {
        return Get-TimeZone | Select-Object -ExpandProperty Id
    }
    
    return tzutil /g
}

Export-ModuleMember -function Convert-LocalToUtc
Export-ModuleMember -function Convert-UtcToLocal
Export-ModuleMember -function Convert-TimeZone