00001 #ifndef __timer_h__ 00002 #define __timer_h__ 00003 00004 #ifdef HAVE_SYS_TYPES_H 00005 #include <sys/types.h> 00006 #endif 00007 00008 #ifdef TIME_WITH_SYS_TIME 00009 #include <sys/time.h> 00010 #include <time.h> 00011 #else 00012 #ifdef HAVE_SYS_TIME_H 00013 #include <sys/time.h> 00014 #else 00015 #include <time.h> 00016 #endif 00017 #endif 00018 00019 #ifdef HAVE_UNISTD_H 00020 #include <unistd.h> 00021 #endif 00022 00023 #include <string> 00024 00025 #include "asserts.h" 00026 #include "types.h" 00027 00028 /** Used as a stopwatch */ 00029 class timer 00030 { 00031 public: 00032 typedef time_t value_type; 00033 typedef double duration_type; 00034 00035 timer(); 00036 timer(const timer& a_timer); 00037 timer(const value_type a_start); 00038 timer(const value_type a_start, const value_type a_stop); 00039 00040 void clear(void); 00041 void start(void); 00042 void stop(void); 00043 00044 const value_type start_value(void) const; 00045 const value_type stop_value(void) const; 00046 00047 void assign(const timer& a_timer); 00048 void assign(const value_type a_start); 00049 void assign(const value_type a_start, const value_type a_stop); 00050 00051 timer& operator=(const timer& a_timer); 00052 00053 void use_localtime(const bool a_switch); 00054 const bool use_localtime(void) const; 00055 00056 const std::string started_at(void) const; 00057 const std::string stopped_at(void) const; 00058 const std::string duration(void) const; 00059 const std::string eta(unsigned int a_percent_complete) const; 00060 const std::string eta(unsigned int a_complete, unsigned int a_total) const; 00061 const std::string bps(uint64 a_bytes) const; 00062 00063 const bool is_started(void) const; 00064 const bool is_stopped(void) const; 00065 00066 const duration_type duration_secs(void) const; 00067 const duration_type duration_mins(void) const; 00068 const duration_type duration_hours(void) const; 00069 const duration_type duration_days(void) const; 00070 const duration_type duration_years(void) const; 00071 00072 private: 00073 value_type m_start; 00074 value_type m_stop; 00075 bool m_started; 00076 bool m_stopped; 00077 duration_type m_duration; 00078 bool m_use_localtime; 00079 00080 void mf_start_value(const value_type a_t); 00081 void mf_stop_value(const value_type a_t); 00082 const duration_type mf_calculate_duration( 00083 const value_type a_start, const value_type a_stop) const; 00084 const std::string mf_make_timer_string(const value_type a_t) const; 00085 const std::string mf_make_string(const value_type a_t) const; 00086 const std::string mf_make_string(const duration_type a_d) const; 00087 }; 00088 00089 const std::string current_time(void); 00090 00091 /** A null timer */ 00092 const timer null_timer(0,0); 00093 00094 std::string stamp(const pid_t = 0, const int a_indention = 0); 00095 00096 #endif