//clang #include #include #include #include #include #include using namespace std::chrono; // note! using namespace std; void myfun() { cout << "waiting..."; int c; cin >> c; } void writetime(ostream &os, time_t tc) { // alternative to put_time iomanip locale loc; const time_put& tp = use_facet>(loc); const char *pat = "%F %T"; tp.put(os,os,' ',localtime(&tc),pat,pat+strlen(pat)); } int main(int argc, char **argv) { // three clocks: steady_clock, system_clock, high_resolution_clock // steady_clock: monotonic // system_clock: "wall clock" time -- might be adjusted backward // high_resolution_clock: best accuracy clock -- might be one above // // each have member data types: // rep: type for counting ticks // period: std::ratio giving ticks per second // duration: type std::chrono::duration // represents durations // time_point: tpe std::chrono::time_point // represents time points // // all have method "is_steady()" which returns whether they // are steady (static method) // // all have method now() (static) that returns time_point // // system_clock has methods to_time_t and from_time_t // (static methods that convert to and from time_t structs) // // a duration has +,-,++,--,+=,-=,=,==,!=,<=,>=,<,> defined // it has min(), max(), and zero() (static members that return the // min, max, or zero representations) // it has count() that returns the number of ticks // Using durations: // ================ // auto = std::chrono::time_point auto t1 = high_resolution_clock::now(); myfun(); auto t2 = high_resolution_clock::now(); cout << "duration count = " << (t2-t1).count() << endl; cout << "duration in milliseconds = " << duration_cast(t2-t1).count() << endl; cout << "duration in seconds = " << duration_cast(t2-t1).count() << endl; cout << "duration in minutes = " << duration_cast(t2-t1).count() << endl; // nanoseconds, microseconds, and hours also defined cout << "duration in 20ths of a second = " << duration_cast>>(t2-t1).count() << endl; // Using abs times: // ================= auto t3 = system_clock::now(); time_t t3_c = system_clock::to_time_t(t3); // std:put_time declared in iomanip // the following should work, but gcc doesn't seem to support // the put_time IO manipulator... //cout << "the current time is " << std::put_time(localtime(&t3_c),"%F %T") << endl; cout << "the current time is "; writetime(cout,t3_c); cout << endl; cout << "the time 5 minutes ago was "; writetime(cout,system_clock::to_time_t(t3-minutes(5))); cout << endl; cout << "the time 3 days ago was "; writetime(cout,system_clock::to_time_t(t3-hours(24*3))); cout << endl; // Pausing: // ======== // this uses the threads part of STL, which I'll cover more later // (currently, g++ 4.7 doesn't compile below unless _GLIBCXX_USE_NANOSLEEP // is defined -- not sure why cout << "start..." << endl; std::this_thread::sleep_for(seconds(2)); cout << "...end" << endl; // don't finish until 10 seconds after program started... cout << "start.." << endl; std::this_thread::sleep_until(t1+seconds(10)); cout << "...end" << endl; }