Recipe 7.2. Accessing the System s Time Zone


Recipe 7.2. Accessing the System's Time Zone

Problem

You want to programmatically determine the time-zone offset for the local system's time and determine if daylight savings time is currently in effect.

Solution

Sample code folder: Chapter 07 \SystemTimeZone

Use the TimeZone object, which provides properties and methods for determining the name of the current time zone, the number of hours offset from Greenwich Mean Time (GMT), and whether daylight savings is currently in effect.

Discussion

The following code shows how the TimeZone information is accessed:

 Dim theZone As TimeZone = TimeZone.CurrentTimeZone Dim result As New System.Text.StringBuilder result.Append("DaylightName: ").AppendLine( _    theZone.DaylightName) result.Append("StandardName: ").AppendLine( _    theZone.StandardName) result.Append("IsDaylightSavingTime(  Now): ").AppendLine( _    theZone.IsDaylightSavingTime(Now)) result.Append("GetUtcOffset(Now): ").AppendLine( _    theZone.GetUtcOffset(Now).ToString) result.Append("  System time is Local Time: ") result.AppendLine(Now.Kind = DateTimeKind.Local) result.Append("System time is Universal Coordinated Time: ") result.AppendLine(Now.Kind = DateTimeKind.Utc) result.Append("System time is Unspecified: ") result.AppendLine(Now.Kind = DateTimeKind.Unspecified) MsgBox(result.ToString()) 

The TimeZone variable theZone is assigned the current system's time-zone information in the first line of this code. The rest of the lines extract information from this variable and prepare string versions for display by appending to the StringBuilder. The theZone variable lets you determine the name of the time zone, the number of hours that time zone is offset from GMT, and whether daylight savings is currently in effect.

The Kind property determines if a Date represents local time, Coordinated Universal Time (UTC), or is unspecified. This is a property of a Date, not a TimeZone, but the information it provides is closely associated with the TimeZone information.

Figure 7-2 shows the TimeZone information displayed for a computer set to Central Standard Time during the summer.

Figure 7-2. Determining a system's time-zone information





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net