1

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.
Juri Knauth
  • 359
  • 1
  • 4
  • 11
  • I'll have to investigate a bit. But of what you shown here, only `TZConvert.GetTimeZoneInfo("Pacific Standard Time")` should work (assuming that is using my TimeZoneConverter` library). You could also try `TZConvert.GetTimeZoneInfo("America/Los_Angeles")` or just `TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angles")` (without TimeZoneConverter). Can you give any details about the specific Android version and device? It may simply be missing time zone data. – Matt Johnson-Pint Mar 29 '21 at 16:57
  • Also, which specific Google API are you needing to call that requires Pacific time? Link to the reference doc please. – Matt Johnson-Pint Mar 29 '21 at 16:58
  • Keep in mind that "Central Pacific Standard Time" is for a few islands in the Pacific ocean such as New Caledonia (UTC+11) which is probably not what you are interested in. Also, "PST" isn't a valid time zone ID. – Matt Johnson-Pint Mar 29 '21 at 17:07
  • 1
    You might want to see [this bug](https://issuetracker.unity3d.com/issues/win-system-dot-timezoneinfo-does-not-return-any-time-zones) and investigate using [Noda Time](https://nodatime.org/) with its built-in TZDB provider. However, I still think there's likely some other way to deal with this. – Matt Johnson-Pint Mar 29 '21 at 17:13
  • 1
    Yes thanks! Node time did the trick for me. I used NodaTime.2.4.13. Jon Skeet made 3.x also work, I didn't just now... https://github.com/nodatime/nodatime/issues/1525 I will do some more testing this weekend. I will also try to get your TZConvert to work again in Unity. As you pointed out there is a bug in unity atm, in my android build TimeZoneInfo.GetSystemTimeZones() returns an empty Collection. – Juri Knauth Mar 30 '21 at 23:08
  • Thanks again and keep up the good work! – Juri Knauth Mar 30 '21 at 23:16

1 Answers1

0

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.

amitklein
  • 1,302
  • 6
  • 23