Changeset 82:bcdf2a9fd146 in rrlib_util


Ignore:
Timestamp:
28.03.2013 18:36:34 (7 years ago)
Author:
Patrick Fleischmann <fleischmann@…>
Branch:
default
Phase:
public
Transplant:
048fe7fc49d3dbbe210747dab5db8dfe8e3005c4
Message:

Added compatibility and conversion to new rrlib::time libraries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tTime.h

    r43 r82  
    2929#include <sys/time.h> 
    3030#include <iostream> 
     31#include "rrlib/time/time.h" 
    3132 
    3233namespace rrlib 
     
    4546//! Repesents times (absolutes and differences) 
    4647/*! Use this class whenever you want to deal with times, 
    47   as it provides a number of operators and functions. 
     48 as it provides a number of operators and functions. 
    4849 */ 
    49 class  tTime 
     50class tTime 
    5051{ 
    5152public: 
     
    5657    tv_sec = 0; 
    5758    tv_usec = 0; 
    58   }; 
    59  
     59  } 
     60  ; 
    6061 
    6162  //! constructor, takes a timeval for creation 
     
    8182  } 
    8283 
     84  //! constructor that gets a time in seconds plus microseconds 
     85  tTime(const rrlib::time::tTimestamp &timestamp) 
     86  { 
     87    // 1970-01-01T00:00:00+00:00 
     88    const rrlib::time::tTimestamp unix_timestamp = std::chrono::system_clock::from_time_t(0); 
     89    rrlib::time::tDuration duration = timestamp - unix_timestamp; 
     90    tv_sec = std::chrono::duration_cast < std::chrono::seconds > (duration).count(); 
     91    tv_usec = std::chrono::duration_cast < std::chrono::microseconds > (duration).count() - tv_sec * 1000000; 
     92    NormalizeTime(); 
     93  } 
     94 
    8395  //! This static function returns a tTime that contains the current System-time 
    8496  static inline tTime Now() 
     
    90102 
    91103  /*! Returns the time of the current thread (time elapsed since start 
    92     of thread. It exists only for internal MCA usage. You do not need 
    93     to call it directly. */ 
     104   of thread. It exists only for internal MCA usage. You do not need 
     105   to call it directly. */ 
    94106  static inline tTime TaskTime() 
    95107  { 
     
    115127 
    116128  /*! Returns a time that is calculated by 
    117     tTime::Now()+tTime.FromMSec(msec) 
    118   */ 
     129   tTime::Now()+tTime.FromMSec(msec) 
     130   */ 
    119131  static inline tTime FutureMSec(long msec) 
    120132  { 
     
    189201 
    190202  /*! Returns tTime in nanoseconds */ 
    191   inline long long ToNSec()const 
     203  inline long long ToNSec() const 
    192204  { 
    193205    return ((long long)tv_usec + (long long)tv_sec * 1000000) * 1000; 
     
    195207 
    196208  /*! Returns tTime in microseconds */ 
    197   inline long long ToUSec()const 
     209  inline long long ToUSec() const 
    198210  { 
    199211    return (long long)tv_usec + (long long)tv_sec * 1000000; 
     
    201213 
    202214  /*! Returns tTime in milli seconds rounded down to an long integer.*/ 
    203   inline long long ToMSec()const 
     215  inline long long ToMSec() const 
    204216  { 
    205217    return (long long)tv_usec / 1000 + (long long)tv_sec * 1000; 
     
    207219 
    208220  /*! Returns tTime in seconds rounded down to an long integer.*/ 
    209   inline long ToSec()const 
     221  inline long ToSec() const 
    210222  { 
    211223    return tv_sec; 
     
    213225 
    214226  /*! Don't use this function: Returns the tv_sec value if timeval which is the basis of tTime */ 
    215   inline long TvSec()const 
     227  inline long TvSec() const 
    216228  { 
    217229    return tv_sec; 
     
    243255 
    244256  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
    245   inline int Hours()const 
     257  inline int Hours() const 
    246258  { 
    247259    return tv_sec / 3600 % 24; 
     
    249261 
    250262  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
    251   inline int Minutes()const 
     263  inline int Minutes() const 
    252264  { 
    253265    return (tv_sec % 3600) / 60; 
     
    255267 
    256268  /*! Use this function if you want to express the time in hours, minutes and seconds */ 
    257   inline int Seconds()const 
     269  inline int Seconds() const 
    258270  { 
    259271    return (tv_sec % 3600) % 60; 
     
    261273 
    262274  /*! Use this function if you want to get the subseconds in milliseconds (rounded) */ 
    263   inline int MSeconds()const 
     275  inline int MSeconds() const 
    264276  { 
    265277    return tv_usec / 1000; 
     
    267279 
    268280  /*! Adds two times */ 
    269   inline tTime operator+(const tTime& b)const 
     281  inline tTime operator+(const tTime& b) const 
    270282  { 
    271283    tTime a = *this; 
     
    284296 
    285297  /*! Substracts the second time from the first */ 
    286   inline tTime operator-(const tTime& b)const 
     298  inline tTime operator-(const tTime& b) const 
    287299  { 
    288300    tTime a = *this; 
     
    301313 
    302314  /*! sign operator */ 
    303   inline tTime operator-()const 
     315  inline tTime operator-() const 
    304316  { 
    305317    tTime a(-tv_sec, tv_usec); 
     
    310322 
    311323  /*! Multiplies the time by a factor. Uses operator*= (see below). */ 
    312   inline tTime operator*(double factor)const 
     324  inline tTime operator*(double factor) const 
    313325  { 
    314326    tTime a = *this; 
     
    326338 
    327339  /*! Compares two variables of type tTime. Returns true if they are not equal*/ 
    328   inline bool operator!=(const tTime& b)const 
     340  inline bool operator!=(const tTime& b) const 
    329341  { 
    330342    return (tv_usec != b.tv_usec) || (tv_sec != b.tv_sec); 
     
    361373  } 
    362374 
     375  operator rrlib::time::tTimestamp() 
     376  { 
     377    rrlib::time::tTimestamp timestamp = std::chrono::system_clock::from_time_t((time_t) this->tv_sec); 
     378    return timestamp + std::chrono::duration_cast<rrlib::time::tDuration>(std::chrono::microseconds(this->tv_usec)); 
     379  } 
     380 
    363381  /*! Casts this tTime object into timespec object (consists of tv_sec/tv_nsec, see h)*/ 
    364382  operator timespec() 
    365383  { 
    366     timespec t = {tv_sec, 1000 * tv_usec}; 
     384    timespec t = { tv_sec, 1000 * tv_usec }; 
    367385    return t; 
    368386  } 
     
    375393    return std::string(buffer); 
    376394  } 
    377  
    378395 
    379396  // some standard time intervals to be used for timeouts etc 
     
    412429 
    413430  /*! Makes sure that tv_usec lies in the interval [0; 1000000] by: 
    414       - converting multiples of 1000000 usec into multiples of sec 
    415       - transforming tv_sec and tv_usec so that the usec part is positive 
    416   */ 
     431   - converting multiples of 1000000 usec into multiples of sec 
     432   - transforming tv_sec and tv_usec so that the usec part is positive 
     433   */ 
    417434  inline void NormalizeTime() 
    418435  { 
     
    424441    if (tv_usec < 0) 
    425442    { 
    426       tv_sec --; 
     443      tv_sec--; 
    427444      tv_usec += 1000000; 
    428445    } 
     
    460477} 
    461478 
     479 
    462480#ifdef _LIB_RRLIB_SERIALIZATION_PRESENT_ 
    463481 
Note: See TracChangeset for help on using the changeset viewer.