Juri Knauth
Juri Knauth

Reputation: 379

How to get TimeZoneInfo on Android, using Unity 2020.3.0f1?

Since the Google Play Leaderboard Servie is in the Pacific Standard Time Zone, I want get the local time there to update a calendar, which unlocks daily levels. Once the player actually launches a level I fetch the time from a server. However at application launch I already wanted to set the time for the calendar. However I just can't seem to get any TimeZoneInfo.

        public static DateTime GetDatePacificStandardTime(this DateTime value)
    {
#if UNITY_ANDROID && !UNITY_EDITOR

        try
        {
            AndroidJavaClass Java = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

            AndroidJavaObject AndroidActivity = Java.GetStatic<AndroidJavaObject>("currentActivity");
            TimeZone timeZone = AndroidActivity.Call<TimeZone>("getTimeZone", "America/Los_Angeles");

            TimeSpan difference = timeZone.GetUtcOffset(value);

            return value.Add(difference);
        }
        catch(Exception exception)
        {
            Debug.Log("AndroidActivity.Call failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }
        catch (Exception exception)
        {
            Debug.Log("TimeZoneInfo.FindSystemTimeZoneById Pacific Standard Time failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TimeZoneInfo.FindSystemTimeZoneById("Central Pacific Standard Time"));
        }
        catch (Exception exception)
        {
            Debug.Log("TimeZoneInfo.FindSystemTimeZoneById Central Pacific Standard Time failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TimeZoneInfo.FindSystemTimeZoneById("PST"));
        }
        catch (Exception exception)
        {
            Debug.Log("TimeZoneInfo.FindSystemTimeZoneById PST failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TZConvert.GetTimeZoneInfo("PST"));
        }
        catch (Exception exception)
        {
            Debug.Log("TZConvert.GetTimeZoneInfo PST failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TZConvert.GetTimeZoneInfo("Pacific Standard Time"));
        }
        catch (Exception exception)
        {
            Debug.Log("TZConvert.GetTimeZoneInfo Pacific Standard Time failed: " + exception);
        }

        try
        {
            return TimeZoneInfo.ConvertTimeFromUtc(value, TZConvert.GetTimeZoneInfo("Central Pacific Standard Time"));
        }
        catch (Exception exception)
        {
            Debug.Log("TZConvert.GetTimeZoneInfo Central Pacific Standard Time failed: " + exception);
        }

        return value;
#endif
        return TimeZoneInfo.ConvertTimeFromUtc(value, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));

    }
  1. LogCat gives me
    • 03-27 18:44:58.362: I/Unity(25251): AndroidActivity.Call failed: System.Exception: JNI: Unknown signature for type 'System.TimeZone' (obj = System.TimeZone) equal
    • 03-27 18:44:58.362: I/Unity(25251): AndroidActivity.Call failed: System.Exception: JNI: Unknown signature for type 'System.TimeZone' (obj = System.TimeZone) equal
    • 03-27 18:44:58.365: I/Unity(25251): TimeZoneInfo.FindSystemTimeZoneById Pacific Standard Time failed: System.TimeZoneNotFoundException: Couldn't read time zone file /usr/share/zoneinfo/Pacific Standard Time ---> System.IO.DirectoryNotFoundException: Could not find a part of the path "/usr/share/zoneinfo/Pacific Standard Time".
    • 03-27 18:44:58.367: I/Unity(25251): TimeZoneInfo.FindSystemTimeZoneById Central Pacific Standard Time failed: System.TimeZoneNotFoundException: Couldn't read time zone file /usr/share/zoneinfo/Central Pacific Standard Time ---> System.IO.DirectoryNotFoundException: Could not find a part of the path "/usr/share/zoneinfo/Central Pacific Standard Time".
    • 03-27 18:44:58.370: I/Unity(25251): TimeZoneInfo.FindSystemTimeZoneById PST failed: System.TimeZoneNotFoundException: Couldn't read time zone file /usr/share/zoneinfo/PST ---> System.IO.DirectoryNotFoundException: Could not find a part of the path "/usr/share/zoneinfo/PST".
    • 03-27 18:44:58.371: I/Unity(25251): TZConvert.GetTimeZoneInfo PST failed: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
    • 03-27 18:44:58.372: I/Unity(25251): TZConvert.GetTimeZoneInfo Pacific Standard Time failed: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
    • 03-27 18:44:58.373: I/Unity(25251): TZConvert.GetTimeZoneInfo Central Pacific Standard Time failed: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.

Upvotes: 1

Views: 2786

Answers (1)

amitklein
amitklein

Reputation: 1395

First solution:

You can use the TimeZoneInfo.DisplayName

In this post you can see the outputs of the other TimeZoneInfo variables.

Second solution:

On the first time opening the app you can get the phone local time with System.DateTime.Now, and you know the Google Play Leaderboard Service time zone so you can just save the difference in PlayerPrefs.SetFloat("timeDifarence", Value);

Then whenever you need it just use PlayerPrefs.GetFloat("timeDifarence"); and calculate the time on the machine.

Upvotes: 0

Related Questions