My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
TimeVal.h
Go to the documentation of this file.
1 #ifndef TIMEVAL_H
2 #define TIMEVAL_H
3 
11 #include <time.h>
12 #include <sys/time.h>
13 #include <iostream>
14 
15 #include "gcp/util/common/Directives.h"
16 
17 namespace gcp {
18  namespace util {
19 
55  class TimeVal {
56  public:
57 
61  TimeVal();
62 
75  TimeVal(unsigned int seconds, unsigned int microSeconds,
76  unsigned int nanoSeconds);
77 
81  TimeVal(unsigned int seconds, unsigned int nanoSeconds);
82 
86  TimeVal(double seconds);
87 
95  TimeVal(std::string time_string, std::string format,
96  struct tm& refdate, double& reftime);
97  /* counter to keep track of the number of errors*/
98  int ErrorCounter_;
105  TimeVal(std::string time_string, std::string format);
106 
110  TimeVal(const struct timeval& tVal);
111 
115  TimeVal(const struct timespec& timeSpec);
116  TimeVal(const TimeVal& tVal);
117  TimeVal(TimeVal& tVal);
118 
119  //------------------------------------------------------------
120  // Methods for seting the time.
121  //------------------------------------------------------------
122 
123 #if DIR_HAVE_RT
124 #else
125  enum clockid_t {
126  CLOCK_REALTIME,
127  CLOCK_HIGHRES
128  };
129 #endif
130 
141  void setToCurrentTime(clockid_t clock = CLOCK_REALTIME);
142 
146  void setMjd(unsigned int days, unsigned int seconds,
147  unsigned int nanoSeconds);
148 
152  void setMjd(unsigned int days, unsigned int milliSeconds);
153 
157  void setMjd(double mjd);
158 
163  void setTime(unsigned int seconds, unsigned int microSeconds,
164  unsigned int nanoSeconds);
165 
169  void setTime(unsigned int seconds, unsigned int nanoSeconds);
170 
174  void setTime(const struct timespec& timeSpec);
175 
179  void setTime(const struct timeval& tVal);
183  void setTime(double seconds);
184 
185  void setTimeInMilliSeconds(unsigned int milliSeconds);
186 
187  //------------------------------------------------------------
188  // Methods to modify the time.
189  //------------------------------------------------------------
190 
194  void incrementSeconds(double seconds);
195 
199  void incrementMilliSeconds(unsigned milliSeconds);
200 
204  void incrementNanoSeconds(unsigned nanoSeconds);
205 
209  void reset();
210 
214  struct timeval* timeVal();
215 
219  struct timespec* timeSpec();
220 
224  const TimeVal operator+(const TimeVal& tVal);
225 
229  const TimeVal operator-(const TimeVal& tVal);
233  void operator=(const TimeVal& tVal);
234  void operator=(TimeVal& tVal);
235  bool operator>=(TimeVal& tVal);
236  bool operator>=(const TimeVal& tVal);
237  bool operator<(TimeVal& tVal);
238  bool operator<(const TimeVal& tVal);
239  void operator-=(const TimeVal& tVal);
240 
241  //------------------------------------------------------------
242  // Methods returning the time.
243  //------------------------------------------------------------
244 
248  double getTimeInMjdDays();
249 
253  double getTimeInSeconds() const;
254 
258  unsigned int getTimeInMicroSeconds();
259 
263  unsigned int getTimeInMilliSeconds();
264 
268  unsigned int getTimeInNanoSeconds();
269 
270  //------------------------------------------------------------
271  // Methods returning parts of the time.
272  //------------------------------------------------------------
273 
278 
282  unsigned int getSeconds();
283 
287  unsigned int getMilliSeconds(bool round=false);
288 
292  unsigned int getMicroSeconds();
293 
297  unsigned int getNanoSeconds();
298 
302  unsigned int getMjdDays();
303 
307  unsigned int getMjdSeconds();
308 
312  unsigned int getMjdMilliSeconds(bool round=false);
313 
317  unsigned int getMjdNanoSeconds();
318 
322  double getMjd();
323 
327  unsigned int getMjdId(unsigned nanoSecondInterval);
328  // was int <<--
329  //------------------------------------------------------------
330  // When our timespec struct is used as a countdown timer, it
331  // can be modified to reflect the elapsed time. The following
332  // methods return the elapsed time in different
333  // representations.
334  //------------------------------------------------------------
335 
341  double getElapsedTimeInSeconds();
342 
347 
351  unsigned int getElapsedTimeInNanoSeconds();
352 
353 
354  //------------------------------------------------------------
355  // Print methods
356  //------------------------------------------------------------
357 
358  std::string getUtcString();
359 
360  std::string dateString();
361 
365  friend std::ostream& operator<<(std::ostream& os, TimeVal& tVal);
366 
367  private:
368 
369  // The following are all equivalent representations of a
370  // single time stamp.
371 
372  // timeval stores a time as seconds and microseconds.
373 
374  struct timeval timeVal_;
375 
376  // timespec stores a time as seconds and nanoseconds.
377 
378  struct timespec timeSpec_;
379 
380  // A generic representation of time as seconds and nanoseconds.
381 
382  unsigned int seconds_;
383  unsigned int nanoSeconds_;
384 
385  // An equivalent representation of time as MJD day number,
386  // seconds and nanoseconds.
387 
388  unsigned int mjdDays_;
389  unsigned int mjdSeconds_;
390  unsigned int mjdNanoSeconds_;
391 
392  public:
393 
394  //------------------------------------------------------------
395  // Methods to update all represenations of time.
396  //------------------------------------------------------------
400  void setDateTime(std::string time_string, std::string format, struct tm& refdate, double& reftime);
401 
405  void setSeconds(unsigned int seconds);
406 
413  void setMilliSeconds(unsigned int milliSeconds);
414 
421  void setMicroSeconds(unsigned int microSeconds);
422 
429  void setNanoSeconds(unsigned int nanoSeconds);
430 
431  }; // End class TimeVal
432 
433  } // End namespace util
434 } // End namespace gcp
435 
436 
437 #endif // End #ifndef
bool operator>=(TimeVal &tVal)
Definition: TimeVal.cc:571
double getFractionalTimeInSeconds()
Definition: TimeVal.cc:628
void setMjd(unsigned int days, unsigned int seconds, unsigned int nanoSeconds)
Definition: TimeVal.cc:818
void operator=(const TimeVal &tVal)
Definition: TimeVal.cc:518
unsigned int getTimeInNanoSeconds()
Definition: TimeVal.cc:620
void setMilliSeconds(unsigned int milliSeconds)
Definition: TimeVal.cc:324
void incrementMilliSeconds(unsigned milliSeconds)
Definition: TimeVal.cc:406
unsigned int getTimeInMicroSeconds()
Definition: TimeVal.cc:597
unsigned int getMicroSeconds()
Definition: TimeVal.cc:659
void incrementSeconds(double seconds)
Definition: TimeVal.cc:358
void reset()
Definition: TimeVal.cc:415
void setToCurrentTime(clockid_t clock=CLOCK_REALTIME)
Definition: TimeVal.cc:710
unsigned int getElapsedTimeInNanoSeconds()
Definition: TimeVal.cc:699
double getElapsedTimeInMicroSeconds()
Definition: TimeVal.cc:688
void operator-=(const TimeVal &tVal)
Definition: TimeVal.cc:494
unsigned int getTimeInMilliSeconds()
Definition: TimeVal.cc:605
void incrementNanoSeconds(unsigned nanoSeconds)
Definition: TimeVal.cc:380
unsigned int getNanoSeconds()
Definition: TimeVal.cc:667
double getMjd()
Definition: TimeVal.cc:786
double getTimeInMjdDays()
Definition: TimeVal.cc:579
friend std::ostream & operator<<(std::ostream &os, TimeVal &tVal)
TimeVal()
Definition: TimeVal.cc:78
const TimeVal operator+(const TimeVal &tVal)
Definition: TimeVal.cc:446
unsigned int getSeconds()
Definition: TimeVal.cc:636
unsigned int getMilliSeconds(bool round=false)
Definition: TimeVal.cc:645
void setDateTime(std::string time_string, std::string format, struct tm &refdate, double &reftime)
Definition: TimeVal.cc:161
unsigned int getMjdMilliSeconds(bool round=false)
Definition: TimeVal.cc:765
struct timespec * timeSpec()
Definition: TimeVal.cc:435
Definition: TimeVal.h:55
unsigned int getMjdNanoSeconds()
Definition: TimeVal.cc:778
double getTimeInSeconds() const
Definition: TimeVal.cc:589
void setTime(unsigned int seconds, unsigned int microSeconds, unsigned int nanoSeconds)
Definition: TimeVal.cc:237
const TimeVal operator-(const TimeVal &tVal)
Definition: TimeVal.cc:466
std::string getUtcString()
Definition: TimeVal.cc:861
void setMicroSeconds(unsigned int microSeconds)
Definition: TimeVal.cc:332
void setNanoSeconds(unsigned int nanoSeconds)
Definition: TimeVal.cc:340
unsigned int getMjdDays()
Definition: TimeVal.cc:749
bool operator<(TimeVal &tVal)
Definition: TimeVal.cc:555
struct timeval * timeVal()
Definition: TimeVal.cc:427
void setSeconds(unsigned int seconds)
Definition: TimeVal.cc:308
double getElapsedTimeInSeconds()
Definition: TimeVal.cc:677
unsigned int getMjdSeconds()
Definition: TimeVal.cc:757
unsigned int getMjdId(unsigned nanoSecondInterval)
Definition: TimeVal.cc:847
void setTimeInMilliSeconds(unsigned int milliSeconds)
Definition: TimeVal.cc:297