As all we know .Net’s (up to framework 2) Time Zone class is not much of use since it only gives as objects that are associate with the current time zone, therefore I decided to create a time zone class and called it
TimeZoneInfo that can give us information and methods for the other time zones as well. Mainly
TimeZoneInfo class uses the windows registry information under the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Time Zones” so it is critical that the registry includes the latest updates from MS.
The class provides all the information and the functionality for the time zones. For example it provides Daylight Saving Time dates for the time zone, Current UtcOffset, Current Time, Current System Time Zone etc.
Using the Class:
vb Code:
'Get all time zones on the system and show them in a ListBox control
Me.ListBox1.DataSource = TimeZoneInfo.GetTimeZones
vb Code:
Dim tzInfo As TimeZoneInfo
'Create new instans of a TimeZoneInfo for a specific time zone.
tzInfo = New TimeZoneInfo("Pacific Standard Time")
'Or
tzInfo = TimeZoneInfo.FromStandardName("Pacific Standard Time")
'Get the current UtcOffset of the time zone
Dim utcOffset As TimeSpan = tzInfo.CurrentUtcOffset
'Get the current time of the time zone
Dim time As DateTime = tzInfo.CurrentTime
'Get a value specifing if it is a dailight saving time currently for the time zone
Dim isDaylight As Boolean = tzInfo.IsDaylightSavingTime
'Get dailight changes of the time zone
Dim dStart, dEnd As DateTime
Dim dt As System.Globalization.DaylightTime
dt = tzInfo.GetDaylightChanges(2007)
dStart = dt.Start
dEnd = dt.End
'And much more
'...
'...
The class should work on 98/windows NT/2000/XP, but I am not sure if it will work on Vista. Please notify me about the results if you tried it on the other windows than XP.
Update History:
Quote:
09/04/2007 -- Original version.
09/17/2007 -- (Some internal minor changes to optimize the Sort functionality).
09/21/2007 -- (A small bag fix).
09/24/2007 -- (The CurrentTimeZone property of TimeZoneInfo was not getting the correct system current time zone after changing it in the code. This was happening because the .Net TimeZone.CurrentTimeZone has a bug and the TimeZoneInfo class was using this property of TimeZone to return the current system time zone. Now the class implements its own method by using GetTimeZoneInformation Api function which returns the correct system current time zone.
There are also some changes to the IsDaylightSavingTime and GetDaylightChanges functions to optimize and fix some small bugs.)
Please feel free to rate this post , comment and notify me about any problem associated with it.