The .NET Developer Community

[RESOLVED] Old School Time Zone Info

rated by 0 users
This post has 8 Replies | 1 Follower

Tanner65
Not Ranked
Dallas, TX
Since 5/24/2008
Posts 124
Reputation 1,595
Tanner65 (Tanner65) Posted: 3/13/2009 9:49 AM
So, I'm working on a short term project for one of our customers here in which I need to write in .Net 1.1 a configuration program. Everything else I've gotten down pat, and have no problems with except the bloody time zone lists.

Essentially, I need to populate a combo box with the lists of time zones, similarly to what the date/time properties does, determine which one was selected and set the time zone accordingly.

I've read through plenty of threads regarding time zone population and manipulation and understand how to set it (currently using command line, why change what works :D ) but can't quite grasp the population in .Net 1.1.

I can handle the figuring out which one was called so long as I can find a procedure that will pull the time zone information from the machine. I don't care the language, C#, VB, hell even C or C++ will do at this point. This is my only hold up on this project.

Thanks!


Summary: How do I pull all time zones from the machine in any .Net 1.1 language?

---------------------------------------------------------------------------
Disclaimer: I'm not always right, so feel free to let me know!

Quote:
Act in haste, look a fool for a eternity.


---------------------------------------------------------------------------

  • | Post Points: 80
Kulrom
Top 50 Contributor
Republic of Macedonia
Since 5/29/2006
Posts 2,864
Reputation 17,829
Hmm i have not VS 2003 so i cannot test if it works with .NET FW 1.1 but it certainly works with FW 2.0 and above.


Code:

Dim timeZone As TimeZoneInfo
For Each timeZone In TimeZoneInfo.GetSystemTimeZones
     Me.ComboBox1.Items.Add(timeZone.DisplayName)
Next


You may try this class as well http://www.codeproject.com/KB/vb/TimeZoneInfo.aspx


  • | Post Points: 5
Tanner65
Not Ranked
Dallas, TX
Since 5/24/2008
Posts 124
Reputation 1,595

Quote:
Posted by Kulrom on 3/13/2009 11:06:06 AM (PST):

Hmm i have not VS 2003 so i cannot test if it works with .NET FW 1.1 but it certainly works with FW 2.0 and above.


Code:

Dim timeZone As TimeZoneInfo
For Each timeZone In TimeZoneInfo.GetSystemTimeZones
     Me.ComboBox1.Items.Add(timeZone.DisplayName)
Next


You may try this class as well http://www.codeproject.com/KB/vb/TimeZoneInfo.aspx



Hey there Kulrom,

I've tried the link and it's only for .Net 2.0 and up.

I've tried every incarnation of TimeZone I can find and none of them have a procedure to GetSystemTimeZones. :-( Thanks for the tip though!

Any more suggestions?

---------------------------------------------------------------------------
Disclaimer: I'm not always right, so feel free to let me know!

Quote:
Act in haste, look a fool for a eternity.


---------------------------------------------------------------------------

  • | Post Points: 5
Kulrom
Top 50 Contributor
Republic of Macedonia
Since 5/29/2006
Posts 2,864
Reputation 17,829
Hi Tanner,

Could you please test something along these lines:

Code:

        Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\", True)
        For Each sName As String In rKey.GetSubKeyNames
            Dim regKey As RegistryKey = rKey.OpenSubKey(sName, True)
            ComboBox1.Items.Add(CType(regKey.GetValue("Display"), String))
        Next


I think this would work in FW 1.1 :)
  • | Post Points: 5
Kulrom
Top 50 Contributor
Republic of Macedonia
Since 5/29/2006
Posts 2,864
Reputation 17,829
Or even better sort the items before you add them to the combo.

Code:

        Dim arr As New ArrayList
        Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\", True)
        For Each sName As String In rKey.GetSubKeyNames
            Dim regKey As RegistryKey = rKey.OpenSubKey(sName, True)
            arr.Add(CType(regKey.GetValue("Display"), String))
        Next
        arr.Sort()
        For Each Str As String In arr
            ComboBox1.Items.Add(Str)
        Next


HTH
Regards :thumb:
  • | Post Points: 5
Tanner65
Not Ranked
Dallas, TX
Since 5/24/2008
Posts 124
Reputation 1,595

Quote:
Posted by Kulrom on 3/13/2009 2:54:12 PM (PST):

Or even better sort the items before you add them to the combo.

Code:

        Dim arr As New ArrayList
        Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\", True)
        For Each sName As String In rKey.GetSubKeyNames
            Dim regKey As RegistryKey = rKey.OpenSubKey(sName, True)
            arr.Add(CType(regKey.GetValue("Display"), String))
        Next
        arr.Sort()
        For Each Str As String In arr
            ComboBox1.Items.Add(Str)
        Next


