#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("getDefault")) { return timeZone.Call("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