TimeZoneInfo.FindSystemTimeZoneById() throws exception in a Unity application

回眸只為那壹抹淺笑 提交于 2019-11-27 08:07:13

问题


If I execute this code in a C# console application, it works fine.

TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
Console.WriteLine(easternZone.DisplayName);

However, when I use the same method in a Unity application, an exception is thrown:

System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.FindSystemTimeZoneByFileName (System.String id, System.String filepath) [0x00000] in <filename unknown>:0
  at System.TimeZoneInfo.FindSystemTimeZoneById (System.String id) [0x00000] in <filename unknown>:0
  ...

A curious thing that I've noticed is that the exception is thrown in a method named "FindSystemTimeZoneByFileName" when the MSDN documentation explicitly says that the information is retrieved from the Registry.


回答1:


Unity applications use Mono, and can target non-Windows systems - so the registry information is not always available. It appears that Mono will use whatever time zone information is available on the system, whether that happens to be Windows time zones, or IANA time zones - so, you may need to check for one or the other, or both:

TimeZoneInfo easternZone;

try
{
    easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
}
catch (TimeZoneNotFoundException)
{
    easternZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}

Of course, you can reverse these if you typically are going to be running on non-Windows platforms. If neither are found, then it will still throw an exception.

You can review the list of IANA time zones here. You may also want to read the timezone tag wiki to understand the distinction.

Update: As Shaul's answer pointed out, you can now use my TimeZoneConverter library to accomplish this. While the above code is no longer required, you can now simply do this instead:

TimeZoneInfo easternZone = TZConvert.GetTimeZoneInfo(timeZoneName);

The timeZoneName parameter can be either America/New_York or Eastern Standard Time.




回答2:


For the record, there's a nice little package called TimeZoneConverter that will do this for you without having to catch exceptions:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
    timeZoneName = TZConvert.WindowsToIana(timeZoneName);
}
var zoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName); 


来源:https://stackoverflow.com/questions/35043022/timezoneinfo-findsystemtimezonebyid-throws-exception-in-a-unity-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!