Results 1 to 12 of 12

Thread: Time Zone Class

Threaded View

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Time Zone Class

    Last updated: 09/24/2007

    Some times I update the component and post the updated date and the assembly version number so it is recommended to check it once a while.

    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:
    1. 'Get all time zones on the system and show them in a ListBox control
    2.  Me.ListBox1.DataSource = TimeZoneInfo.GetTimeZones
    vb Code:
    1. Dim tzInfo As TimeZoneInfo
    2.  'Create new instans of a TimeZoneInfo for a specific time zone.
    3.  tzInfo = New TimeZoneInfo("Pacific Standard Time")
    4.  'Or
    5.  tzInfo = TimeZoneInfo.FromStandardName("Pacific Standard Time")
    6.  'Get the current UtcOffset of the time zone
    7.  Dim utcOffset As TimeSpan = tzInfo.CurrentUtcOffset
    8.  'Get the current time of the time zone
    9.  Dim time As DateTime = tzInfo.CurrentTime
    10.  'Get a value specifing if it is a dailight saving time currently for the time zone
    11.  Dim isDaylight As Boolean = tzInfo.IsDaylightSavingTime
    12.  'Get dailight changes of the time zone
    13.  Dim dStart, dEnd As DateTime
    14.  Dim dt As System.Globalization.DaylightTime
    15.  dt = tzInfo.GetDaylightChanges(2007)
    16.  dStart = dt.Start
    17.  dEnd = dt.End
    18.  
    19.  'And much more
    20.  '...
    21.  '...
    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:
    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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width