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
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();
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 } // end namespace fawkes
00136
00137 #endif