Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


October 09, 2006

Don't Let Daylight Saving Time Sneak Up on You

Script determines when those fateful Sundays occur
RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    Daylight Saving Time Is Changing in 2007

Download the Code Here

How GetDLSDates.vbs
Works Listing 1 shows GetDLSDates.vbs This script starts by creating an array named arDOW to hold the values of the seven days of the week. The array is modeled after how the StandardDayOfWeek and DaylightDayOfWeek properties values are defined. For example, array element 0 contains the value of "Sunday" and array element 6 contains the value of "Saturday." I'll describe how this array comes into play shortly.

Next, GetDLSDates.vbs gathers a collection of the local machine's time zone property values by querying the Win32_TimeZone class. When the DaylightBias property value is 0, the script immediately exits with a descriptive message. A value of 0 indicates that the Windows clock isn't set to automatically adjust for daylight saving time, possibly because a time zone (e.g., the Seoul time zone) or location (e.g., Hawaii) doesn't observe daylight saving time. When the DaylightBias property value isn't 0, the script uses a user-defined function named DLS and VBScript's TimeSerial function to display the starting and ending dates and time of daylight saving time. GetDLSDates.vbs displays the results in two dialog boxes, which show when daylight saving time begins and ends.

The DLS function determines the starting and ending dates (but not the time) of daylight saving time for the current year. As callout A in Listing 1 shows, this function takes three parameters: Mnth, occurrence, and dow. When the script is determining the starting date of daylight saving time, it uses the DaylightMonth, DaylightDay, and DaylightDayOfWeek properties to obtain the values for these parameters. When the script is determining the ending date, it uses the StandardMonth, StandardDay, and StandardDayOfWeek properties.

To determine the starting and ending times of daylight saving time, the script uses the DaylightHour, DaylightMinute, and DaylightSecond properties to obtain the starting time and the StandardHour, Standard-Minute, and StandardSecond properties to obtain the ending time. The TimeSerial function transforms these properties' values into a format that's easy to read.

The DLS function is the heart of GetDLSDates.vbs, so let's take a closer look at it. When this function receives the values for the Mnth, occurrence, and dow parameters, it first checks to make sure that the occurrence variable's value isn't greater than 5, as the code at callout B shows. This will never be the case if you're evaluating WMI's DaylightDay or StandardDay property. However, I included this code in case you want to use the function to get the date of a particular occurrence of a weekday in a given month (e.g., get the date of the last Saturday in December). The GetDLSDates.vbs script that you can download from the Windows Scripting Solutions Web site (see page 1 for download information) includes code for this purpose.

Next, the DLS function obtains the current year with VBScript's Now function and assigns that value to a variable named yr. The function then uses VBScript's MonthName function to turn the numerical value in the Mnth parameter to a string (e.g., turns the value of 3 into the string March) and assigns that string to the MName variable.

As I mentioned previously, Get-DLSDates.vbs works year after year, without having to change any code. To achieve this capability, the script uses a For...Next statement. For example, suppose you run this script next year. The For...Next loop first determines the first Sunday of the month that's specified in the MName variable. To make this determination, the code at callout C creates a string that includes the value in MName (in this case, March), one of the counter (i) variable's possible values (e.g., 1), and the value in yr (in this case, 2007). Using VBScript's CDate function, the loop transforms the resulting string (e.g., March 1, 2007) to a Date data type (e.g., 3/1/2007). Using VBScript's Weekday function, the code gets the value representing the weekday for that date (in this case, 4, which represents Thursday), then uses VBScript's WeekdayName function to transform that value into a string (i.e., Thursday).

At this point, the arDOW array comes into play. Recall that the dow parameter holds either the DaylightDayOfWeek or StandardDayOfWeek property value, which can range from 0 to 6. The loop uses this value within the arDOW array to get the associated string (e.g., arDOW(0) = "Sunday"). The loop then compares the WeekdayName function's string to arDOW's string. When the strings match, you have the date of first Sunday of the specified month (in this case, March 4). The If...Then loop uses this date with specified occurrence (i.e., the value in the occurrence variable) to find when saving daylight time starts or ends. As the code at callout D shows, the following equation is used

i + (7 * (occurrence-1)) 

The subtraction of 1 from the occurrence variable's value is necessary because you already have the first occurrence stored in the i variable.

For example, let's say that the DaylightMonth value is March, the DaylightDayOfWeek value is Sunday, and the DaylightDay value is 2 (i.e., the occurrence is the second Sunday of the month). After the code at callout C determines that the first Sunday of the month is March 4 (i.e., i = 4), the code at callout D calculates the second occurrence of Sunday with the equation

4 + (7 * (2-1)) 

So, in this case, saving daylight time starts on March 11.

Let's look at another example. Suppose that in the future, the US government implements a new Energy Policy Act, which changes the end of the daylight saving time to the fifth Sunday of August. In this scenario, the StandardMonth value is August, the StandardDayOfWeek value is Sunday, and the StandardDay value is 5. After the code at callout C finds the first Sunday of the month (e.g., i = 6), the code at callout D calculates the second occurrence of Sunday with the equation

6 + (7 * (5-1)) 

which produces a value of 34—as you know, there's no August 34. The script compensates for situations like this by checking the calculated day against the total number of days in any given month, as callout E shows. If the calculated day is greater than the number of days in the month, the code subtracts 7 from the calculated value, which gives the correct date. In this scenario, the code subtracts 7 from 34, which means that daylight saving time ends on August 27.

The DLS function ends by creating a string that consists of the month, the calculated day, and the year. The function then uses the CDate function to convert the string to a date data type, which GetDLSDates.vbs displays in a message box.

Note that although you can use GetDLSDates.vbs year after year to display daylight saving time information, you can't use this script to find this information for past or future years. This limitation lies in the Win32 _TimeZone class properties: They're specific to the current year only.

   Previous  1  [2]  3  Next 


Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement