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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…