首页 文章

TimeZoneInfo.FindSystemTimeZoneById()不适用于“海湾标准时间”

提问于
浏览
0

我使用下面的代码来获取“海湾标准时间”的时区详细信息,但其抛出错误如下所示

The time zone ID 'Gulf Standard Time' was not found on the local computer.

下面是我正在使用的代码行

TimeZoneInfo tZone = TimeZoneInfo.FindSystemTimeZoneById("Gulf Standard Time");

你能否告诉我代码中究竟是什么问题,因为我已经检查了它的正确时区名称 .

1 回答

  • 0

    正如评论中指出的那样, "Gulf Standard Time" 不是有效的Windows时区标识符 .

    海湾标准时间通常是指UTC 04:00没有DST,如在阿拉伯联合酋长国和阿曼观察到的,as described here . Windows中的相应时区显示为英文显示名称 (UTC+04:00) Abu Dhabi, Muscat ,并且具有相应的ID Arabian Standard Time .

    因此在.NET中:

    TimeZoneInfo tZone = TimeZoneInfo.FindSystemTimeZoneById("Arabian Standard Time");
    Console.WriteLine(tzone.DisplayName);
    
    // prints:  (UTC+04:00) Abu Dhabi, Muscat
    

    要获取支持的时区列表,请在.NET代码中使用 TimeZoneInfo.GetSystemTimeZones() ,并检查 IdDisplayName 属性 . 或者,您可以在命令行上调用 TZUTIL /L 来列出它们 .

    另外,只是要指出这一切都假设您在Windows上运行 . 如果您实际上在非Windows系统(Linux,OSX等)上运行.NET Core,则应使用IANA时区ID . 在这种情况下, "Asia/Dubai""Asia/Muscat" 是合适的 .

    如果您的代码可能在Windows和非Windows系统上运行,那么您将需要利用我的TimeZoneConverter库 .

相关问题