HTH
Regards :thumb:



Woah! That's exactly what I'm looking for. Thank you thank you thank you!

This is going to sound like an odd question, but how would I sort this similarly to the windows does? Currently it sorts as: GMT, +1 to +12, -1 to -12. I need -12 to -1, GMT, +1 to +12.

In theory, if I said, give me all strings < 0 and sorted those in reverse, got all strings > 0 and sorted those and put the GMT in between them, then it would be exactly alike?

I'm on spring break, so I told myself I'm going to take off Friday and Saturday, so I won't have a solution until Sunday or Monday!


Thank you very much Kulrom!

---------------------------------------------------------------------------
Disclaimer: I'm not always right, so feel free to let me know!

Quote:
Act in haste, look a fool for a eternity.


---------------------------------------------------------------------------

  • | Post Points: 5
Kulrom
Top 50 Contributor
Republic of Macedonia
Since 5/29/2006
Posts 2,864
Reputation 17,829
Well it can be sorted out. You just need to compare the time zones when you sort the arraylist.
  • | Post Points: 5
Tanner65
Not Ranked
Dallas, TX
Since 5/24/2008
Posts 124
Reputation 1,595
So, I combined your code and a custom class to make exactly what I wanted. See below:

Do you see any way to make it more efficient? It runs quickly, but I want to make sure it's "correct."

Select time zone and click Go:
Go Procedure:

Code:

    Private Sub butConfigure_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butConfigure.Click
        MessageBox.Show(Me.m_TimeZoneCollection.Item(DirectCast(Me.cboTimeZone.SelectedItem.ToString, String)).TimeZoneName)
        Shell(String.Format("Control.exe TIMEDATE.CPL,,/Z {0}", Me.m_TimeZoneCollection.Item(DirectCast(Me.cboTimeZone.SelectedItem.ToString, String)).TimeZoneName))

    End Sub


Procedures:

Code:
'Populates the TimeZoneCollection with the TimeZoneInformation and populates the TimeZone combobox
    Private Sub GetTimeZones()
        Dim arr As New ArrayList

        If m_TimeZoneCollection Is Nothing Then m_TimeZoneCollection = New TimeZoneInformationCollection

        Dim rKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\", True)
        For Each sName As String In rKey.GetSubKeyNames
            Dim regKey As RegistryKey = rKey.OpenSubKey(sName, True)

            If Not (DirectCast(regKey.GetValue("Display"), String).IndexOf("Old") > -1) Then
               m_TimeZoneCollection.Add(DirectCast(regKey.GetValue("Display"), String), New TimeZoneInformation(DirectCast(regKey.GetValue("Display"), String), DirectCast(regKey.GetValue("STD"), String), regKey.Name, New TimeZoneInformation.DayLightSavingsTime(regKey.Name, DirectCast(regKey.GetValue("DLT"), String))))
                arr.Add(regKey.GetValue("Display"))
            End If
        Next

        arr = SortTimeZoneArray(arr)

        For Each Str As String In arr
            Me.cboTimeZone.Items.Add(Str)
        Next
    End Sub '/GetTimeZones

    'Sorts time zones to appear as they do in the Date/Time properties
    Private Function SortTimeZoneArray(ByVal FromArray As ArrayList) As ArrayList
        Dim PosArray As New ArrayList
        Dim NegArray As New ArrayList
        Dim GMTArray As New ArrayList
        Dim arrReturn As New ArrayList

        For Each item As String In FromArray
            If item.IndexOf("+") > -1 Then
                PosArray.Add(item)
            ElseIf item.IndexOf("-") > -1 Then
                NegArray.Add(item)
            Else
                GMTArray.Add(item)
            End If
        Next
        PosArray.Sort()
        NegArray.Sort() : NegArray.Reverse()
        GMTArray.Sort()

        For Each item As String In NegArray
            arrReturn.Add(item)
        Next
        For Each item As String In GMTArray
            arrReturn.Add(item)
        Next
        For Each item As String In PosArray
            arrReturn.Add(item)
        Next

        Return arrReturn
    End Function '/SortTimeZoneArray


Custom Classes:

