These are discussed in more detail in the following sections.
//not compiled, just a detailed sketch template<class clock> class timer { public: enum START_OPTION = {AUTOSTART, MANUAL_START}; timer(time_duration initial_duration = time_duration(0,0,0) START_OPTION = AUTOSTART) : start_time_(ptime(not_a_date_time)), elapsed_(initial_duration), { if (START_OPTION == AUTO_START) { start(); } } void start() { start_time_ = clock::local_time(); } void restart() { clear_elapsed(); start(); } time_duration elapsed() const { if (start_time_ != ptime(not_a_date_time)) { boost::posix_time::ptime current = clock::local_time(); elapsed_ += (current - start_time_); start_time_ = current; } return elapsed_; } void pause() { boost::posix_time::ptime current = clock::local_time(); elapsed_ += (current - start_time_); start_time_ = ptime(not_a_date_time); //not_a_date_time signals a not-running state } void continue() { boost::posix_time::ptime current = clock::local_time(); elapsed_ += (current - start_time_); start_time_ = ptime(not_a_date_time); } void clear_elapsed() { elapsed_ = time_duration(0,0,0); } private: mutable boost::posix_time::ptime start_time_; boost::posix_time::time_duration elapsed_; }; // timer
//just replacing the current timer interfaces with date-time classes like time_duration and posix_time class timer { public: timer() : state_time_(boost::posix_time::microsec_clock::local_time()) {} // postcondition: elapsed()==0 void restart() { start_time_ = microsec_clock::local_time(); } // post: elapsed()==0 time_duration elapsed() const { return boost::posix_time::microsec_clock::local_time() - start_time_;}
//this would depend on the clock. TBD on how to replace this not sure it's needed given time_duration range... //double elapsed_max() const // return estimated maximum value for elapsed() // Portability warning: elapsed_max() may return too high a value on systems // where std::clock_t overflows or resets at surprising values. //{ // return (double((std::numeric_limits<std::clock_t>::max)()) // - double(_start_time)) / double(CLOCKS_PER_SEC); //}
//this would depend on the clock. TBD on how to replace this //double elapsed_min() const // return minimum value for elapsed() // { return double(1)/double(CLOCKS_PER_SEC); }
private: boost::posix_time::ptime start_time_; }; // timer
T.B.D