Changeset 20:31efb5377bba in rrlib_util


Ignore:
Timestamp:
10.03.2011 16:00:54 (9 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Convert:
svn:dcce5f72-5200-4218-9373-75d9081937af/trunk@27
Message:

added complete tTime implementation

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tTime.cpp

    r18 r20  
    2626#include "rrlib/util/tTime.h" 
    2727 
    28 std::ostream& rrlib::util::operator<<(std::ostream& str, const rrlib::util::tTime& time) 
    29 { 
    30   return str << "(" << time.TvSec() << ", " << time.TvUSec() << ")"; 
    31 } 
     28using rrlib::util::tTime; 
    3229 
    33 const bool rrlib::util::operator == (const tTime &a, const tTime &b) 
    34 { 
    35   return a.TvSec() == b.TvSec() && a.TvUSec() == b.TvUSec(); 
    36 } 
    37  
    38 const bool rrlib::util::operator != (const tTime &a, const tTime &b) 
    39 { 
    40   return !(a == b); 
    41 } 
    42 const bool rrlib::util::operator < (const tTime &a, const tTime &b) 
    43 { 
    44   return a.TvSec() == b.TvSec() ? a.TvUSec() < b.TvUSec() : a.TvSec() < b.TvSec(); 
    45 } 
    46  
    47 const bool rrlib::util::operator > (const tTime &a, const tTime &b) 
    48 { 
    49   return !(a == b || a < b); 
    50 } 
     30const tTime tTime::time_forever(-1, 0); 
     31const tTime tTime::time_0ms(0, 0); 
     32const tTime tTime::time_1us(0, 1); 
     33const tTime tTime::time_1ms(0, 1000); 
     34const tTime tTime::time_5ms(0, 5000); 
     35const tTime tTime::time_10ms(0, 10000); 
     36const tTime tTime::time_20ms(0, 20000); 
     37const tTime tTime::time_25ms(0, 25000); 
     38const tTime tTime::time_30ms(0, 30000); 
     39const tTime tTime::time_40ms(0, 40000); 
     40const tTime tTime::time_50ms(0, 50000); 
     41const tTime tTime::time_100ms(0, 100000); 
     42const tTime tTime::time_200ms(0, 200000); 
     43const tTime tTime::time_250ms(0, 250000); 
     44const tTime tTime::time_300ms(0, 300000); 
     45const tTime tTime::time_400ms(0, 400000); 
     46const tTime tTime::time_500ms(0, 500000); 
     47const tTime tTime::time_1s(1, 0); 
     48const tTime tTime::time_2s(2, 0); 
     49const tTime tTime::time_5s(5, 0); 
     50const tTime tTime::time_10s(10, 0); 
     51const tTime tTime::time_30s(30, 0); 
     52const tTime tTime::time_60s(60, 0); 
     53const tTime tTime::time_120s(120, 0); 
     54const tTime tTime::time_180s(180, 0); 
     55const tTime tTime::time_240s(240, 0); 
     56const tTime tTime::time_30000s(30000, 0); 
     57const tTime tTime::time_1year(365 * 86400, 0); 
  • tTime.h

    r18 r20  
    3434namespace util 
    3535{ 
    36 class tTime 
     36//! Repesents times (absolutes and differences) 
     37/*! Use this class whenever you want to deal with times, 
     38  as it provides a number of operators and functions. 
     39 */ 
     40class  tTime 
    3741{ 
    3842public: 
    39   tTime(): tv_sec(0), tv_usec(0) 
    40   {} 
    41  
    42   tTime(timeval time): tv_sec(time.tv_sec), tv_usec(time.tv_usec) 
    43   {} 
     43 
     44  //! standard constructor, creates a null-time 
     45  tTime() 
     46  { 
     47    tv_sec = 0; 
     48    tv_usec = 0; 
     49  }; 
     50 
     51 
     52  //! constructor, takes a timeval for creation 
     53  tTime(const tTime & t) 
     54  { 
     55    tv_sec = t.tv_sec; 
     56    tv_usec = t.tv_usec; 
     57  } 
     58 
     59  //! constructor, takes a timeval for creation 
     60  tTime(timeval t) 
     61  { 
     62    tv_sec = t.tv_sec; 
     63    tv_usec = t.tv_usec; 
     64  } 
    4465 
    4566  //! constructor that gets a time in seconds plus microseconds 
     
    5172  } 
    5273 
    53  
    54   inline void SetTvSec(long new_tv_sec) 
    55   { 
    56     this->tv_sec = new_tv_sec; 
    57   } 
    58   inline void SetTvUSec(long new_tv_usec) 
    59   { 
    60     this->tv_usec = new_tv_usec; 
    61   } 
    62   inline long TvSec()const 
    63   { 
    64     return this->tv_sec; 
    65   } 
    66   inline long TvUSec()const 
    67   { 
    68     return this->tv_usec; 
     74  //! This static function returns a tTime that contains the current System-time 
     75  static inline tTime Now() 
     76  { 
     77    timeval ntime; 
     78    gettimeofday(&ntime, 0); 
     79    return tTime(ntime); 
     80  } 
     81 
     82  /*! Returns the time of the current thread (time elapsed since start 
     83    of thread. It exists only for internal MCA usage. You do not need 
     84    to call it directly. */ 
     85  static inline tTime TaskTime() 
     86  { 
     87# if (CLOCKS_PER_SEC == 1000000) 
     88 
     89    return tTime(0, clock()); 
     90# else 
     91 
     92    return tTime(0, clock()*(1000000 / CLOCKS_PER_SEC)); 
     93# endif 
     94 
     95  } 
     96 
     97  /*! Returns a time that is calculated by tTime::Now()+tTime(0,usec) 
     98   */ 
     99  static inline tTime FutureUSec(long usec) 
     100  { 
     101    tTime ntime(0, usec); 
     102    //FromUSec(usec); 
     103    ntime += tTime::Now(); 
     104    return ntime; 
     105  } 
     106 
     107  /*! Returns a time that is calculated by 
     108    tTime::Now()+tTime.FromMSec(msec) 
     109  */ 
     110  static inline tTime FutureMSec(long msec) 
     111  { 
     112    tTime ntime(0, msec*1000); 
     113    //FromMSec(msec); 
     114    ntime += tTime::Now(); 
     115    return ntime; 
     116  } 
     117 
     118  /*! Returns a time that is calculated by tTime::Now()+tTime(sec,0) 
     119   */ 
     120  static inline tTime FutureSec(long sec) 
     121  { 
     122    tTime ntime(sec, 0); 
     123    //FromSec(sec); 
     124    ntime += tTime::Now(); 
     125    return ntime; 
     126  } 
     127 
     128  /*! Sets tTime to zero. Return value is tTime itself. */ 
     129  inline tTime FromZero() 
     130  { 
     131    tv_usec = 0; 
     132    tv_sec = 0; 
     133    return *this; 
     134  } 
     135 
     136  /*! Sets tTime to the current  Return value is tTime itself. */ 
     137  inline tTime FromNow() 
     138  { 
     139    *this = Now(); 
     140    return *this; 
     141  } 
     142 
     143  /*! Sets tTime to the current task  Return value is tTime itself. */ 
     144  inline tTime FromTaskTime() 
     145  { 
     146    *this = TaskTime(); 
     147    return *this; 
     148  } 
     149 
     150  /*! Sets tTime to tTime(sec,0). Return value is tTime itself. */ 
     151  inline tTime FromSec(long sec) 
     152  { 
     153    *this = tTime(sec, 0); 
     154    return *this; 
     155  } 
     156 
     157  /*! Splits msec in sec and usec and sets tTime to tTime(sec,usec). Return value is tTime itsself. */ 
     158  inline tTime FromMSec(long msec) 
     159  { 
     160    if (msec < 0) 
     161      *this = tTime(msec / 1000 - 1, ((msec % 1000) + 1000) * 1000); 
     162    else 
     163      *this = tTime(msec / 1000, (msec % 1000) * 1000); 
     164    return *this; 
    69165  } 
    70166  inline tTime FromUSec(long usec) 
     
    76172    return *this; 
    77173  } 
    78   //! This static function returns a tTime that contains the current System-time 
    79   static inline tTime Now() 
    80   { 
    81     timeval ntime; 
    82     gettimeofday(&ntime, 0); 
    83     return tTime(ntime); 
    84   } 
    85  
    86   /*! Returns tTime in seconds rounded down to an long integer.*/ 
    87   inline long long ToSec()const 
    88   { 
    89     return this->tv_sec; 
     174 
     175  /*! Compares a tTime with zero. */ 
     176  inline bool IsZero() const 
     177  { 
     178    return ((tv_usec == 0) && (tv_sec == 0)); 
     179  } 
     180 
     181  /*! Returns tTime in nanoseconds */ 
     182  inline long long ToNSec()const 
     183  { 
     184    return ((long long)tv_usec + (long long)tv_sec*1000000)*1000; 
     185  } 
     186 
     187  /*! Returns tTime in microseconds */ 
     188  inline long long ToUSec()const 
     189  { 
     190    return (long long)tv_usec + (long long)tv_sec*1000000; 
    90191  } 
    91192 
     
    96197  } 
    97198 
    98   /*! Returns tTime in microseconds */ 
    99   inline long long ToUSec()const 
    100   { 
    101     return (long long)tv_usec + (long long)tv_sec*1000000; 
     199  /*! Returns tTime in seconds rounded down to an long integer.*/ 
     200  inline long ToSec()const 
     201  { 
     202    return tv_sec; 
     203  } 
     204 
     205  /*! Don't use this function: Returns the tv_sec value if timeval which is the basis of tTime */ 
     206  inline long TvSec()const 
     207  { 
     208    return tv_sec; 
     209  } 
     210 
     211  /*! Don't use this function: Returns the tv_usec value if timeval which is the basis of tTime */ 
     212  inline long TvUSec() const 
     213  { 
     214    return tv_usec; 
     215  } 
     216 
     217  /*! Don't use this function: Returns the tv_usec value if timeval which is the basis of tTime */ 
     218  inline long TvNSec() const 
     219  { 
     220    return tv_usec * 1000; 
     221  } 
     222 
     223  /*! Don't use this function: Sets the internal tv_sec variable of timeval */ 
     224  inline void SetTvSec(long new_tv_sec) 
     225  { 
     226    tv_sec = new_tv_sec; 
     227  } 
     228 
     229  /*! Don't use this function: Sets the internal tv_usec variable of timeval */ 
     230  inline void SetTvUSec(long new_tv_usec) 
     231  { 
     232    tv_usec = new_tv_usec; 
     233  } 
     234 
     235  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
     236  inline int Hours()const 
     237  { 
     238    return tv_sec / 3600 % 24; 
     239  } 
     240 
     241  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
     242  inline int Minutes()const 
     243  { 
     244    return (tv_sec % 3600) / 60; 
     245  } 
     246 
     247  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
     248  inline int Seconds()const 
     249  { 
     250    return (tv_sec % 3600) % 60; 
     251  } 
     252 
     253  /*! Use this function if you want to get the subseconds in milliseconds (rounded) */ 
     254  inline int MSeconds()const 
     255  { 
     256    return tv_usec / 1000; 
     257  } 
     258 
     259  /*! Adds two times */ 
     260  inline tTime operator+(const tTime& b)const 
     261  { 
     262    tTime a = *this; 
     263    return a += b; 
     264  } 
     265 
     266  /*! Adds a second time */ 
     267  inline tTime operator+=(const tTime& b) 
     268  { 
     269    tv_usec += b.tv_usec; 
     270    tv_sec += b.tv_sec; 
     271    //NormalizeTimePositive(); 
     272    NormalizeTime(); 
     273    return *this; 
    102274  } 
    103275 
     
    114286    tv_usec -= b.tv_usec; 
    115287    tv_sec -= b.tv_sec; 
     288    //NormalizeTimeNegative(); 
    116289    NormalizeTime(); 
    117290    return *this; 
    118291  } 
     292 
     293  /*! sign operator */ 
     294  inline tTime operator-()const 
     295  { 
     296    tTime a(-tv_sec, tv_usec); 
     297//     a.tv_usec = tv_usec; 
     298//     a.tv_sec = -tv_sec; 
     299    return a; 
     300  } 
     301 
     302  /*! Multiplies the time by a factor. Uses operator*= (see below). */ 
     303  inline tTime operator*(double factor)const 
     304  { 
     305    tTime a = *this; 
     306    return a *= factor; 
     307  } 
     308 
     309  /*! Multiplies by a factor. */ 
     310  inline tTime operator*=(double factor) 
     311  { 
     312    tv_usec = (long)(tv_usec * factor); 
     313    tv_sec = (long)(tv_sec * factor); 
     314    NormalizeTime(); 
     315    return *this; 
     316  } 
     317 
     318  /*! Compares two variables of type tTime. Returns true if they are not equal*/ 
     319  inline bool operator!=(const tTime& b)const 
     320  { 
     321    return (tv_usec != b.tv_usec) || (tv_sec != b.tv_sec); 
     322  } 
     323 
     324  /*! Compares two variables of type tTime. Returns true if they are equal*/ 
     325  inline bool operator==(const tTime& b) const 
     326  { 
     327    return (tv_usec == b.tv_usec) && (tv_sec == b.tv_sec); 
     328  } 
     329 
     330  /*! Compares two variables of type tTime. Returns true if the first one is earlier than the second one*/ 
     331  inline bool operator<(const tTime& b) const 
     332  { 
     333    return (tv_sec == b.tv_sec) ? (tv_usec < b.tv_usec) : (tv_sec < b.tv_sec); 
     334  } 
     335 
     336  /*! Compares two variables of type tTime. Returns true if the first one is later than the second one*/ 
     337  inline bool operator>(const tTime& b) const 
     338  { 
     339    return (tv_sec == b.tv_sec) ? (tv_usec > b.tv_usec) : (tv_sec > b.tv_sec); 
     340  } 
     341 
     342  /*! Compares two variables of type tTime. Returns true if the first one is erlier than or equal to the second one*/ 
     343  inline bool operator<=(const tTime& b) const 
     344  { 
     345    return (tv_sec == b.tv_sec) ? ((tv_usec == b.tv_usec) || (tv_usec < b.tv_usec)) : (tv_sec < b.tv_sec); 
     346  } 
     347 
     348  /*! Compares two variables of type tTime. Returns true if the first one is later than or equal to the second one*/ 
     349  inline bool operator>=(const tTime& b) const 
     350  { 
     351    return (tv_sec == b.tv_sec) ? ((tv_usec == b.tv_usec) || (tv_usec > b.tv_usec)) : (tv_sec > b.tv_sec); 
     352  } 
     353 
     354  /*! Casts this tTime object into timespec object (consists of tv_sec/tv_nsec, see h)*/ 
     355  operator timespec() 
     356  { 
     357    timespec t = {tv_sec, 1000*tv_usec}; 
     358    return t; 
     359  } 
     360 
     361  /*! Returns a formatted string for strftime-like-usage instead of getting "(sec, usec)", which is not really readable when used as global timestamp (date) */ 
     362  inline std::string GetString(const std::string &format_string) const 
     363  { 
     364    char buffer[64]; 
     365    strftime(buffer, sizeof(buffer), format_string.c_str(), localtime(&tv_sec)); 
     366    return std::string(buffer); 
     367  } 
     368 
     369 
     370  // some standard time intervals to be used for timeouts etc 
     371  static const tTime time_forever; 
     372  static const tTime time_0ms; 
     373  static const tTime time_1us; 
     374  static const tTime time_1ms; 
     375  static const tTime time_5ms; 
     376  static const tTime time_10ms; 
     377  static const tTime time_20ms; 
     378  static const tTime time_25ms; 
     379  static const tTime time_30ms; 
     380  static const tTime time_40ms; 
     381  static const tTime time_50ms; 
     382  static const tTime time_100ms; 
     383  static const tTime time_200ms; 
     384  static const tTime time_250ms; 
     385  static const tTime time_300ms; 
     386  static const tTime time_400ms; 
     387  static const tTime time_500ms; 
     388  static const tTime time_1s; 
     389  static const tTime time_2s; 
     390  static const tTime time_5s; 
     391  static const tTime time_10s; 
     392  static const tTime time_30s; 
     393  static const tTime time_60s; 
     394  static const tTime time_120s; 
     395  static const tTime time_180s; 
     396  static const tTime time_240s; 
     397  static const tTime time_30000s; 
     398  static const tTime time_1year; 
    119399 
    120400private: 
     
    139419    } 
    140420  } 
    141  
    142421}; 
    143422 
    144 std::ostream& operator<<(std::ostream& str, const tTime& time); 
    145  
    146 const bool operator == (const tTime &a, const tTime &b); 
    147  
    148 const bool operator != (const tTime &a, const tTime &b); 
    149  
    150 const bool operator < (const tTime &a, const tTime &b); 
    151  
    152 const bool operator > (const tTime &a, const tTime &b); 
    153  
     423//! Overloading the << operator for ostream 
     424/*! Outputs a time as a pair of long integer representing (seconds, microseconds). 
     425 */ 
     426inline std::ostream &operator<<(std::ostream &ost, const tTime& time) 
     427{ 
     428  ost << "(" << time.TvSec() << ", " << time.TvUSec() << ")"; 
     429  return ost; 
     430} 
     431 
     432//! Overloading the << operator for ostream 
     433/*! Outputs a time as a pair of long integer representing (seconds, microseconds). 
     434 */ 
     435inline std::istream &operator>>(std::istream &str, tTime &time) 
     436{ 
     437  char temp; 
     438  long int tv_sec, tv_usec; 
     439  str >> temp; 
     440  if (temp == '(') 
     441  { 
     442    str >> tv_sec >> temp >> tv_usec >> temp; 
     443  } 
     444  else 
     445  { 
     446    str.putback(temp); 
     447    str >> tv_sec >> tv_usec; 
     448  } 
     449  time = tTime(tv_sec, tv_usec); 
     450  return str; 
    154451} 
    155452} 
    156  
     453} 
    157454#endif 
Note: See TracChangeset for help on using the changeset viewer.