You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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