diff --git a/Assets/Utils.meta b/Assets/Utils.meta new file mode 100644 index 0000000..558eeb1 --- /dev/null +++ b/Assets/Utils.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cdc8c485ff3349409e3bc22fa04a148e +timeCreated: 1676013511 \ No newline at end of file diff --git a/Assets/Utils/DateTimeUtil.cs b/Assets/Utils/DateTimeUtil.cs new file mode 100644 index 0000000..acdf1b4 --- /dev/null +++ b/Assets/Utils/DateTimeUtil.cs @@ -0,0 +1,34 @@ +using System; + +namespace Utils +{ + public class DateTimeUtil + { + private static readonly DateTime UtcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); + + /// + /// 转换为Unix时间戳 + /// + /// 需要转换的时间 + /// Unix时间戳 + public static long ToUnixTimeStamp(DateTime dateTime) + { + var utcTime = dateTime.ToUniversalTime(); + var timeSpan = utcTime - UtcStartTime; + + return Convert.ToInt64(timeSpan.TotalSeconds); + } + + /// + /// 从Unix时间戳转换为日期时间对象 + /// + /// Unix时间戳 + /// Unix时间戳代表的日期时间对象 + public static DateTime FromUnixTimeStamp(long timeStamp) + { + var utcTime = UtcStartTime.AddSeconds(timeStamp); + + return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local); + } + } +} \ No newline at end of file diff --git a/Assets/Utils/DateTimeUtil.cs.meta b/Assets/Utils/DateTimeUtil.cs.meta new file mode 100644 index 0000000..0135626 --- /dev/null +++ b/Assets/Utils/DateTimeUtil.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: aed416aa0d4d4f04a91c72c39d052972 +timeCreated: 1676013530 \ No newline at end of file