c++中的时间处理

3880阅读 0评论2021-02-26 轨迹16
分类:C/C++

参考资料

cpp官方文档:


时间处理往往有以下几种

1、获取时间戳
2、获取当天的时间,年月日时分秒
3、时间的运算,譬如:向前推移1小时2分钟3秒
4、时间差,譬如:用户计算程序的运行时间


c++11之前/纯c系统调用的使用方式
  1. 获取时间戳

    #include 
    #include 
    
    int main()
    {
    	time_t t = time(NULL);
    	printf("time stamp : %lld\n", t);
    } 
  2. 获取时间

    1. 从上面的时间戳中转为tm结构
    #include 
    #include 
    
    int main()
    {
        time_t t = time(NULL);
        printf("time stamp : %lld\n", t); 
        tm* tm_ = localtime(&t);
    
        int year = tm_->tm_year + 1900;  // 年,tm结构体存储的是从1900年开始的时间,所以tm_year加上1900
        int month = tm_->tm_mon + 1;     // 月,tm结构体的月份存储范围为0-11,所以为tm_mon加上1
        int day = tm_->tm_mday;          // 日
        int hour = tm_->tm_hour;         // 时
        int minute = tm_->tm_min;        // 分
        int second = tm_->tm_sec;  
    
        printf("year: %d, month: %d, day: %d, hour: %d, minute: %d, second: %d\n",
            year, month, day, hour, minute, second);
    } 
  3. time和tz之间的转换

    time_t t_ = mktime(&tm_); 
  4. 获取毫/微秒级的时间
    系统函数time只能获取秒级的精度。如果想获取毫/微秒级的精度,那么需要其他的技巧。linux下面可以直接使用gettimeofday。

    #include 
    #include 
    
    int main()
    {
        struct timeval curTime;
        gettimeofday(&curTime, NULL);
        printf("sec: %d, usec: %d\n", curTime.tv_sec, curTime.tv_usec);
    } 

    在windows下面并么有gettimeofday的系统函数,可以通过封装一个实现了跨平台。

    #include 
    #if (defined(WIN32) || defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) 
    #include 
    #else
    #include 
    #endif
    #if (defined(WIN32) || defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) 
    int gettimeofday(struct timeval *tp, void *tzp)
    {
      time_t clock;
      struct tm tm;
      SYSTEMTIME wtm;
      GetLocalTime(&wtm);
      tm.tm_year   = wtm.wYear - 1900;
      tm.tm_mon   = wtm.wMonth - 1;
      tm.tm_mday   = wtm.wDay;
      tm.tm_hour   = wtm.wHour;
      tm.tm_min   = wtm.wMinute;
      tm.tm_sec   = wtm.wSecond;
      tm. tm_isdst  = -1;
      clock = mktime(&tm);
      tp->tv_sec = (long)clock;
      tp->tv_usec = wtm.wMilliseconds * 1000;
      return (0);
    }
    #endif 

c++11 中的时间处理

c++11中时间的处理,放在一个的头文件中。支持多跨平台的使用。
主要涉及的类和函数

  1. 如何获取当前的时间
    #include 
    #include 
    using namespace std;
    
    int main()
    {
        // 获取当天的时间
        auto curTimePoint = chrono::system_clock::now();
        // 获取当前的时间戳
        cout << curTimePoint.time_since_epoch().count() << endl;
        auto d1 = chrono::seconds(10);
        // 把当前时间向后推10秒钟
        auto time2 = curTimePoint + d1;
        cout << time2.time_since_epoch().count() << endl;
        // 转为经过的小时
        cout << chrono::duration_cast(time2.time_since_epoch()).count() << endl;
    
        //转为time_t的结构
        time_t t = chrono::system_clock::to_time_t(time2);
        cout << t << endl;
    } 
其他获取时间的方式

资料参考2020年cpp峰会资料

函数 精度(微秒) 耗时(时钟周期) 备注
clock 1 ~1800 发生系统调用
gettimeofday 1 ~69 不发生系统调用
clock_gettime 0.0265(38) ~67 不发生系统调用
std::chrono::system_clock 0.0274(38) ~68 c++11标准,建议使用
std::chrono::steady_clock 0.0272(28) ~68 c++11标准,建议使用
std::chrono::high_resolution_clock 0.0275(20) ~69 c++11标准,建议使用
rdtsc 0.00965(48) ~24 直接读寄存器的值,最快
有效的建议
  1. 如果是c++11或以上,尽量使用chrono相关的应用
  2. 如果要求精度特别高,使用rdtsc
  3. 如果是纯c语言获取时间戳time。如果要获取到精度比较高的。使用gettimeofday
请关注微信公众号:cpp有效编程
上一篇:C++ 中的闭包
下一篇:没有了