LRAUV  revA
Timestamp.h
Go to the documentation of this file.
1 
9 #ifndef TIMESTAMP_H_
10 #define TIMESTAMP_H_
11 
12 #include <sys/time.h>
13 
14 class Str;
15 class Timespan;
16 
31 class Timestamp
32 {
33 public:
37  static const Timestamp NOT_SET_TIME;
38 
41 
43  Timestamp( const double& seconds );
44 
46  Timestamp( const int seconds, const unsigned int microseconds );
47 
49  Timestamp( const int year, const int month, const int day,
50  const int hour, const int minute, const int second );
51 
53  Timestamp( const struct timeval& timeval );
54 
56  Timestamp( const char* timeString );
57 
59  Timestamp( const Timestamp& copyMe = NOT_SET_TIME );
60 
62  static Timestamp FromMicros( const long long& micros );
63 
65  void addFromMicros( const long long& micros );
66 
68  static Timestamp FromMillis( const long long& millis );
69 
71  void addFromMillis( const long long& millis );
72 
73  // Round to the nearest #of seconds indicated.
74  // i.e., use 60 for nearest minute, 3600 for hour, etc
75  void round( int seconds );
76 
78  virtual ~Timestamp()
79  {}
80  ;
81 
83  Timespan elapsed() const;
84 
86  virtual double asDouble() const;
87 
89  virtual double asFloat() const;
90 
92  long long asMicros() const;
93 
95  long long asMillis() const;
96 
97  inline time_t asTimeT() const
98  {
99  return timeval_.tv_sec;
100  }
101 
112  struct tm asStructTm() const;
113 
114  //== Copy operators ==
116  Timestamp& operator= ( const Timestamp& rhs );
118  Timestamp& operator= ( const double& seconds );
120  Timestamp& operator= ( const struct timeval& timeval );
121 
122  //== Math operator ==
124  Timestamp& operator+= ( const Timespan& rhs );
126  Timestamp& operator-= ( const Timespan& rhs );
128  Timestamp operator+ ( const Timespan& rhs ) const;
130  Timestamp operator- ( const Timespan& rhs ) const;
132  Timespan operator- ( const struct timeval& rhs ) const;
134  Timespan operator- ( const Timestamp& rhs ) const;
135 
136  //== Inequalities ==
138  bool operator== ( const Timestamp& rhs ) const;
140  bool operator!= ( const Timestamp& rhs ) const;
142  bool operator> ( const Timestamp& rhs ) const;
144  bool operator< ( const Timestamp& rhs ) const;
146  bool operator<= ( const Timestamp& rhs ) const;
148  bool operator>= ( const Timestamp& rhs ) const;
149 
150  //== Functions for setting time ==
153 
154 #ifdef __FASTSIM
155  Timestamp& setToRealCurrentTime();
157 #endif
158 
161 
163  static Timestamp Now();
164 
165 #ifdef __FASTSIM
166 
167  static Timespan SetNow( const Timestamp newNow );
168 
169 #endif
170 
172  void sleepTill();
173 
175  void toTimespec( struct timespec* ) const;
176 
178  void fromTimespec( const struct timespec* in );
179 
181  const struct timeval& getTimeval() const
182  {
183  return timeval_;
184  };
185 
186  //== Put input/output functions here... ==
187 
194  const char* toString( char* buf, int buflen, int precision = 3 ) const;
195 
197  const Str toString( int precision = 3 ) const;
198 
200  const Str toSmallString() const;
201 
203  const Str toSmallerString() const;
204 
206  const Str toDateHourString() const;
207 
208 
209 protected:
210 
212  struct timeval timeval_;
213 
215  struct timeval& fromDouble( const double& seconds );
216 
218  struct timeval& fromIntegers( const int seconds, const unsigned int microseconds );
219 
221  struct timeval& fromIntegers( const int year, const int month, const int day,
222  const int hour, const int minute, const int second );
223 
225  struct timeval& fromString( const char* timeString );
226 
228  static struct timeval TimevalFromIntegers( const int seconds, const unsigned int microseconds );
229 
231  static struct timeval TimevalFromMicros( const long long& micros );
232 
234  static struct timeval TimevalFromMillis( const long long& millis );
235 
236 #ifdef __FASTSIM
237  // Allows for fast simulation
238  static Timespan TimeOffset_;
239 #endif
240 
241 };
242 
248 class Timespan : public Timestamp
249 {
250 public:
253 
255  static const Timespan ZERO_TIMESPAN;
256 
258  Timespan( const double& seconds );
259 
261  Timespan( const struct timeval& timeval );
262 
264  Timespan( const Timespan& copyMe = ZERO_TIMESPAN );
265 
267  static Timespan FromMicros( const long long& micros );
268 
270  static Timespan FromMillis( const long long& millis );
271 
274  {}
275  ;
276 
278  virtual double asDouble() const;
279 
281  virtual double asFloat() const;
282 
286  bool setDuration( const char* duration );
287 
288  //== Math operator ==
290  Timespan& operator+= ( const Timespan& rhs );
292  Timespan& operator-= ( const Timespan& rhs );
294  Timespan& operator-= ( const struct timeval& rhs );
295 
296  //== Functions for dealing with "whole units" of time ==
298  static Timespan Milliseconds( float msec )
299  {
300  return Timespan( msec / 1000.0 );
301  }
302 
307  static Timespan Seconds( double sec )
308  {
309  return Timespan( sec );
310  };
311 
313  static Timespan Minutes( int min )
314  {
315  return Timespan( 60.0 * min );
316  };
317 
319  static Timespan Hours( int hour )
320  {
321  return Timespan( hour * 3600.0 );
322  };
323 
325  static Timespan Days( int day )
326  {
327  return Timespan( day * 3600.0 * 24.0 );
328  };
329 
330 #ifdef __FASTSIM
331  void sleepFast( void ) const
332  {
333  TimeOffset_ += *this;
334  }
335 #endif
336 
338  void sleepFor( void ) const;
339 
340  //== Input/output ==
341 
342 
349  const char* toString( char* buf, int buflen, int precision = 3 ) const;
350 
352  const Str toString( int precision = 3 ) const;
353 
354 };
355 
356 #endif /*TIMESTAMP_H_*/
static const Timespan ZERO_TIMESPAN
Constant value to represent instant of time.
Definition: Timestamp.h:255
const Str toSmallerString() const
Writes the Timestamp as a smaller Str (YYYYmmddHHMM).
Definition: Timestamp.cpp:455
static Timespan Days(int day)
Static allows creation of Timespans denominated in days.
Definition: Timestamp.h:325
*struct tm asStructTm() const
Definition: Timestamp.cpp:163
const struct timeval & getTimeval() const
Get the internal storage.
Definition: Timestamp.h:181
const Str toSmallString() const
Writes the Timestamp as a small Str (YYYYmmdd'T'HHMMSS).
Definition: Timestamp.cpp:446
bool setDuration(const char *duration)
Sets the timespan using an XML-like duration string.
Definition: Timestamp.cpp:848
Timestamp & setToCurrentTime()
Set to the current time.
Definition: Timestamp.cpp:322
bool operator<(const Timestamp &rhs) const
Less-than operator.
Definition: Timestamp.cpp:295
time_t asTimeT() const
Definition: Timestamp.h:97
Timestamp & operator-=(const Timespan &rhs)
Subtraction of a Timespan.
Definition: Timestamp.cpp:224
Timespan elapsed() const
Returns the time elapsed since this timestamp.
Definition: Timestamp.cpp:121
~Timespan()
Destructor.
Definition: Timestamp.h:273
Timespan & operator+=(const Timespan &rhs)
Addition with a Timespan.
Definition: Timestamp.cpp:920
Timestamp & operator+=(const Timespan &rhs)
Addition with a Timespan.
Definition: Timestamp.cpp:211
virtual double asDouble() const
Accessor, convert to double.
Definition: Timestamp.cpp:826
static Timestamp FromMicros(const long long &micros)
Named Constructor, set from seconds << 20 + microseconds,.
Definition: Timestamp.cpp:71
void round(int seconds)
Definition: Timestamp.cpp:110
Timestamp & setToMonoTime()
Set to clock that represents monotonic time since some unspecified starting point.
Definition: Timestamp.cpp:348
bool operator>(const Timestamp &rhs) const
Greater-than operator.
Definition: Timestamp.cpp:289
Timespan class, represents a delta of time.
Definition: Timestamp.h:248
static Timespan FromMillis(const long long &millis)
Named Constructor, set from seconds << 10 + milliseconds >> 10.
Definition: Timestamp.cpp:820
const char * toString(char *buf, int buflen, int precision=3) const
Writes the Timestamp as to a unsigned char buffer.
Definition: Timestamp.cpp:418
static const Timestamp EPOCH_START_TIME
Constant value to represent beginning of (1970 Jan 1) epoch.
Definition: Timestamp.h:40
struct timeval & fromString(const char *timeString)
Set the internal storage to the specified ISO9601 string value.
Definition: Timestamp.cpp:692
static Timespan Minutes(int min)
Static allows creation of Timespans denominated in minutes.
Definition: Timestamp.h:313
void toTimespec(struct timespec *) const
Internal conversion function.
Definition: Timestamp.cpp:393
static struct timeval TimevalFromMillis(const long long &millis)
Returns a struct timeval from asMillis() result.
Definition: Timestamp.cpp:757
Timestamp operator-(const Timespan &rhs) const
Subtraction from Timespan.
Definition: Timestamp.cpp:250
static Timespan FromMicros(const long long &micros)
Named Constructor, set from seconds << 20 + microseconds,.
Definition: Timestamp.cpp:814
void addFromMicros(const long long &micros)
add seconds << 20 + microseconds
Definition: Timestamp.cpp:85
static struct timeval TimevalFromMicros(const long long &micros)
Returns a struct timeval from asMicros() result.
Definition: Timestamp.cpp:749
static const Timestamp NOT_SET_TIME
Constant value to represent "no time".
Definition: Timestamp.h:37
bool operator<=(const Timestamp &rhs) const
Greater-than-equal operator.
Definition: Timestamp.cpp:307
void sleepFor(void) const
Sleep for the timespan.
Definition: Timestamp.cpp:951
bool operator>=(const Timestamp &rhs) const
Less-than-equal operator.
Definition: Timestamp.cpp:301
virtual ~Timestamp()
Destructor.
Definition: Timestamp.h:78
bool operator!=(const Timestamp &rhs) const
Equalities/inequalities.
Definition: Timestamp.cpp:276
static Timestamp Now()
Static for current time for use on RHS.
Definition: Timestamp.cpp:362
virtual double asFloat() const
Accessor, convert to float.
Definition: Timestamp.cpp:133
Timestamp(const double &seconds)
Constructor, set time from double.
Definition: Timestamp.cpp:37
Replacement for standard template class string.
Definition: Str.h:12
static Timespan Seconds(double sec)
Static allows creation of Timespans denominated in seconds (as a float) This function originally took...
Definition: Timestamp.h:307
void sleepTill()
Sleep until the given time.
Definition: Timestamp.cpp:386
const Str toDateHourString() const
Writes the Timestamp as a date-hour Str (YYYYmmddHH).
Definition: Timestamp.cpp:464
const char * toString(char *buf, int buflen, int precision=3) const
Writes the Timespan to a unsigned char buffer.
Definition: Timestamp.cpp:963
struct timeval & fromDouble(const double &seconds)
Set the internal storage to the specified value.
Definition: Timestamp.cpp:472
virtual double asFloat() const
Accessor, convert to float.
Definition: Timestamp.cpp:836
virtual double asDouble() const
Accessor, convert to double.
Definition: Timestamp.cpp:127
void addFromMillis(const long long &millis)
add seconds << 10 + microseconds >> 10
Definition: Timestamp.cpp:94
Timestamp operator+(const Timespan &rhs) const
Addition to Timespan.
Definition: Timestamp.cpp:245
Timestamp & operator=(const Timestamp &rhs)
Copy operator.
Definition: Timestamp.cpp:176
static struct timeval TimevalFromIntegers(const int seconds, const unsigned int microseconds)
Returns a struct timeval from the specified values.
Definition: Timestamp.cpp:741
struct timeval & fromIntegers(const int seconds, const unsigned int microseconds)
Set the internal storage to the specified values.
Definition: Timestamp.cpp:765
void fromTimespec(const struct timespec *in)
Internal conversion function.
Definition: Timestamp.cpp:404
Timespan(const double &seconds)
Constructor, set time from double.
Definition: Timestamp.cpp:797
long long asMillis() const
Quickly returns the (seconds << 10) + (microseconds >> 10) (42 bits)
Definition: Timestamp.cpp:146
long long asMicros() const
Quickly returns the (seconds << 20) + microseconds (52 bits)
Definition: Timestamp.cpp:139
struct timeval timeval_
Internal storage for time.
Definition: Timestamp.h:212
Timespan & operator-=(const Timespan &rhs)
Subtraction of a Timespan.
Definition: Timestamp.cpp:933
static Timestamp FromMillis(const long long &millis)
Named Constructor, set from seconds << 10 + milliseconds >> 10.
Definition: Timestamp.cpp:103
static const Timespan INVALID_TIMESPAN
Constant value to represent an invalid timespan.
Definition: Timestamp.h:252
static Timespan Hours(int hour)
Static allows creation of Timespans denominated in hours.
Definition: Timestamp.h:319
Represents absolute times.
Definition: Timestamp.h:31
bool operator==(const Timestamp &rhs) const
Equalities/inequalities.
Definition: Timestamp.cpp:267
static Timespan Milliseconds(float msec)
Static allows creation of Timespans denominated in milliseconds.
Definition: Timestamp.h:298