C# 時間戳的相關操作


一般的 時間戳 格式分為兩種 即 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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM