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"));
}
- 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.