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

99 lines
2.9 KiB
C#
Raw Normal View History

2024-03-25 15:53:35 +08:00
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);
2024-03-26 16:27:09 +08:00
/// <summary>
2025-02-12 18:48:49 +08:00
/// 根据时间戳获取时间(秒时间戳)
2024-03-26 16:27:09 +08:00
/// </summary>
2024-04-03 16:07:49 +08:00
/// <param name="timestamp">秒</param>
2024-03-26 16:27:09 +08:00
/// <returns></returns>
public static DateTime GetTimeByTimestamp(uint timestamp)
{
return Epoch.AddSeconds(timestamp).ToLocalTime();
}
/// <summary>
2025-02-12 18:48:49 +08:00
/// 根据时间戳获取时间(毫秒时间戳)
2024-03-26 16:27:09 +08:00
/// </summary>
2024-04-03 16:07:49 +08:00
/// <param name="timestamp">毫秒</param>
2024-03-26 16:27:09 +08:00
/// <returns></returns>
public static DateTime GetTimeByTimestamp(ulong timestamp)
{
return Epoch.AddMilliseconds(timestamp).ToLocalTime();
}
2024-03-25 15:53:35 +08:00
/// <summary>
/// 获取当前时间
/// </summary>
/// <param name="timeType"></param>
/// <returns></returns>
public static DateTime GetCurTime(E_TimeType timeType)
{
return timeType switch
{
E_TimeType.Local => DateTime.Now,
2024-03-26 16:27:09 +08:00
E_TimeType.Server => GetTimeByTimestamp(NetManager.Instance.ServerTimeMS),
2024-03-25 15:53:35 +08:00
_ => DateTime.MinValue,
};
}
2024-04-29 17:20:40 +08:00
/// <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;
}
2024-11-04 15:22:27 +08:00
/// <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;
}
2024-03-25 15:53:35 +08:00
}
}