c# - "Malay Peninsula Standard Time"的时区问题

标签 c# time

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

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

当我运行它时,出现以下异常:

System.TimeZoneNotFoundException:在本地计算机上找不到时区 ID“马来半岛标准时间”。

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

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

这次我也没有异常(exception),我收到了带有时区“马来半岛标准时间”的弹出消息

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

最佳答案

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

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

马来半岛标准时间

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

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\

如果您浏览它们,您会看到没有一个键名为“马来半岛标准时间”,该时区的值是“新加坡标准时间”。该时区的全部信息如下:

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 不存在,这就是我遇到异常的原因。我将注册表项重命名为“马来半岛标准时间”,问题就消失了。

关于c# - "Malay Peninsula Standard Time"的时区问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31589491/

相关文章:

ios - 更改 Audio Core 中回调之间的通话时间

c# - 如何从 DragDrop 事件定位 datagridview 行或单元格?

c# - 如何修复 "The type or namespace name could not be found"?

c# - 控制台应用程序 - 当前工作线上方的 WriteLine

c# - 从 Controller MVC 4 访问 UserProfile

php - 将纪元时间转换为 H :mm timestamp in PHP

python - 问题拆分日期时间 - 'str' 对象没有属性 'strptime'

java - 如何从当前时间戳中减去一个小时

php - 如何显示月份、日期和当前时间加十二个小时?

c# - 视觉 C# : Move multiple files with the same extensions into another directory