“马来半岛标准时间"的时区问题 [英] Time zone issue with "Malay Peninsula Standard Time"

查看:52
本文介绍了“马来半岛标准时间"的时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program that runs the following code on C#:

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

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

When I run this, I got the following exception:

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

System.TimeZoneNotFoundException: The time zone ID 'Malay Peninsula Standard Time' was not found on the local computer.

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

I made a slight modification on the code and used this line:

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

而这次我也没有例外,我收到了时区为马来半岛标准时间"的弹出消息

And this time I got no exception, and I got the popup message with the time zone "Malay Peninsula Standard Time"

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

Why the first fragment of code is not working?

推荐答案

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

After some time (days), I finally found out what was happening.

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

Debugging the code, I see that TimeZoneInfo.Local.Id was retrieving the following value:

马来半岛标准时间

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

I made some investigation, and found out that all the time zones for the server are stored on this registry key:

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

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

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

If you browse through them, you will see that no key is called "Malay Peninsula Standard Time", the value that is for that time zone is "Singapore Standard Time". The whole information for that time zone is the following:

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 不存在,这就是我收到该异常的原因.我将注册表项重命名为马来半岛标准时间",问题就解决了.

So, as you see, the ID that I was looking for did not exist, and that's why I got that exception. I renamed the registry key to "Malay Peninsula Standard Time" and the problem was gone.

这篇关于“马来半岛标准时间"的时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