using System;
using UnityEngine;
public class ServerTimeManager : SingletonMonoBehaviour<ServerTimeManager>
{
private long lCurServerTime = 0; //当前服务器时间 毫秒为单位
private float fCheckTime = 0.0f;
private DateTime daStartTime = new DateTime(1970, 1, 1); //服务器相对于C#的起始时间
private float deltaTime = 0.0f;
void Awake()
{
SetServerTime((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
}
public void SetServerTime(long _time)
{
fCheckTime = Time.realtimeSinceStartup;
lCurServerTime = _time;
Update();
}
public long GetfCurSerTime()
{
return lCurServerTime + (long)(deltaTime * 1000);
}
public DateTime GetServerTime()
{
return GetDateTime(GetfCurSerTime());
}
private void Update()
{
deltaTime = Time.realtimeSinceStartup - fCheckTime;
}
public bool IsSameDay(DateTime day)
{
DateTime now = GetServerTime();
return day.Day == now.Day && day.Month == now.Month && day.Year == now.Year;
}
public bool IsSameDay(long time)
{
DateTime day = GetDateTime(time);
return IsSameDay(day);
}
public float DeltaTime()
{
return deltaTime;
}
public DateTime GetDateTime(long millisceonds)
{
return daStartTime.AddMilliseconds(millisceonds).ToLocalTime();
}
public string GetDaysToNowString(long time)
{
if(time == 0)
{
return "";
}
if (IsSameDay(time))
{
return "今天";
}
TimeSpan span = GetServerTime().Date - GetDateTime(time);
if (span.Days == 0)
{
return "昨天";
}
else if (span.Days >= 999)
{
return "999天前";
}
else
{
return span.Days + "天前";
}
}
}
Unity 服务器时间
最新推荐文章于 2023-09-11 07:30:00 发布