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

C# 时间与时间戳互转的方法(13位)

程序员文章站 2023-12-09 19:02:33
目前经常出现的时间有三个: 本地时间(locale time) 格林威治时间(greenwich mean time gmt) 时间协调时间 (uni...

目前经常出现的时间有三个:

  • 本地时间(locale time)
  • 格林威治时间(greenwich mean time gmt)
  • 时间协调时间 (universal time coordinated utc)

时间标准:

(1)世界时
世界时是最早的时间标准。在1884年,国际上将1s确定为全年内每日平均长度的1/8.64×104。以此标准形成的时间系统,称为世界时(ut1)。 1972年国际上开始使用国际原子时标,从那以后,经过格林威治老天文台本初子午线的时间便被称为世界时(ut2),或称格林威治时间(gmt),ut2是对地球转速周期性差异进行校正后的世界时。

(2)原子时
1967年,人们利用铯原子振荡周期极为规律的特性,研制出了高精度的原子时钟,将铯原子能级跃迁辐射9192631770周所经历的时间定为1s。现在用的时间就是1971年10月定义的国际原子时,是通过世界上大约200多台原子 钟进行对比后,再由国际度量衡局时间所进行数据处理,得出的统一的原子时,简称tai。

(3)世界协调时
世界协调时是以地球自转为基础的时间标准。由于地球自转速度并不均匀,并非每天都是精确的86400原子s,因而导致了自转时间与世界时之间存在18个月有1s的误差。为纠正这种误差,国际地球自转研究所根据地球自转的实际情况对格林威治时间进行增减闰s的调整,与国际度量衡局时间所联合向全世界发布标准时间,这就是所谓的世界协调时(utc:coordinatdeuniversaltime)。utc的表示方式为:年(y)、月(m)、日(d)、时(h)、分(min)、秒(s),均用数字表示。

gps 系统中有两种时间区分,一为utc,另一为lt(地方时)两者的区别为时区不同,utc就是0时区的时间,地方时为本地时间,如北京为早上八点(东八区),utc时间就为零点,时间比北京时晚八小时,以此计算即可

通过上面的了解,我们可以认为格林威治时间就是时间协调时间(gmt=utc),格林威治时间和utc时间均用秒数来计算的。

/// <summary>
    /// 获取时间戳
    /// </summary>
    /// <returns></returns>
    public static string gettimestamp(system.datetime time)
    {
      long ts = convertdatetimetoint(time);
      return ts.tostring();
    }
    /// <summary> 
    /// 将c# datetime时间格式转换为unix时间戳格式 
    /// </summary> 
    /// <param name="time">时间</param> 
    /// <returns>long</returns> 
    public static long convertdatetimetoint(system.datetime time)
    {
      system.datetime starttime = timezone.currenttimezone.tolocaltime(new system.datetime(1970, 1, 1, 0, 0, 0, 0));
      long t = (time.ticks - starttime.ticks) / 10000;  //除10000调整为13位   
      return t;
    }
    /// <summary>    
    /// 时间戳转为c#格式时间    
    /// </summary>    
    /// <param name=”timestamp”></param>    
    /// <returns></returns>    
    private datetime convertstringtodatetime(string timestamp)
    {
      datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970, 1, 1));
      long ltime = long.parse(timestamp + "0000");
      timespan tonow = new timespan(ltime);
      return dtstart.add(tonow);
    } 
 /// <summary>
  /// 时间戳
  /// </summary>
  public class timehelp
  {
    /// <summary>
    /// 获取时间戳
    /// </summary>
    /// <returns></returns>
    public static string gettimestamp(system.datetime time,int length=13)
    {
      long ts = convertdatetimetoint(time);
      return ts.tostring().substring(0,length);
    }
    /// <summary> 
    /// 将c# datetime时间格式转换为unix时间戳格式 
    /// </summary> 
    /// <param name="time">时间</param> 
    /// <returns>long</returns> 
    public static long convertdatetimetoint(system.datetime time)
    {
      system.datetime starttime = timezone.currenttimezone.tolocaltime(new system.datetime(1970, 1, 1, 0, 0, 0, 0));
      long t = (time.ticks - starttime.ticks) / 10000;  //除10000调整为13位   
      return t;
    }
    /// <summary>    
    /// 时间戳转为c#格式时间    
    /// </summary>    
    /// <param name=”timestamp”></param>    
    /// <returns></returns>    
    public static datetime convertstringtodatetime(string timestamp)
    {
      datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970, 1, 1));
      long ltime = long.parse(timestamp + "0000");
      timespan tonow = new timespan(ltime);
      return dtstart.add(tonow);
    }

    /// <summary>
    /// 时间戳转为c#格式时间10位
    /// </summary>
    /// <param name="timestamp">unix时间戳格式</param>
    /// <returns>c#格式时间</returns>
    public static datetime getdatetimefrom1970ticks(long curseconds)
    {
      datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970, 1, 1));
      return dtstart.addseconds(curseconds);
    }

    /// <summary>
    /// 验证时间戳
    /// </summary>
    /// <param name="time"></param>
    /// <param name="interval">差值(分钟)</param>
    /// <returns></returns>
    public static bool istime(long time, double interval)
    {
      datetime dt = getdatetimefrom1970ticks(time);
      //取现在时间
      datetime dt1 = datetime.now.addminutes(interval);
      datetime dt2 = datetime.now.addminutes(interval * -1);
      if (dt > dt2 && dt < dt1)
      {
        return true;
      }
      else {
        return false;
      }
    }

    /// <summary>
    /// 判断时间戳是否正确(验证前8位)
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    public static bool istime(string time)
    {
      string str = gettimestamp(datetime.now,8);
      if (str.equals(time))
      {
        return true;
      }
      else {
        return false;
      }
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。