Forest_Client/Forest/Assets/PhxhSDK/Phxh/Utils/Utils.Time.cs

71 lines
2.1 KiB
C#

using System;
namespace PhxhSDK
{
public static partial class Utils
{
public static ulong GetTimeStampMilliseconds()
{
DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);// 当地时区
ulong timeStamp = (ulong)(DateTime.Now - startTime).TotalMilliseconds;
return timeStamp;
}
public static ulong GetTimeStamp()
{
var ms = GetTimeStampMilliseconds();
return ms / 1000;
}
public static DateTime GetDateTime(long timeStamp)
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
long unixTimeStampInTicks = timeStamp * TimeSpan.TicksPerMillisecond;
return new DateTime(dateTime.Ticks + unixTimeStampInTicks, DateTimeKind.Utc) ;
}
public static string GetDateTimeString(DateTime dateTime)
{
return dateTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
}
public static string GetDateByTimeStamp(long timeStamp)
{
return GetDateTimeString(GetDateTime(timeStamp));
}
public static string GetTimeString(int second, bool shortStr) {
int hour = (int) second / 3600;
int leftMinutes = (second % 3600) / 60;
int leftSeconds = second % 60;
if (hour == 0) {
if (shortStr)
{
return $"{leftMinutes}m";
}
else
{
return $"{leftMinutes}m{leftSeconds:d2}s";
}
}
if (hour >= 24) {
if (shortStr)
{
return $"{hour / 24}d";
}
else
{
return $"{hour / 24}d{hour % 24:d2}h";
}
}
if (shortStr)
{
return $"{hour}h";
}
else
{
return $"{hour}h{leftMinutes:d2}m";
}
}
}
}