Chrono
Durations
Measure time spans, such as one minute, two hours, or ten milliseconds
- #include <iostream>
- #include <chrono>
-
- int main(int argc, char *argv[])
- {
- //milliseconds
- //std::chrono::milliseconds m(10);
- //std::cout<<std::chrono::milliseconds::period::num<<std::endl;//1
- //std::cout<<std::chrono::milliseconds::period::den<<std::endl;/1000
-
- typedef std::chrono::duration<int, std::ratio<1, 1>> seconds_t;
- typedef std::chrono::duration<int, std::milli> millis_t;
- typedef std::chrono::duration<int, std::ratio<3600, 1>> hours_t;
-
- hours_t h(24);//24 hours
- seconds_t s(3600*24);//3600*24 seconds
- millis_t m(s);//3600*24*1000 ms
-
- std::cout<<"One day is "<<h.count()<<" hours"<<std::endl;
- std::cout<<"One day is "<<s.count()<<" seconds"<<std::endl;
- std::cout<<"One day is "<<m.count()<<" milliseconds"<<std::endl;
-
- return 0;
- }
-
- #include <iostream>
- #include <chrono>
-
- int main(int argc, char *argv[])
- {
- auto t1 = std::chrono::steady_clock::now();
- std::cout<<"Interval between two time points ..."<<std::endl;
- auto t2 = std::chrono::steady_clock::now();
-
- auto d = t2 - t1;
-
- if(d == std::chrono::steady_clock::duration::zero())
- {
- std::cout<<"The internal clock did not tick ..."<<std::endl;
- }
-
- std::cout<<d.count()<<std::endl;
-
- return 0;
- }
-
Time Points
A refernce to a specific point in time
- #include <iostream>
- #include <chrono>
-
- int main(int argc, char *argv[])
- {
- std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
- std::chrono::system_clock::duration dtn = tp.time_since_epoch();
-
- std::cout<<"Current time since epoch: "<<dtn.count()<<" microseconds ..."<<std::endl;
-
- return 0;
- }
-
- #include <iostream>
- #include <chrono>
- #include <ctime>
-
- int main(int argc, char *argv[])
- {
- std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
- std::time_t tt = std::chrono::system_clock::to_time_t(tp);//convert time point to time
- std::cout<<"Time: "<<ctime(&tt)<<std::endl;
-
- return 0;
- }
-
System_clock
A system-wide realtime clock
- #include <iostream>
- #include <chrono>
- #include <ctime>
-
- int main(int argc, char *argv[])
- {
- if(std::chrono::system_clock::is_steady)
- std::cout<<"Steady ..."<<std::endl;
- else
- std::cout<<"System time is not steady ..."<<std::endl;
-
- //now and to_time_t
- auto t = std::chrono::system_clock::now();
- auto tt = std::chrono::system_clock::to_time_t(t);
- std::cout<<"Time: "<<ctime(&tt)<<std::endl;
-
- //from_time_t
- time_t tt2;
- time(&tt2);
- std::cout<<"Time: "<<ctime(&tt2)<<std::endl;
- auto t2 = std::chrono::system_clock::from_time_t(tt2);
-
- auto d = std::chrono::system_clock::now() - t;
- std::cout<<"Count: "<<d.count()<<std::endl;
- std::cout<<decltype(d)::period::den<<std::endl;
-
- return 0;
- }
-
Steady_clock
Specifically designed to calculated time intervals
- #include <iostream>
- #include <chrono>
- #include <ctime>
-
- int main(int argc, char *argv[])
- {
- //now
- auto t = std::chrono::steady_clock::now();
-
- for(int i = 0; i < 100; i++)
- std::cout<<"*"<<" ";
- std::cout<<std::endl;
-
- auto d = std::chrono::steady_clock::now() - t;
- std::cout<<"Count: "<<d.count()<<std::endl;
- std::cout<<decltype(d)::period::den<<std::endl;
-
- return 0;
- }
-
Reference