46. Local time zone clocks

This example solution uses an approach similar to the one used by the preceding solution to store TimeZoneInfo objects for cities. It also uses the following code to declare an array to hold the cities' CultureInfo objects:

private CultureInfo[] CultureInfos;

The form's Load event handler initializes the arrays holding the clock labels and city TimeZoneInfo objects as before. It also includes the following code to initialize the cities' CultureInfo objects:

// Get the corresponding CultureInfo objects.
string[] cultureNames = { "en-US", "fr-FR", "en-GB", "JP-jp", "en-AU", };
CultureInfos = new CultureInfo[cultureNames.Length];
for (int i = 0; i < cultureNames.Length; i++)
CultureInfos[i] = new CultureInfo(cultureNames[i]);

This code creates an array holding the cities' culture codes. It then loops through those codes to create the corresponding CultureInfo objects.

Finally, the program uses the following code to update the clock labels:

// Display the time in the various time zones.
private void clockTimer_Tick(object sender, EventArgs e)
{
// Get the current time.
DateTime utcTime = DateTime.UtcNow;

// Display the times.
for (int i = 0; i < ClockLabels.Length; i++)
{
DateTime time = TimeZoneInfo.ConvertTimeFromUtc(utcTime,
TimeZoneInfos[i]);
ClockLabels[i].Text = time.ToString(
CultureInfos[i].DateTimeFormat.LongTimePattern);
}
}

This is similar to the previous program's code. The only difference is in the statement that sets the labels' text. In this version, the code calls the time structure's ToString method, passing that method the long time pattern for the city's locale.

Download the LocalTimeZoneClocks example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset