| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #if UNITY_EDITOR
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using UnityEngine;
- public static class DeviceTimeZone
- {
- #if UNITY_IOS && !UNITY_EDITOR
- private static IntPtr IOS_GetLocalTimeZoneId() => NativeApi.TimeZone.IOS_GetLocalTimeZoneId();
- private static void IOS_FreeCString(IntPtr pointer) => NativeApi.TimeZone.IOS_FreeCString(pointer);
- #endif
- public static string GetIanaId()
- {
- string id;
- #if UNITY_ANDROID && !UNITY_EDITOR
- id = GetAndroidTimeZoneId();
- #elif UNITY_IOS && !UNITY_EDITOR
- id = GetIOSTimeZoneId();
- #else
- id = TimeZoneInfo.Local.Id;
- #endif
- if (string.IsNullOrEmpty(id) ||
- string.Equals(id, "Local", StringComparison.OrdinalIgnoreCase))
- {
- return GetUtcOffsetText();
- }
- return id;
- }
- public static int GetUtcOffsetMinutes()
- {
- return (int)DateTimeOffset.Now.Offset.TotalMinutes;
- }
- #if UNITY_ANDROID && !UNITY_EDITOR
- private static string GetAndroidTimeZoneId()
- {
- try
- {
- using (var timeZoneClass =
- new AndroidJavaClass("java.util.TimeZone"))
- using (var timeZone =
- timeZoneClass.CallStatic<AndroidJavaObject>("getDefault"))
- {
- return timeZone.Call<string>("getID");
- }
- }
- catch (Exception exception)
- {
- Debug.LogException(exception);
- return null;
- }
- }
- #endif
- #if UNITY_IOS && !UNITY_EDITOR
- private static string GetIOSTimeZoneId()
- {
- IntPtr pointer = IOS_GetLocalTimeZoneId();
- if (pointer == IntPtr.Zero)
- return null;
- try
- {
- return Marshal.PtrToStringAnsi(pointer);
- }
- finally
- {
- IOS_FreeCString(pointer);
- }
- }
- #endif
- private static string GetUtcOffsetText()
- {
- TimeSpan offset = DateTimeOffset.Now.Offset;
- string sign = offset < TimeSpan.Zero ? "-" : "+";
- offset = offset.Duration();
- return string.Format(
- "UTC{0}{1:00}:{2:00}",
- sign,
- offset.Hours,
- offset.Minutes
- );
- }
- }
- #endif
|