一般的 时间戳 格式分为两种 即 10位(秒)时间戳 与 13位(毫秒)时间戳

            时间戳 类型也分为两种 即 本地时间戳 与 世界统一(UTC)时间戳

废话不多说,直接上代码:

一、时间戳获取方法

/// <summary>
/// 获取时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc)</param>
/// <param name="format">时间戳格式(只能为 10、13)</param>
/// <returns></returns>
private double GetTimestamp(int format, DateTimeKind timeKind)
{
    TimeSpan timeSpan = new TimeSpan();

    switch (timeKind)
    {
        case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        default: throw new Exception("时间类型 只能为 Local、Utc");
    }

    switch (format)
    {
        case 10: return timeSpan.TotalSeconds;
        case 13: return timeSpan.TotalMilliseconds;
        default: throw new Exception("时间戳格式 只能为 10、13");
    }
}

/// <summary>
/// 获取10位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public int Get10Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
    return Convert.ToInt32(GetTimestamp(10, timeKind));
}

/// <summary>
/// 获取13位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public long Get13Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
    return Convert.ToInt64(GetTimestamp(13, timeKind));
}

二、时间戳验证方法

/// <summary>
/// 验证时间戳(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeDiff">允许时差(10位时单位为 秒,13位时单位为 毫秒)</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public bool ValidateTimestamp(double timestamp, int timeDiff, DateTimeKind timeKind = DateTimeKind.Local)
{
    TimeSpan timeSpan = new TimeSpan();

    switch (timeKind)
    {
        case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        default: throw new Exception("时间类型 只能为 Local、Utc");
    }

    double nowTimestamp = 0;  //现在的时间戳
    int format = timestamp.ToString("f0").Length;

    switch (format)
    {
        case 10: nowTimestamp = timeSpan.TotalSeconds; break;
        case 13: nowTimestamp = timeSpan.TotalMilliseconds; break;
        default: throw new Exception("时间戳格式 错误");
    }

    double nowTimeDiff = nowTimestamp - timestamp;  //现在的时差

    if (-timeDiff <= nowTimeDiff && nowTimeDiff <= timeDiff)
        return true;
    else
        return false;
}

三、由 时间戳 转换为 DateTime 方法

/// <summary>
/// 将时间戳装换为DateTime(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <param name="toTimeKind">返回的时间类型(只能为 Local、Utc,默认与 timeKind 一致)</param>
/// <returns></returns>
public DateTime TimestampToDateTime(double timestamp, DateTimeKind timeKind = DateTimeKind.Local, DateTimeKind toTimeKind = DateTimeKind.Unspecified)
{
    DateTime startTime;
    toTimeKind = timeKind;

    switch (timeKind)
    {
        case DateTimeKind.Utc: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        case DateTimeKind.Local: startTime = new DateTime(1970, 1, 1, 0, 0, 0, timeKind); break;
        default: throw new Exception("时间类型 只能为 Local、Utc");
    }

    DateTime newTime;
    int format = timestamp.ToString("f0").Length;

    switch (format)
    {
        case 10: newTime = startTime.AddSeconds(timestamp); break;
        case 13: newTime = startTime.AddMilliseconds(timestamp); break;
        default: throw new Exception("时间戳格式 错误");
    }

    if (newTime.Kind != toTimeKind)
        newTime = toTimeKind == DateTimeKind.Local ? newTime.ToLocalTime() : newTime.ToUniversalTime();

    return newTime;
}

版权声明:本文为原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: