-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-DaylightSaving.ps1
161 lines (142 loc) · 6.08 KB
/
Get-DaylightSaving.ps1
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
function Get-DaylightSaving {
<#
.SYNOPSIS
Returns info about your timezone's DST
.DESCRIPTION
Returns info about your timezone's DST, including start and end times.
.EXAMPLE
Get-DaylightSaving
.OUTPUTS
A PsCustomObject with a Win32_TimeZone object as one of its properties.
#>
[CmdletBinding()]
param (
# Provide a date, defaults to now
[Parameter(Position=0)]
[ValidateNotNull()]
[datetime]
$Date = (Get-Date)
)
$TZo = Get-CimInstance win32_timezone
$TimeZone = $TZo.StandardName
$TZa = [System.TimeZoneInfo]::FindSystemTimeZoneById($TimeZone)
if ($TZa.SupportsDaylightSavingTime) {
# We'll use this logic twice
$indexToDayOfWeek = {param($index)
switch ($index) {
0 {'Sunday'}
1 {'Monday'}
2 {'Tuesday'}
3 {'Wednesday'}
4 {'Thursday'}
5 {'Friday'}
6 {'Saturday'}
}
}
# Here, 12 + 1 SHOULD equal 1 (January)
$monthAfterDaylight = if (($TZo.DaylightMonth + 1) -eq 13) {1} else {$TZo.DaylightMonth + 1}
$monthAfterStandard = if (($TZo.StandardMonth + 1) -eq 13) {1} else {$TZo.StandardMonth + 1}
# This calculates the standard date that DST changes in the given year
$stdDayOfWeek = Invoke-Command -ScriptBlock $indexToDayOfWeek -ArgumentList ($TZo.StandardDayOfWeek)
$stdProps = @{
Ordinal = $TZo.StandardDay
DayOfWeek = $stdDayOfWeek
StartDate = (Get-Date -Year $Date.Year -Month $TZo.StandardMonth -Day 1).Date
EndDate = (Get-Date -Year $Date.Year -Month $monthAfterStandard -Day 1).Date.AddTicks(-1)
}
$stdDate = (Find-DateByWeekNumber @stdProps).AddHours(
$TZo.StandardHour).AddMinutes($TZo.StandardMinute
).AddSeconds($TZo.StandardSecond).AddMilliseconds($TZo.StandardMillisecond)
#
# This calculates the daylight date that DST changes in the given year
$dayDayOfWeek = Invoke-Command -ScriptBlock $indexToDayOfWeek -ArgumentList ($TZo.DaylightDayOfWeek)
$dayProps = @{
Ordinal = $TZo.DaylightDay
DayOfWeek = $dayDayOfWeek
StartDate = (Get-Date -Year $Date.Year -Month $TZo.DaylightMonth -Day 1).Date
EndDate = (Get-Date -Year $Date.Year -Month $monthAfterDaylight -Day 1).Date.AddTicks(-1)
}
$dayDate = (Find-DateByWeekNumber @dayProps).AddHours(
$TZo.DaylightHour).AddMinutes($TZo.DaylightMinute
).AddSeconds($TZo.DaylightSecond).AddMilliseconds($TZo.DaylightMillisecond)
#
# Now we can know where on the calendar we are now
if ($Date -ge $stdDate -or $Date -lt $dayDate) {
$CurrentDstMode = 'Standard'
$CurrentDstName = $TZa.StandardName
$CurrentBias = $TZo.Bias - $TZo.StandardBias
$NextDstMode = 'Daylight'
$NextDstName = $TZa.DaylightName
$NextBias = $TZo.Bias - $TZo.DaylightBias
} elseif ($Date -lt $stdDate -and $Date -ge $dayDate) {
$CurrentDstMode = 'Daylight'
$CurrentDstName = $TZa.DaylightName
$CurrentBias = $TZo.Bias - $TZo.DaylightBias
$NextDstMode = 'Standard'
$NextDstName = $TZa.StandardName
$NextBias = $TZo.Bias - $TZo.StandardBias
}
$NextBiasShift = $NextBias - $CurrentBias
$NextBiasShiftDirection = if ($NextBiasShift -lt 0) {
-1
} elseif ($NextBiasShift -gt 0) {
1
} else {0}
# Adjust for next year if needed
if ($Date -ge $stdDate) {
$stdProps.Set_Item(
'StartDate' ,
("$($TZo.StandardMonth)/1/$($Date.Year + 1)")
)
$stdProps.Set_Item(
'EndDate' ,
([datetime]("$($TZo.StandardMonth + 1)/1/$($Date.Year + 1)")).AddTicks(-1)
)
$stdDate = (Find-DateByWeekNumber @stdProps).AddHours(
$TZo.StandardHour).AddMinutes($TZo.StandardMinute
).AddSeconds($TZo.StandardSecond).AddMilliseconds($TZo.StandardMillisecond)
}
if ($Date -ge $dayDate) {
$dayProps.Set_Item(
'StartDate' ,
("$($TZo.DaylightMonth)/1/$($Date.Year + 1)")
)
$dayProps.Set_Item(
'EndDate' ,
([datetime]("$($TZo.DaylightMonth + 1)/1/$($Date.Year + 1)")).AddTicks(-1)
)
$dayDate = (Find-DateByWeekNumber @dayProps).AddHours(
$TZo.DaylightHour).AddMinutes($TZo.DaylightMinute
).AddSeconds($TZo.DaylightSecond).AddMilliseconds($TZo.DaylightMillisecond)
}
# Now the next date for change is
$UntilNextChange = @(
[timespan]($stdDate - $Date)
[timespan]($dayDate - $Date)
) | Sort-Object | Select-Object -First 1
$nextChange = $Date + $UntilNextChange
$SystemChange = [pscustomobject]@{
IsFixedDateRule = $TZa.GetAdjustmentRules().DaylightTransitionEnd[-1].IsFixedDateRule
DaylightTransitionStart = $TZa.GetAdjustmentRules().DaylightTransitionStart
DaylightTransitionEnd = $TZa.GetAdjustmentRules().DaylightTransitionEnd
}
}#END: if ($TZa.SupportsDaylightSavingTime) {
# Now output an object
[PSCustomObject][ordered]@{
TimeStamp = $Date
CurrentBias = $CurrentBias
SupportsDaylightSavingTime = $TZa.SupportsDaylightSavingTime
CurrentDstMode = $CurrentDstMode
CurrentDstName = $CurrentDstName
Win32_TimeZone = $TZo
SystemChange = $SystemChange
NextStandardChangeOn = $stdDate
NextDaylightChangeOn = $dayDate
NextDstMode = $NextDstMode
NextDstName = $NextDstName
NextChange = $nextChange
UntilNextChange = $UntilNextChange
NextBiasShiftDirection = $NextBiasShiftDirection
NextBias = $NextBias
}
}#END: function Get-DaylightSaving {}