Code:
Friend Class TimeZoneInformation

    'The DLT setting for this time zone
    Private m_DLST As DayLightSavingsTime
    'The name displayed in the combo box
    Private m_TimeZoneDisplay As String
    'The time zone name listed inside the registry location
    Private m_TimeZoneName As String
    'The registry location of the data
    Private m_TimeZoneRegistry As String

    'Initializes a new instance of the TimeZoneInformation class
    Public Sub New(ByVal TimeZoneDisplay As String, ByVal TimeZoneName As String, ByVal TimeZoneRegistry As String)
        m_TimeZoneDisplay = TimeZoneDisplay
        m_TimeZoneName = TimeZoneName
        m_TimeZoneRegistry = TimeZoneRegistry
        m_DLST = Nothing
    End Sub '/New

    'Initializes a new instance of the TimeZoneInformation class
    Public Sub New(ByVal TimeZoneDisplay As String, ByVal TimeZoneName As String, ByVal TimeZoneRegistry As String, ByVal DLST As DayLightSavingsTime)
        m_TimeZoneDisplay = TimeZoneDisplay
        m_TimeZoneName = TimeZoneName
        m_TimeZoneRegistry = TimeZoneRegistry
        m_DLST = DLST
    End Sub '/New

    'Used to hold the DST information
    Public Property DLST() As DayLightSavingsTime
        Get
            Return m_DLST
        End Get
        Set(ByVal Value As DayLightSavingsTime)
            m_DLST = Value
        End Set
    End Property '/DLST

    'The time zone name listed inside the registry location
    Public Property TimeZoneName() As String
        Get
            Return m_TimeZoneName
        End Get
        Set(ByVal Value As String)
            m_TimeZoneName = Value
        End Set
    End Property '/TimeZoneName

    'The registry location of the data
    Public Property TimeZoneRegistry() As String
        Get
            Return m_TimeZoneRegistry
        End Get
        Set(ByVal Value As String)
            m_TimeZoneRegistry = Value
        End Set
    End Property '/TimeZoneRegistry

    'Used to hold the DST information
    Friend Class DayLightSavingsTime

        'The time zone name listed inside the registry location
        Private m_TimeZoneName As String
        'The registry location of the data
        Private m_TimeZoneRegistry As String

        'Initializes a new instance of the DayLightSavingsTime class
        Public Sub New(ByVal TimeZoneRegistry As String, ByVal TimeZoneName As String)
            m_TimeZoneName = TimeZoneName
            m_TimeZoneRegistry = TimeZoneRegistry
        End Sub '/New

        'The time zone name listed inside the registry location
        Public Property TimeZoneName() As String
            Get
                Return m_TimeZoneName
            End Get
            Set(ByVal Value As String)
                m_TimeZoneName = Value
            End Set
        End Property '/TimeZoneName

        'The registry location of the data
        Public Property TimeZoneRegistry() As String
            Get
                Return m_TimeZoneRegistry
            End Get
            Set(ByVal Value As String)
                m_TimeZoneRegistry = Value
            End Set
        End Property '/TimeZoneRegistry
    End Class
End Class

Friend Class TimeZoneInformationCollection
    Inherits DictionaryBase

    'Holds the TimeZoneInformation class
    Private m_TimeZoneInformation As TimeZoneInformation

    'Returns the existance of the key in the dictionary
    Public Function Contains(ByVal key As String) As Boolean
        Return Dictionary.Contains(key)
    End Function '/Contains

    'Adds a new TimeZoneInformation instance into the dictionary
    Public Sub Add(ByVal key As String, ByVal value As TimeZoneInformation)
        Dictionary.Add(key, value)
    End Sub '/Add

    'Removes a key from the dictionary
    Public Sub Remove(ByVal key As String)
        Dictionary.Remove(key)
    End Sub '/Remove

    Default Public Property Item(ByVal key As String) As TimeZoneInformation
        Get
            Return CType(Dictionary(key), TimeZoneInformation)
        End Get
        Set(ByVal Value As TimeZoneInformation)
            Dictionary(key) = Value
        End Set
    End Property '/Item

    'Returns the selected TimeZoneInformation
    Public Property TimeZoneInformation() As TimeZoneInformation
        Get
            Return m_TimeZoneInformation
        End Get
        Set(ByVal Value As TimeZoneInformation)
            m_TimeZoneInformation = Value
        End Set
    End Property '/TimeZoneInformation
End Class


Thanks again for your help! Hopefully my work will help some other poor soul in the future.

---------------------------------------------------------------------------
Disclaimer: I'm not always right, so feel free to let me know!

Quote:
Act in haste, look a fool for a eternity.


---------------------------------------------------------------------------

  • | Post Points: 5
Kulrom
Top 50 Contributor
Republic of Macedonia
Since 5/29/2006
Posts 2,864
Reputation 17,829
It seems ok to me. Go for it! :thumb:


  • | Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright 1998-2017 vbCity.com LLC