Fawkes API  Fawkes Development Version
time.h
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 #include <cmath>
00029 
00030 namespace fawkes {
00031 
00032 /** Calculate time difference of two time structs.
00033  * The calculated time is t = a - b, where t is a represented as the number of
00034  * seconds in a single precision float.
00035  * @param a time to subtract from
00036  * @param b time to subtract
00037  * @return a - b
00038  */
00039 inline double
00040 time_diff_sec(const timeval &a, const timeval &b)
00041 {
00042   //double required if we do not want to loose the usecs
00043   double res = a.tv_sec  - b.tv_sec + (a.tv_usec - b.tv_usec) / 1000000.0;
00044   return res;
00045 }
00046 
00047 
00048 /** Calculate time difference of two time structs.
00049  * The calculated time is t = a - b, where t is a represented as the number of
00050  * seconds in a single precision float.
00051  * @param a_sec seconds of time to subtract from
00052  * @param a_usec microseconds of time to subtract from
00053  * @param b_sec seconds of time to subtract
00054  * @param b_usec microseconds of time to subtract
00055  * @return a_sec - b_sec  + (a_usec - b_usec) / 1000000.f
00056  */
00057 inline double
00058 time_diff_sec(const long int a_sec, const long int a_usec,
00059               const long int b_sec, const long int b_usec)
00060 {
00061   //double required if we do not want to loose the usecs
00062   double res = a_sec - b_sec + (a_usec - b_usec) / 1000000.0;
00063   return res;
00064 }
00065 
00066 
00067 /** Convert seconds to micro seconds.
00068  * @param sec seconds to convert
00069  * @return time in microseconds
00070  */
00071 inline long int
00072 time_sec_to_usec(double sec)
00073 {
00074   return (long)round(sec * 1000000.);
00075 }
00076 
00077 /** Get difference between two time structs in microseconds.
00078  * The calculated time is t = a - b
00079  * @param a time to subtract from
00080  * @param b time to subtract
00081  * @return difference between a and b in microseconds
00082  */
00083 inline long int
00084 time_diff_usec(const timeval &a, const timeval &b)
00085 {
00086   return (a.tv_sec - b.tv_sec) * 1000000 + (a.tv_usec - b.tv_usec);
00087 }
00088 
00089 class Clock;
00090 
00091 class Time
00092 {
00093  friend class Clock;
00094  public:
00095   Time();
00096   Time(const timeval* tv);
00097   Time(long sec, long usec, Clock *clock = 0);
00098   Time(long ms);
00099   Time(double sec);
00100   Time(Clock *clock);
00101   Time(const Time &t);
00102   Time(const Time *t);
00103   ~Time();
00104 
00105   double in_sec() const;
00106   long   in_msec() const;
00107   long   in_usec() const;
00108 
00109   const timeval * get_timeval() const { return &__time; }
00110   long            get_sec() const  { return __time.tv_sec; }
00111   long            get_msec() const { return __time.tv_usec / 1000; }
00112   long            get_usec() const { return __time.tv_usec; }
00113   long            get_nsec() const { return __time.tv_usec * 1000; }
00114   void            get_timestamp(long &sec, long &usec) const
00115                   { sec  = __time.tv_sec; usec = __time.tv_usec; }
00116   bool            is_zero() const { return (__time.tv_sec == 0) && (__time.tv_usec == 0); }
00117 
00118   void set_time(const timeval* tv);
00119   void set_time(long int sec, long int usec);
00120   void set_time(long ms);
00121   void set_time(double sec);
00122   void set_time(const Time &t);
00123   void set_time(const Time *t);
00124 
00125   void set_clock(Clock *clock);
00126 
00127   void add(double seconds);
00128 
00129   Time & stamp();
00130   Time & stamp_systime();
00131 
00132   Time   operator+(const double sec) const;
00133   Time   operator+(const long int usec) const;
00134   Time   operator+(const Time& t) const;
00135   Time   operator+(const Time* t) const;
00136   Time   operator-(const Time& t) const;
00137   double operator-(const Time* t) const;
00138   Time   operator-(const long int usec) const;
00139   Time   operator-(const double sec) const;
00140   Time & operator+=(const long int usec);
00141   Time & operator+=(const Time& t);
00142   Time & operator+=(const double sec);
00143   Time & operator-=(const Time& t);
00144   Time & operator-=(const double sec);
00145   Time & operator-=(const long int usec);
00146   Time & operator=(const Time& t);
00147   bool   operator==(const Time& t) const;
00148   bool   operator==(const Time* t) const;
00149   bool   operator!=(const Time& t) const;
00150   bool   operator!=(const Time* t) const;
00151   bool   operator>(const Time& t) const;
00152   bool   operator>(const Time* t) const;
00153   bool   operator>=(const Time& t) const;
00154   bool   operator>=(const Time* t) const;
00155   bool   operator<(const Time& t) const;
00156   bool   operator<(const Time* t) const;
00157   bool   operator<=(const Time& t) const;
00158   bool   operator<=(const Time* t) const;
00159 
00160   void wait();
00161   void wait_systime();
00162 
00163   const char * str(bool utc = false) const;
00164   void         str_r(char *s, bool utc = false);
00165 
00166   static const unsigned int TIMESTR_SIZE;
00167 
00168  private:
00169   Clock   *__clock;
00170   timeval  __time;
00171   mutable char    *__timestr;
00172 };
00173 
00174 extern const Time TIME_MAX;
00175 extern const Time TIME_MIN;
00176 
00177 } // end namespace fawkes
00178 
00179 #endif