Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * time.h - Time utils 00004 * 00005 * Created: Wed Jan 18 15:56:33 2006 (from FireVision) 00006 * Copyright 2005-2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 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 /** Calculate time difference of two time structs. 00032 * The calculated time is t = a - b, where t is a represented as the number of 00033 * seconds in a single precision float. 00034 * @param a time to subtract from 00035 * @param b time to subtract 00036 * @return a - b 00037 */ 00038 inline float 00039 time_diff_sec(const timeval &a, const timeval &b) 00040 { 00041 //double required if we do not want to loose the usecs 00042 double res = a.tv_sec - b.tv_sec + (a.tv_usec - b.tv_usec) / 1000000.0; 00043 return res; 00044 } 00045 00046 00047 /** Calculate time difference of two time structs. 00048 * The calculated time is t = a - b, where t is a represented as the number of 00049 * seconds in a single precision float. 00050 * @param a_sec seconds of time to subtract from 00051 * @param a_usec microseconds of time to subtract from 00052 * @param b_sec seconds of time to subtract 00053 * @param b_usec microseconds of time to subtract 00054 * @return a_sec - b_sec + (a_usec - b_usec) / 1000000.f 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 //double required if we do not want to loose the usecs 00061 double res = a_sec - b_sec + (a_usec - b_usec) / 1000000.0; 00062 return res; 00063 } 00064 00065 00066 /** Get difference between two time structs in microseconds. 00067 * The calculated time is t = a - b 00068 * @param a time to subtract from 00069 * @param b time to subtract 00070 * @return difference between a and b in microseconds 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 } // end namespace fawkes 00149 00150 #endif