แก้ไข

แชร์ผ่าน


How to: Enumerate time zones present on a computer

Successfully working with a designated time zone requires that information about that time zone be available to the system. The Windows XP and Windows Vista operating systems store this information in the registry. However, although the total number of time zones that exist throughout the world is large, the registry contains information about only a subset of them. In addition, the registry itself is a dynamic structure whose contents are subject to both deliberate and accidental change. As a result, an application cannot always assume that a particular time zone is defined and available on a system. The first step for many applications that use time zone information applications is to determine whether required time zones are available on the local system, or to give the user a list of time zones from which to select. This requires that an application enumerate the time zones defined on a local system.

Note

If an application relies on the presence of a particular time zone that may not be defined on a local system, the application can ensure its presence by serializing and deserializing information about the time zone. The time zone can then be added to a list control so that the application user can select it. For details, see How to: Save Time Zones to an Embedded Resource and How to: Restore time zones from an embedded resource.

To enumerate the time zones present on the local system

  1. Call the TimeZoneInfo.GetSystemTimeZones method. The method returns a generic ReadOnlyCollection<T> collection of TimeZoneInfo objects. The entries in the collection are sorted by their DisplayName property. For example:

    ReadOnlyCollection<TimeZoneInfo> tzCollection;
    tzCollection = TimeZoneInfo.GetSystemTimeZones();
    
    Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones
    
  2. Enumerate the individual TimeZoneInfo objects in the collection by using a foreach loop (in C#) or a For EachNext loop (in Visual Basic), and perform any necessary processing on each object. For example, the following code enumerates the ReadOnlyCollection<T> collection of TimeZoneInfo objects returned in step 1 and lists the display name of each time zone on the console.

    foreach (TimeZoneInfo timeZone in tzCollection)
        Console.WriteLine($"   {timeZone.Id}: {timeZone.DisplayName}");
    
    For Each timeZone As TimeZoneInfo In tzCollection
        Console.WriteLine("   {0}: {1}", timeZone.Id, timeZone.DisplayName)
    Next
    

To present the user with a list of time zones present on the local system

  1. Call the TimeZoneInfo.GetSystemTimeZones method. The method returns a generic ReadOnlyCollection<T> collection of TimeZoneInfo objects.

  2. Assign the collection returned in step 1 to the DataSource property of a Windows forms or ASP.NET list control.

  3. Retrieve the TimeZoneInfo object that the user has selected.

The example provides an illustration for a Windows application.

Example

The example starts a Windows application that displays the time zones defined on a system in a list box. The example then displays a dialog box that contains the value of the DisplayName property of the time zone object selected by the user.

private void Form1_Load(object sender, EventArgs e)
{
    ReadOnlyCollection<TimeZoneInfo> tzCollection;
    tzCollection = TimeZoneInfo.GetSystemTimeZones();
    _timeZoneList.DataSource = tzCollection;
}

private void OkButton_Click(object sender, EventArgs e)
{
    TimeZoneInfo? selectedTimeZone = (TimeZoneInfo?)_timeZoneList.SelectedItem;
    MessageBox.Show($"You selected the {selectedTimeZone?.ToString()} time zone.");
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo)
    tzCollection = TimeZoneInfo.GetSystemTimeZones()
    Me.timeZoneList.DataSource = tzCollection
End Sub

Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
    Dim selectedTimeZone As TimeZoneInfo = DirectCast(Me.timeZoneList.SelectedItem(), TimeZoneInfo)
    MsgBox("You selected the " & selectedTimeZone.ToString() & " time zone.")
End Sub

Most list controls (such as the System.Windows.Forms.ListBox or System.Web.UI.WebControls.BulletedList control) allow you to assign a collection of object variables to their DataSource property as long as that collection implements the IEnumerable interface. (The generic ReadOnlyCollection<T> class does this.) To display an individual object in the collection, the control calls that object's ToString method to extract the string that is used to represent the object. In the case of TimeZoneInfo objects, the ToString method returns the TimeZoneInfo object's display name (the value of its DisplayName property).

Note

Because list controls call an object's ToString method, you can assign a collection of TimeZoneInfo objects to the control, have the control display a meaningful name for each object, and retrieve the TimeZoneInfo object that the user has selected. This eliminates the need to extract a string for each object in the collection, assign the string to a collection that is in turn assigned to the control's DataSource property, retrieve the string the user has selected, and then use this string to extract the object that it describes.

Compiling the code

This example requires:

See also