Files
hjha-server/ServerCore/Helper/DateTimeHelper.cs
xiaoou e9616125ce feat: initial commit - HJHA game server full source
6 major server modules (PdkFriendServer/GlobalSever/ServerCore/GameModule/GameNetModule) +
game logic (GameFix/GameDAL/ServerData) +
network layer (NetWorkMessage) +
data layer (ObjectModel) +
utilities (MrWu/Core/Config/CloudAPI/dll) +
adapters (zyxAdapter/base)

.NET 8.0 C# solution, 16 projects, 958 source files
2026-07-07 12:02:15 +08:00

57 lines
2.0 KiB
C#

using System;
namespace Server
{
public class DateTimeHelper
{
private static readonly System.DateTime unixTime =
TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
/// <summary>
/// 时间转时间戳
/// </summary>
/// <param name="time">时间</param>
/// <param name="bflag">true 表示10位时间戳 false 表示13位时间戳</param>
/// <returns>时间戳</returns>
public static long DateTime2TimeStamp(System.DateTime time, bool bflag = true)
{
TimeSpan ts = time - unixTime;
if (bflag)
return Convert.ToInt64(ts.TotalSeconds);
else
return Convert.ToInt64(ts.TotalMilliseconds);
}
/// <summary>
/// 时间戳转时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static System.DateTime TimeStamp2DateTime(long timeStamp)
{
string timeStampStr = timeStamp.ToString();
if (timeStampStr.Length == 10)
{
DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeSeconds(timeStamp);
dateTime = dateTime.ToOffset(TimeSpan.FromHours(8)); //UTC 转北京时间
return dateTime.DateTime;
}
if (timeStampStr.Length == 13)
{
DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp);
dateTime = dateTime.ToOffset(TimeSpan.FromHours(8));
return dateTime.DateTime;
}
return System.DateTime.MinValue;
}
public static float TimeBeTween(System.DateTime now,System.DateTime last)
{
TimeSpan ts = now.Subtract(last).Duration();
return (float)ts.TotalMilliseconds;
}
}
}