Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are some time zones returned by GetSystemTimeZones not found by FindSystemTimeZoneById?

Tags:

c#

.net

I've got an odd problem I can't seem to resolve. When I call TimeZoneInfo.GetSystemTimeZones on my Win 7 x64 machine I get 101 results. When I call TimeZoneInfo.FindSystemTimeZoneById on each of these and pass the StandardName attribute of the TimeZoneInfo object, 3 of them throw TimeZoneNotFoundException.

Here's a sample:

var tzs = TimeZoneInfo.GetSystemTimeZones();

foreach (var timeZoneInfo in tzs.OrderBy(t => t.BaseUtcOffset))
{
  try
  {
    TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.StandardName);
  }
  catch (TimeZoneNotFoundException)
  {
    Console.WriteLine(timeZoneInfo.DisplayName + "|" + timeZoneInfo.StandardName + "|" + timeZoneInfo.BaseUtcOffset);
  }
}

Console.ReadLine();

This has trouble finding "Coordinated Universal Time", "Jerusalem Standard Time" and "Malay Peninsula Standard Time". Taking a case like Malaysia, I can see an entry for it when I look at the available time zones in my regional settings, although it's showing the DisplayName attribute rather than the StandardName:

Time zones

However, I can't see it under either name when browsing the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones

What's going on here? Why can't the Malaysia time zone be loaded by name?

Please no alternative time zone implementations using other libraries - I just want to get to the bottom of this issue for now. Thanks!

like image 867
Troy Hunt Avatar asked Feb 10 '12 09:02

Troy Hunt


1 Answers

TimeZoneInfo.FindSystemTimeZoneById method accepts the time zone id as parameter. You're using timeZoneInfo.StandardName instead.

It seems, that for these 3 zones values for TimeZoneInfo.StandardName and TimeZoneInfo.Id properties are different. Using this:

TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.Id);

will solve the issue.

like image 112
Oleks Avatar answered Oct 04 '22 20:10

Oleks