欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 时间戳的相关操作

程序员文章站 2023-04-05 09:28:03
一般的 时间戳 格式分为两种 即 10位(秒)时间戳 与 13位(毫秒)时间戳 时间戳 类型也分为两种 即 本地时间戳 与 世界统一(UTC)时间戳 废话不多说,直接上代码: 一、时间戳获取方法 二、时间戳验证方法 三、由 时间戳 转换为 DateTime 方法 ......

一般的 时间戳 格式分为两种 即 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;
}