c++11之chrono类的时间使用

时间点与时间段

  

C++11 std::chrono时间库

typedef duration <Rep, ratio<3600,1>> hours;
typedef duration <Rep, ratio<60,1>> minutes;
typedef duration <Rep, ratio<1,1>> seconds;
typedef duration <Rep, ratio<1,1000>> milliseconds;
typedef duration <Rep, ratio<1,1000000>> microseconds;
typedef duration <Rep, ratio<1,1000000000>> nanoseconds;

例:
chrono::minutes mintu{2};//2分钟
chrono::seconds sec{3};//3秒钟
chrono::milliseconds mills{500};//500毫秒
auto dul = sec - mills;//两者差值,单位默认转到更小的 2500ms
dul.count(); //值为2500
std::this_thread::sleep_for(std::chrono::milliseconds(100)); //当前线程休眠100毫秒

//chrono::duration_cast<>() 时间单位转换
chrono::duration_cast<chrono::seconds>(mintu).count(); //2分钟换算为120秒 


//chrono::system_clock::time_point  转换 std::time_t 
std::time_t oldTime = time(nullptr);//c函数获取当前时间
cout << "oldTime = " << oldTime << endl;
chrono::system_clock::time_point timePoint = chrono::system_clock::now();//stl库获取当前时间
std::time_t newTime = chrono::system_clock::to_time_t(timePoint);//转换为旧式接口,单位:秒
cout<<"newTime = " << newTime <<endl;// oldTime == timeT


//时间间隔, std::chrono::steady_clock 为了表示稳定的时间间隔,后一次调用now()得到的时间总是比前一次的值大(这句话的意思其实是,如果中途修改了系统时间,也不影响now()的结果),每次tick都保证过了稳定的时间间隔。
// steady_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

using namespace std::chrono;

int main ()
{
  steady_clock::time_point t1 = steady_clock::now();
  std::this_thread::sleep_for(std::chrono::milliseconds(100)); //当前线程休眠100毫秒
  steady_clock::time_point t2 = steady_clock::now();

   std::chrono::nanoseconds tii = t2-t1;//
   //std::chrono::milliseconds ti = time_point_cast<milliseconds>(t2) - time_point_cast<milliseconds>(t1);
   auto ti = duration_cast<std::chrono::milliseconds>(t2 - t1);

   std::cout << tii.count() << " "<< ti.count() << "\n";

return 0;
}

//三种时钟类 system_clock 、  steady_clock 、 high_resolution_clock
std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now());
std::cout << tp.time_since_epoch().count() <<"\n";

 

 

// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <>

using namespace std;
using namespace std::chrono;

int main()
{
    using std::chrono::system_clock;

    std::chrono::duration<int> one_day(1);

    system_clock::time_point today = system_clock::now();
    system_clock::time_point tomorrow = today + one_day;

    std::time_t tt;

    tt = system_clock::to_time_t(today);
    
    // duration<long long, nano> nanoseconds;   //纳秒
    // duration<long long, micro> microseconds;   //微妙
    // duration<long long, milli> milliseconds;   //毫秒
    // duration<long long> seconds;
    // duration<int, ratio<60> > minutes;
    // duration<int, ratio<3600> > hours;

    std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(tomorrow- today);
    //std::cout << "time is: " << ms.count() << "\n";
    std::cout << "time is: " <<today.time_since_epoch().count() << "\n"; //纳秒
    std::cout << "time is: " << std::chrono::duration_cast<std::chrono::milliseconds>(today.time_since_epoch()).count() << "\n";  //毫秒

    //std::cout << "today is: " << ctime(&tt);
    std::cout << "today is: " << tt <<"\n";
    tt = system_clock::to_time_t(tomorrow);
    //std::cout << "tomorrow will be: " << ctime(&tt);
    std::cout << "tomorrow will be: " << tt<<"\n";
    return 0;
}

 参考  C++11中的时间库std::chrono(引发关于时间的思考)_AlbertS Home of Technology-CSDN博客

posted @ 2021-07-12 14:59  玥茹苟  阅读(517)  评论(0编辑  收藏  举报