"Malay Peninsula Standard Time" 的时区问题

Time zone issue with "Malay Peninsula Standard Time"

我有一个程序,运行在 C# 上有以下代码:

TimeZoneInfo localZone = TimeZoneInfo.Local;
string timeZone = TimeZoneInfo.FindSystemTimeZoneById(localZone.Id).ToString();
MessageBox.Show("Local Time Zone ID: " + timeZone);

当我运行这个时,我得到以下异常:

System.TimeZoneNotFoundException: 在本地计算机上找不到时区 ID 'Malay Peninsula Standard Time'。

我对代码稍作修改并使用了这一行:

TimeZoneInfo localZone = TimeZoneInfo.Local;
MessageBox.Show("Local Time Zone ID: " + localZone.StandardName);

这次我也没有例外,我收到了带有时区 "Malay Peninsula Standard Time"

的弹出消息

为什么第一个代码片段不起作用?

经过一段时间(几天)后,我终于知道发生了什么事。

调试代码,我看到 TimeZoneInfo.Local.Id 正在检索以下值:

马来半岛标准时间

我做了一些调查,发现服务器的所有时区都存储在这个注册表项上:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time 区域\

如果您浏览它们,您会看到没有键名为 "Malay Peninsula Standard Time",该时区的值是 "Singapore Standard Time"。该时区的全部信息如下:

ID: Singapore Standard Time
Display Name:      (GMT+08:00) Kuala Lumpur, Singapore
Standard Name:           Malay Peninsula Standard Time
Daylight Name:           Malay Peninsula Daylight Time   ***Does Not Have    Daylight Saving Time***
 Offset from UTC:                       8 hours, 0 minutes
 Number of adjustment rules:                          0

因此,如您所见,我要查找的 ID 不存在,这就是我遇到异常的原因。我将注册表项重命名为 "Malay Peninsula Standard Time",问题消失了。