Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Use of System's TimeZoneInfo throws exceptions

Discussion in 'Scripting' started by jc_crash, Mar 11, 2020.

  1. jc_crash

    jc_crash

    Joined:
    Jul 30, 2019
    Posts:
    21
    Hi all,

    I am required to convert between different timezones when users input dates/times. The easiest way is to specify two TimeZoneInfo objects for the timezones to convert between (i.e. "Eastern Standard Time" and "Utc"), and then use TimeZoneInfo.ConvertTime() to do the conversion.

    I noticed this is throwing an exception so I tried using TimeZoneInfo.GetSystemTimeZones() to make sure I was passing the Conversion function something valid and THIS throws an exception also. This bug has been raised before here, and as you may notice it was never actually solved and was deemed a Unity bug. Instead, alternative 3rd party libraries have been offered as a solution which I would prefer not to use, if possible.

    Does anyone have any insights here?

    UPDATE:
    After enabling full stack trace exception handling in webgl I found the following details.

    TimeZoneInfo.GetSystemTimeZones() is not throwing an exception but it isn't returning anything either (I use it in a foreach loop):

    Code (CSharp):
    1. foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones()) {
    2.     Debug.Log(tz.Id);
    3. }
    The exception is being thrown by TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time") which in the stack trace calls System.TimeZoneInfo.FindSystemTimeZoneByIdCore which calls System.TimeZoneInfo.FindSystemTimeZoneByFileName. And the final exception message is:

    "Couldn't read time zone file /usr/share/zoneinfo/Eastern Standard Time"

    Full stack trace is attached as a .txt.

    Info: Building for WebGL using Unity 2019.1.12f1

    Thanks!
     

    Attached Files:

    Last edited: Mar 11, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Here's my guess.... In Mono, TimeZoneInfo.GetSystemTimeZones() ends up calling this line of code:

    Code (csharp):
    1. Interlocked.CompareExchange (ref systemTimeZones, new ReadOnlyCollection<TimeZoneInfo> (tz), null);
    https://github.com/mono/mono/blob/master/mcs/class/corlib/System/TimeZoneInfo.cs

    Interlocked is part of the System.Threading namespace.
    https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked?view=netframework-4.8

    System.Threading is specifically not supported on WebGL.
    https://docs.unity3d.com/Manual/webgl-gettingstarted.html
    If I'm correct this isn't a bug at all, instead a known and documented limitation of the platform. I didn't investigate ConvertTime all that far, because most of the work in ConvertTime is actually done by DateTime, and DateTime.cs also has the good old "using System.Threading;" at the top. So I'm just going to assume the problem is the same. Investigate it more yourself if you're interested.
     
    Last edited: Mar 11, 2020
    jc_crash likes this.
  3. jc_crash

    jc_crash

    Joined:
    Jul 30, 2019
    Posts:
    21
    Yes I double checked the Unity documentation this morning to see if they said anything about any specific System libraries and they didn't. I didn't count on them leveraging the Threading libraries though..

    I have updated my question above with more specific information on the exception, but I guess you're right in your findings! Looks like I might have to utilize 3rd party libraries after all.

    Thanks!
     
    Joe-Censored likes this.