{"id":4784,"date":"2012-10-23T00:01:00","date_gmt":"2012-10-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/23\/check-for-daylight-savings-time-by-using-powershell\/"},"modified":"2012-10-23T00:01:00","modified_gmt":"2012-10-23T00:01:00","slug":"check-for-daylight-savings-time-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/check-for-daylight-savings-time-by-using-powershell\/","title":{"rendered":"Check for Daylight Savings Time by Using PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Brian Wilhite, guest blogger, discusses a function to check for transition to daylight savings time.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today, we have a guest blogger. Brian Wilhite has been a guest in the past, and you can see his previous posts <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/brian+wilhite\/\" target=\"_blank\">here<\/a>. Brian is speaking at the Atlanta Windows PowerShell Saturday event (Alpharetta) on October 27, 2012. He is reprising his talk from the Charlotte Windows PowerShell Saturday event (it was standing-room only and was very well received). Tickets are still available for the event. For information about Windows PowerShell Saturday in Atlanta (Alpharetta), check out the <a href=\"http:\/\/powershellsaturday.com\/003\/\" target=\"_blank\">PowerShell Saturday site<\/a>.<\/p>\n<p>Take it away, Brian &hellip;<\/p>\n<p>It&rsquo;s that time of year where most of us, in the United States, &ldquo;gain&rdquo; an hour of sleep by setting our clocks back one hour. I&rsquo;ve been getting emails and comments about a Windows PowerShell function I created called <b>Get-DSTInfo<\/b>, which queries the Win32_TimeZone WMI Class to determine when the system &ldquo;thinks&rdquo; it needs to perform those automated changes. It&rsquo;s that time of year where the IT community is looking for ways to ensure their systems are going to behave as expected. I&rsquo;m always excited to see people in the world use some of the tools I create. For more information, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/18\/use-powershell-to-save-time-with-win32-timezone-and-dst-time-changes.aspx\" target=\"_blank\">my previous Hey, Scripting Guy! Blog entry<\/a> introducing this function.<\/p>\n<p>Following is an image of what the output looks like when <b>Get-DSTInfo<\/b> runs locally.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2806.hsg-10-23-12-1.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2806.hsg-10-23-12-1.png\" \/><\/a><\/p>\n<p>Recently, I received some feedback from Justin Moore that my function didn&rsquo;t display the <b>DaylightChgDate<\/b> and <b>StandardChgDate<\/b> Date\/Time correctly for the Central Europe Time Zone. To learn more, we will need to dig into the Win32_TimeZone WMI Class. This class has the following information, which my function uses to calculate the actual date.<\/p>\n<p>The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance Win32_TimeZone | select *<\/p>\n<p>The command and the output from the command are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5023.hsg-10-23-12-2.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5023.hsg-10-23-12-2.png\" \/><\/a><\/p>\n<p>You&rsquo;ll notice that the actual dates of the time changes are not present. However, what we do have is the <b>StandardDay<\/b> property, which means the <b>First<\/b>, <b>Second<\/b>, <b>Third<\/b>, <b>Fourth<\/b>, or <b>Last<\/b> day that the time changes.&nbsp;<\/p>\n<p>Here, in the United States, we will set our clocks back one hour on the first Sunday in November. In this case, the <b>StandardDay<\/b> property equals <b>1<\/b>, meaning the <b>First<\/b> in &ldquo;<b>First <\/b>Sunday of November.&rdquo; The switch statement, shown below, highlights this concept.<\/p>\n<p style=\"padding-left: 30px\">Switch ($TimeZone.StandardDay)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 1 {$STNDDay = &#8220;First&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 2 {$STNDDay = &#8220;Second&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 3 {$STNDDay = &#8220;Third&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 4 {$STNDDay = &#8220;Fourth&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 5 {$STNDDay = &#8220;Last&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;}#End Switch ($TimeZone.StandardDay)&nbsp;<\/p>\n<p>Next, let&rsquo;s take a look at the <b>StandardDayOfWeek<\/b> property, which means the day of the week the time change will occur. In the image above, it equals <b>0<\/b>, which means <b>Sunday<\/b> in &ldquo;First<b> Sunday<\/b> of November.&rdquo; The switch statement follows.<\/p>\n<p style=\"padding-left: 30px\">Switch ($TimeZone.StandardDayOfWeek)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 0 {$STNDWeek = &#8220;Sunday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 1 {$STNDWeek = &#8220;Monday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 2 {$STNDWeek = &#8220;Tuesday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 3 {$STNDWeek = &#8220;Wednesday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 4 {$STNDWeek = &#8220;Thursday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 5 {$STNDWeek = &#8220;Friday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 6 {$STNDWeek = &#8220;Saturday&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;}#End Switch ($TimeZone.StandardDayOfWeek)<\/p>\n<p>Finally, let&rsquo;s review the <b>StandardMonth<\/b> property, which means the month the time change will occur, which, in this example, is <b>11<\/b> for <b>November<\/b> in &ldquo;First<b> <\/b>Sunday of <b>November<\/b>.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">Switch ($TimeZone.StandardMonth)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 1&nbsp; {$STNDMonth = &#8220;January&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 2&nbsp; {$STNDMonth = &#8220;February&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 3&nbsp; {$STNDMonth = &#8220;March&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 4&nbsp; {$STNDMonth = &#8220;April&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 5&nbsp; {$STNDMonth = &#8220;May&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 6&nbsp; {$STNDMonth = &#8220;June&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 7&nbsp; {$STNDMonth = &#8220;July&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 8&nbsp; {$STNDMonth = &#8220;August&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 9&nbsp; {$STNDMonth = &#8220;September&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 10 {$STNDMonth = &#8220;October&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 11 {$STNDMonth = &#8220;November&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 12 {$STNDMonth = &#8220;December&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;}#End Switch ($TimeZone.StandardMonth)<\/p>\n<p>The fun part of Windows PowerShell scripting is figuring out, with the above information, how to calculate the date, so let&rsquo;s break the code down.<\/p>\n<p>Below, I&rsquo;m grabbing the current year to work from.<\/p>\n<p style=\"padding-left: 30px\">$CurrentYear = (Get-Date).Year<\/p>\n<p>Next, using the information from the switch statements, I&rsquo;m going to cast the data into a <strong>DateTime<\/strong> object.&nbsp; For example, <b>$SDate<\/b> would equal <b>Thursday, November 1, 2012 2:00:00 AM<\/b>:<\/p>\n<p style=\"padding-left: 30px\">[DateTime]$SDate = &#8220;$STNDMonth 01, $CurrentYear $STNDHour`:00:00&#8221;<\/p>\n<p>We are now going to use a While loop to determine and calculate the actual change date. I&rsquo;m going to set <b>$i <\/b>to <b>0 <\/b>and run the While loop as long as <b>$i<\/b> is less than <b>$TimeZone.StandardDay<\/b>. The first If Statement checks to see if the <b>$SDate.DayOfWeek<\/b> equals the<b> $TimeZone.StandardDayOfWeek<\/b>. If not, we will add a day to the <b>$SDate<\/b> variable and continue to do so until <b>$SDate.DayOfWeek<\/b> equals <b>StandardDayOfWeek<\/b>.&nbsp;<\/p>\n<p>If you&rsquo;ve successfully followed along, we started out with <b>November 1, 2012<\/b> as the <b>$SDate<\/b> and continued to add days until reaching <b>November 4, 2012 <\/b>since the first If Statement is now true, <b>If ($SDate.DayOfWeek -eq $TimeZone.StandardDayOfWeek)<\/b>. The reason for this is because <b>November 4, 2012 <\/b>is on a Sunday and the <b>$TimeZone.StandardDayOfWeek<\/b> also equals Sunday <b>0<\/b>.&nbsp;<\/p>\n<p>At the moment, we&rsquo;ve incremented <b>$i<\/b> by <b>1<\/b> and <b>$SDate<\/b> equals <b>November 4, 2012<\/b>,<b> <\/b>and we will use the second If Statement <b>If ($i -eq $TimeZone.StandardDay)<\/b> to test if <b>$i<\/b> equals <b>$TimeZone.StandardDay<\/b>. In this case, it does, so we&rsquo;ll assign <b>$SFinalDate<\/b> = <b>$SDate<\/b> (<b>November 4, 2012<\/b>).<\/p>\n<p style=\"padding-left: 30px\">$i = 0<\/p>\n<p style=\"padding-left: 30px\">While ($i -lt $TimeZone.StandardDay)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ($SDate.DayOfWeek -eq $TimeZone.StandardDayOfWeek)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $i++<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ($i -eq $TimeZone.StandardDay)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $SFinalDate = $SDate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }#End If ($i -eq $TimeZone.StandardDay)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $SDate = $SDate.AddDays(1)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}#End Else<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }#End If ($SDate.DayOfWeek -eq $TimeZone.StandardDayOfWeek)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $SDate = $SDate.AddDays(1)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }#End Else<\/p>\n<p style=\"padding-left: 30px\">}#End While ($i -lt $TimeZone.StandardDay)<\/p>\n<p>Back to the reason why this doesn&#8217;t work with the Central Europe Time Zone &hellip; as you can see in the following image, the <b>StandardDay<\/b> equals <b>Last<\/b> or <b>5<\/b>. This will be an issue if there are only four Sundays in October, because according to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa394498(v=vs.85).aspx\" target=\"_blank\">MSDN Win32_TimeZone reference<\/a>, last equals 5. In this case, the loop logic will run through five times and will match the fifth Sunday of October to <b>November 4, 2012<\/b>, which, obviously, is incorrect because there are only four Sundays in October this year.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0066.hsg-10-23-12-3.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0066.hsg-10-23-12-3.png\" \/><\/a><\/p>\n<p>I thought for a minute and came up with this solution.<\/p>\n<p style=\"padding-left: 30px\">If ($SFinalDate.Month -ne $TimeZone.StandardMonth)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $SFinalDate = $SFinalDate.AddDays(-7)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; }#End If ($SFinalDate.Month -ne $TimeZone.StandardMonth)<\/p>\n<p>The above code is placed just after the loop, and it will evaluate whether the loop landing date &ldquo;month&rdquo; matches the month the Win32_TimeZone information returns. If they do not match, it will set the $SFinalDate back seven days so that the date will be correct. Therefore, handling either condition for <b>4<\/b> or <b>5<\/b> for <b>StandardDay<\/b> will calculate the date correctly.<\/p>\n<p>I have updated the function, and it is currently available in the <a href=\"http:\/\/gallery.technet.microsoft.com\/Get-DSTInfo-Determine-Time-8f7f9f91\">Script Repository on TechNet<\/a>.<\/p>\n<p>I hope you&rsquo;ve enjoyed exploring Windows PowerShell with me today. Until next time &hellip;<\/p>\n<p>Thank you, Brian, for sharing this update with us. Awesome job.<\/p>\n<p>Join me tomorrow when we will have another guest blogger.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Brian Wilhite, guest blogger, discusses a function to check for transition to daylight savings time. Microsoft Scripting Guy, Ed Wilson, is here. Today, we have a guest blogger. Brian Wilhite has been a guest in the past, and you can see his previous posts here. Brian is speaking at the Atlanta Windows PowerShell Saturday [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[327,56,3,45],"class_list":["post-4784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-brian-wilhite","tag-guest-blogger","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Brian Wilhite, guest blogger, discusses a function to check for transition to daylight savings time. Microsoft Scripting Guy, Ed Wilson, is here. Today, we have a guest blogger. Brian Wilhite has been a guest in the past, and you can see his previous posts here. Brian is speaking at the Atlanta Windows PowerShell Saturday [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4784"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4784\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}