time.h
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();
00092
00093 float in_sec() const;
00094 long in_msec() const;
00095 long in_usec() const;
00096 const timeval* get_timeval() const;
00097
00098 void set_time(const timeval* tv);
00099 void set_time(long int sec, long int usec);
00100 void set_time(long ms);
00101 void set_time(float sec);
00102 void set_time(const Time &t);
00103 void set_time(const Time *t);
00104
00105 void add(float seconds);
00106
00107 Time & stamp();
00108 Time & stamp_systime();
00109
00110 Time operator+(const float sec) const;
00111 Time operator+(const Time& t) const;
00112 Time operator+(const Time* t) const;
00113 Time operator-(const Time& t) const;
00114 float operator-(const Time* t) const;
00115 Time & operator+=(const long int usec);
00116 Time & operator+=(const Time& t);
00117 Time & operator+=(const float sec);
00118 Time & operator-=(const Time& t);
00119 Time & operator=(const Time& t);
00120
00121 void wait();
00122 void wait_systime();
00123
00124 const char * str(bool utc = false);
00125 void str_r(char *s, bool utc = false);
00126
00127 static const unsigned int TIMESTR_SIZE;
00128
00129 private:
00130 Clock *__clock;
00131 timeval __time;
00132 char *__timestr;
00133 };
00134
00135 }
00136
00137 #endif