1
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
using System;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.Networking;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// 北京时间管理器 - 获取一次后持续自增
|
||||
/// 北京时间管理器 - 同步时记录服务器时间与本地系统时间的差值,获取时返回本地时间+差值
|
||||
/// </summary>
|
||||
public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
{
|
||||
[Header("时间状态")]
|
||||
[SerializeField] private bool _isTimeSynced = false;
|
||||
[SerializeField] private DateTime _currentBeijingTime;
|
||||
[SerializeField] private float _timeSinceLastSync;
|
||||
[SerializeField] private TimeSpan _serverTimeOffset; // 服务器时间 - 本地系统时间
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前北京时间
|
||||
@@ -23,7 +22,7 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
{
|
||||
if (_isTimeSynced)
|
||||
{
|
||||
return _currentBeijingTime.AddSeconds(_timeSinceLastSync);
|
||||
return DateTime.Now + _serverTimeOffset;
|
||||
}
|
||||
return DateTime.Now; // 未同步时返回本地时间
|
||||
}
|
||||
@@ -63,14 +62,6 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
SyncBeijingTime();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isTimeSynced)
|
||||
{
|
||||
_timeSinceLastSync += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步北京时间
|
||||
/// </summary>
|
||||
@@ -112,16 +103,16 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
|
||||
if (url.Contains("timeapi.io"))
|
||||
{
|
||||
|
||||
|
||||
string timeStr = json["dateTime"]?.ToString();
|
||||
Debug.Log(timeStr);
|
||||
if (!string.IsNullOrEmpty(timeStr))
|
||||
{
|
||||
_currentBeijingTime = DateTime.Parse(timeStr);
|
||||
_timeSinceLastSync = 0;
|
||||
DateTime serverTime = DateTime.Parse(timeStr);
|
||||
_serverTimeOffset = serverTime - DateTime.Now;
|
||||
_isTimeSynced = true;
|
||||
Debug.Log($"北京时间同步成功:{_currentBeijingTime:yyyy-MM-dd HH:mm:ss}");
|
||||
OnTimeSynced?.Invoke(_currentBeijingTime);
|
||||
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
|
||||
OnTimeSynced?.Invoke(CurrentBeijingTime);
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
@@ -133,13 +124,13 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
|
||||
if (!string.IsNullOrEmpty(timeStr))
|
||||
{
|
||||
_currentBeijingTime = DateTimeOffset.Parse(timeStr)
|
||||
DateTime serverTime = DateTimeOffset.Parse(timeStr)
|
||||
.ToOffset(TimeSpan.FromHours(8))
|
||||
.DateTime;
|
||||
_timeSinceLastSync = 0;
|
||||
_serverTimeOffset = serverTime - DateTime.Now;
|
||||
_isTimeSynced = true;
|
||||
Debug.Log($"北京时间同步成功:{_currentBeijingTime:yyyy-MM-dd HH:mm:ss}");
|
||||
OnTimeSynced?.Invoke(_currentBeijingTime);
|
||||
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
|
||||
OnTimeSynced?.Invoke(CurrentBeijingTime);
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
@@ -150,11 +141,11 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
|
||||
if (!string.IsNullOrEmpty(timeStr))
|
||||
{
|
||||
_currentBeijingTime = DateTime.ParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null);
|
||||
_timeSinceLastSync = 0;
|
||||
DateTime serverTime = DateTime.ParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null);
|
||||
_serverTimeOffset = serverTime - DateTime.Now;
|
||||
_isTimeSynced = true;
|
||||
Debug.Log($"北京时间同步成功:{_currentBeijingTime:yyyy-MM-dd HH:mm:ss}");
|
||||
OnTimeSynced?.Invoke(_currentBeijingTime);
|
||||
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
|
||||
OnTimeSynced?.Invoke(CurrentBeijingTime);
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
@@ -169,7 +160,7 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
|
||||
}
|
||||
|
||||
Debug.LogError("所有时间服务器都失败了,使用本地时间");
|
||||
_currentBeijingTime = DateTime.Now;
|
||||
_serverTimeOffset = TimeSpan.Zero;
|
||||
_isTimeSynced = false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user