/// <summary>
/// 时间戳辅助类,时间戳是自 1970年1月1日(00:00:00 GMT)至当前时间的总秒数。 10位则精确到秒。13位则精确到毫秒
/// </summary>
public static class TimestampHelper
{
#region 时间戳静态方法
/// <summary>
/// 获取当前时间戳
/// </summary>
/// <param name="millisecond">默认值ture,生成13位时间戳,false生成10位时间戳</param>
/// <returns>返回对应的时间戳字符串</returns>
public static long GetCurrentTimestamp(bool millisecond = true) => ConvertToTimestampLong(DateTime.Now, millisecond);
/// <summary>
/// 转换DateTime类型时间为时间戳
/// </summary>
/// <param name="dateTime">DateTime类型时间</param>
/// <param name="millisecond">是否精确到毫秒,默认值为true</param>
/// <returns>long型时间戳</returns>
public static long ConvertToTimestampLong(DateTime dateTime, bool millisecond = true)
{
TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return millisecond ? Convert.ToInt64(ts.TotalMilliseconds) : Convert.ToInt64(ts.TotalSeconds);
}
/// <summary>
/// 转换时间戳字符串为DateTime类型
/// </summary>
/// <param name="timestamp">10位或者13位时间戳字符串</param>
/// <returns>返回对应的时间</returns>
public static DateTime ConvertToDateTime(string timestamp)
{
DateTime tz = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
return timestamp.Length == 13 ? tz.AddMilliseconds(Convert.ToInt64(timestamp)) : tz.AddSeconds(Convert.ToInt64(timestamp));
}
#endregion
#region 对DateTime和string类型的扩展方法
/// <summary>
/// DateTime类扩展方法:将DateTime格式时间得到对应的字符串类型的时间戳
/// </summary>
/// <param name="dateTime">DateTime格式时间</param>
/// <param name="millisecond">默认值ture,生成13位时间戳,false生成10位时间戳</param>
/// <returns>返回字符串类型时间戳</returns>
public static string ToTimestamp(this DateTime dateTime, bool millisecond = true) => ConvertToTimestampLong(dateTime, millisecond).ToString();
/// <summary>
/// DateTime类扩展方法:转换指定时间得到对应的数值类型的时间戳
/// </summary>
/// <param name="dateTime">DateTime格式时间</param>
/// <param name="millisecond">默认值ture,生成13位时间戳,false生成10位时间戳</param>
/// <returns>返回long型时间戳</returns>
public static long ToTimestampLong(this DateTime dateTime, bool millisecond = true) => ConvertToTimestampLong(dateTime, millisecond);
/// <summary>
/// string类型扩展方法:转换时间戳字符串为DateTime类型
/// </summary>
/// <param name="timestamp">10位或者13位时间戳字符串</param>
/// <returns>返回对应的时间</returns>
public static DateTime ToDateTime(this string timestamp) => ConvertToDateTime(timestamp);
#endregion
#region 时间戳验证
/// <summary>
/// 验证时间戳
/// </summary>
/// <param name="timestamp">(10位或者13位)时间戳字符串</param>
/// <param name="intervalMinutes">差值(分钟)</param>
/// <returns></returns>
public static bool IsTimestamp(string timestamp, double intervalMinutes)
{
DateTime dt = ConvertToDateTime(timestamp);
//取现在时间
DateTime dt1 = DateTime.Now.AddMinutes(intervalMinutes);
DateTime dt2 = DateTime.Now.AddMinutes(intervalMinutes * -1);
return (dt > dt2 && dt < dt1);
}
/// <summary>
/// 简单的判断时间戳是否正确(验证前8位)
/// 大约2分钟之内
/// </summary>
/// <param name="time">(10位或13位)时间戳字符串</param>
/// <returns></returns>
public static bool IsTimestamp(string time) => GetCurrentTimestamp().ToString().Substring(0, 8).Equals(time.Substring(0, 8));
#endregion
}