00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __UTILS_TIME_TIME_H_
00025 #define __UTILS_TIME_TIME_H_
00026
00027 #include <sys/time.h>
00028
00029 namespace fawkes {
00030
00031
00032
00033
00034
00035
00036
00037
00038 inline float
00039 time_diff_sec(const timeval &a, const timeval &b)
00040 {
00041
00042 double res = a.tv_sec - b.tv_sec + (a.tv_usec - b.tv_usec) / 1000000.0;
00043 return res;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 inline float
00057 time_diff_sec(const long int a_sec, const long int a_usec,
00058 const long int b_sec, const long int b_usec)
00059 {
00060
00061 double res = a_sec - b_sec + (a_usec - b_usec) / 1000000.0;
00062 return res;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 inline long int
00073 time_diff_usec(const timeval &a, const timeval &b)
00074 {
00075 return (a.tv_sec - b.tv_sec) * 1000000 + (a.tv_usec - b.tv_usec);
00076 }
00077
00078 class Clock;
00079
00080 class Time
00081 {
00082 friend class Clock;
00083 public:
00084 Time();
00085 Time(const timeval* tv);
00086 Time(long sec, long usec, Clock *clock = 0);
00087 Time(long ms);
00088 Time(float sec);
00089 Time(Clock *clock);
00090 Time(const Time &t);
00091 Time(const Time *t);
00092 ~Time();
00093
00094 float in_sec() const;
00095 long in_msec() const;
00096 long in_usec() const;
00097
00098 const timeval * get_timeval() const { return &__time; }
00099 long get_sec() const { return __time.tv_sec; }
00100 long get_msec() const { return __time.tv_usec * 1000; }
00101 long get_usec() const { return __time.tv_usec; }
00102 void get_timestamp(long &sec, long &usec) const
00103 { sec = __time.tv_sec; usec = __time.tv_usec; }
00104
00105 void set_time(const timeval* tv);
00106 void set_time(long int sec, long int usec);
00107 void set_time(long ms);
00108 void set_time(float sec);
00109 void set_time(const Time &t);
00110 void set_time(const Time *t);
00111
00112 void set_clock(Clock *clock);
00113
00114 void add(float seconds);
00115
00116 Time & stamp();
00117 Time & stamp_systime();
00118
00119 Time operator+(const float sec) const;
00120 Time operator+(const Time& t) const;
00121 Time operator+(const Time* t) const;
00122 Time operator-(const Time& t) const;
00123 float operator-(const Time* t) const;
00124 Time & operator+=(const long int usec);
00125 Time & operator+=(const Time& t);
00126 Time & operator+=(const float sec);
00127 Time & operator-=(const Time& t);
00128 Time & operator=(const Time& t);
00129 bool operator==(const Time& t) const;
00130 bool operator==(const Time* t) const;
00131 bool operator!=(const Time& t) const;
00132 bool operator!=(const Time* t) const;
00133
00134 void wait();
00135 void wait_systime();
00136
00137 const char * str(bool utc = false) const;
00138 void str_r(char *s, bool utc = false);
00139
00140 static const unsigned int TIMESTR_SIZE;
00141
00142 private:
00143 Clock *__clock;
00144 timeval __time;
00145 mutable char *__timestr;
00146 };
00147
00148 }
00149
00150 #endif