namespace Ds.Module.WeChat.Utilities { /// 微信日期处理帮助类 public class DateTimeHelper { /// Unix起始时间 public static readonly DateTimeOffset BaseTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); /// 转换微信DateTime时间到C#时间 /// 微信DateTime /// public static DateTime GetDateTimeFromXml(long dateTimeFromXml) => DateTimeHelper.GetDateTimeOffsetFromXml(dateTimeFromXml).LocalDateTime; /// 转换微信DateTime时间到C#时间 /// 微信DateTime /// public static DateTime GetDateTimeFromXml(string dateTimeFromXml) => DateTimeHelper.GetDateTimeFromXml(long.Parse(dateTimeFromXml)); /// 转换微信DateTimeOffset时间到C#时间 /// 微信DateTime /// public static DateTimeOffset GetDateTimeOffsetFromXml(long dateTimeFromXml) => DateTimeHelper.BaseTime.AddSeconds((double)dateTimeFromXml).ToLocalTime(); /// 转换微信DateTimeOffset时间到C#时间 /// 微信DateTime /// public static DateTimeOffset GetDateTimeOffsetFromXml(string dateTimeFromXml) => (DateTimeOffset)DateTimeHelper.GetDateTimeFromXml(long.Parse(dateTimeFromXml)); /// 获取微信DateTime(UNIX时间戳) /// 时间 /// [Obsolete("请使用 GetUnixDateTime(dateTime) 方法")] public static long GetWeixinDateTime(DateTime dateTime) => DateTimeHelper.GetUnixDateTime(dateTime); /// 获取Unix时间戳 /// /// public static long GetUnixDateTime(DateTimeOffset dateTime) => (long)(dateTime - DateTimeHelper.BaseTime).TotalSeconds; /// 获取Unix时间戳 /// /// public static long GetUnixDateTime(DateTime dateTime) => (long)((DateTimeOffset)dateTime.ToUniversalTime() - DateTimeHelper.BaseTime).TotalSeconds; } }