Reputation: 379
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"));
}
Upvotes: 1
Views: 2786
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