• 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;
    }
  • 相关阅读:
    20个命令行工具监控 Linux 系统性能【转载】
    机器学习及部分NLP相关网上的代码资源整理
    职场中脱颖而出的成长秘诀【转载】
    美好的诗词
    其实这是一种生活方式【转载】
    10个非常有趣的Linux命令【转载】
    如何摆脱工具类【转载】
    在线公开课 | 如何基于模型训练平台快速打造AI能力?
    企业应用架构演化探讨:从微服务到Service Mesh
    生命的价值——大型科技公司的时间“减”史
  • 原文地址:https://www.cnblogs.com/sgwy/p/11609098.html
Copyright © 2020-2023  润新知