NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Utils/CommonUtils.Time.cs

99 lines
2.9 KiB
C#

using cfg.ActorCfg;
using cfg.CharacterCfg;
using Framework;
using Gameplay.Net;
using System;
namespace Gameplay
{
public partial class CommonUtils
{
public enum E_TimeType
{
/// <summary>
/// 本地
/// </summary>
Local,
/// <summary>
/// 服务器
/// </summary>
Server,
}
private static readonly DateTime Epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// 根据时间戳获取时间(秒时间戳)
/// </summary>
/// <param name="timestamp">秒</param>
/// <returns></returns>
public static DateTime GetTimeByTimestamp(uint timestamp)
{
return Epoch.AddSeconds(timestamp).ToLocalTime();
}
/// <summary>
/// 根据时间戳获取时间(毫秒时间戳)
/// </summary>
/// <param name="timestamp">毫秒</param>
/// <returns></returns>
public static DateTime GetTimeByTimestamp(ulong timestamp)
{
return Epoch.AddMilliseconds(timestamp).ToLocalTime();
}
/// <summary>
/// 获取当前时间
/// </summary>
/// <param name="timeType"></param>
/// <returns></returns>
public static DateTime GetCurTime(E_TimeType timeType)
{
return timeType switch
{
E_TimeType.Local => DateTime.Now,
E_TimeType.Server => GetTimeByTimestamp(NetManager.Instance.ServerTimeMS),
_ => DateTime.MinValue,
};
}
/// <summary>
/// 判断是否是生日
/// </summary>
/// <param name="birthTime"></param>
/// <returns></returns>
public static bool IsBirth(DateTime birthTime)
{
DateTime curTime = GetCurTime(E_TimeType.Server);
return curTime.Month == birthTime.Month && curTime.Day == birthTime.Day;
}
/// <summary>
/// 判断是否在未成年人游玩时间段内
/// </summary>
/// <returns></returns>
public static bool IsUnderagePlayTimeAllowed()
{
var time = GetCurTime(E_TimeType.Server);
if (time.Hour != TableManager.Instance.Tables.GlobalConfig.UnderEighteenGameHour)//不在时间段内
{
return false;
}
bool isDayOK = false;
if (TableManager.Instance.Tables.GlobalConfig.UnderEighteenGameWeekDay.Contains((int)time.DayOfWeek))//在游玩日内
{
isDayOK = true;
}
foreach (var date in TableManager.Instance.Tables.Holidays.DataList)//节假日
{
if (date.Month == time.Month && date.Day == time.Day)
{
isDayOK = true;
break;
}
}
return isDayOK;
}
}
}