CARMA C++
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 namespace sza {
16  namespace util {
17 
53  class TimeVal {
54  public:
55 
59  TimeVal();
60 
75  TimeVal(unsigned long seconds, unsigned long microSeconds,
76  unsigned long nanoSeconds);
77 
81  TimeVal(unsigned long seconds, unsigned long nanoSeconds);
82 
86  TimeVal(const struct timeval& tVal);
87 
91  TimeVal(const struct timespec& timeSpec);
92 
96  TimeVal(double mjd);
97 
98  //------------------------------------------------------------
99  // Methods for seting the time.
100  //------------------------------------------------------------
101 
102 #define HAVE_RT 1
103 
104 #if HAVE_RT
105 #else
106  enum clockid_t {
107  CLOCK_REALTIME,
108  CLOCK_HIGHRES
109  };
110 #endif
111 
122  void setToCurrentTime(clockid_t clock = CLOCK_REALTIME);
123 
127  void setMjd(unsigned long days, unsigned long seconds,
128  unsigned long nanoSeconds);
129 
133  void setMjd(unsigned long days, unsigned long milliSeconds);
134 
138  void setMjd(double mjd);
139 
144  void setTime(unsigned long seconds, unsigned long microSeconds,
145  unsigned long nanoSeconds);
146 
150  void setTime(unsigned long seconds, unsigned long nanoSeconds);
151 
155  void setTime(const struct timespec& timeSpec);
156 
160  void setTime(const struct timeval& tVal);
161 
162  //------------------------------------------------------------
163  // Methods to modify the time.
164  //------------------------------------------------------------
165 
169  void incrementSeconds(double seconds);
170 
174  void incrementNanoSeconds(unsigned nanoSeconds);
175 
179  void reset();
180 
184  struct timeval* timeVal();
185 
189  struct timespec* timeSpec();
190 
194  const TimeVal operator+(const TimeVal& tVal);
195 
199  const TimeVal operator-(const TimeVal& tVal);
200 
201  //------------------------------------------------------------
202  // Methods returning the time.
203  //------------------------------------------------------------
204 
208  double getTimeInMjdDays();
209 
213  double getTimeInSeconds();
214 
218  double getTimeInMilliSeconds();
219 
223  double getTimeInMicroSeconds();
224 
228  unsigned long getTimeInNanoSeconds();
229 
230  //------------------------------------------------------------
231  // Methods returning parts of the time.
232  //------------------------------------------------------------
233 
238 
242  unsigned long getSeconds();
243 
247  unsigned long getMicroSeconds();
248 
252  unsigned long getNanoSeconds();
253 
257  unsigned long getMjdDays();
258 
262  unsigned long getMjdSeconds();
263 
267  unsigned long getMjdMilliSeconds();
268 
272  unsigned long getMjdNanoSeconds();
273 
277  double getMjd();
278 
282  double getFractionalMjdDay();
283 
289  unsigned int getMjdId(unsigned nanoSecondInterval);
290 
291  //------------------------------------------------------------
292  // When our timespec struct is used as a countdown timer, it
293  // can be modified to reflect the elapsed time. The following
294  // methods return the elapsed time in different
295  // representations.
296  //------------------------------------------------------------
297 
303  double getElapsedTimeInSeconds();
304 
309 
313  unsigned long getElapsedTimeInNanoSeconds();
314 
315 
316  //------------------------------------------------------------
317  // Print methods
318  //------------------------------------------------------------
319 
320  std::string getUtcString();
321 
322  std::string dateString();
323 
327  friend std::ostream& operator<<(std::ostream& os, TimeVal& tVal);
328 
329  private:
330 
331  // The following are all equivalent representations of a
332  // single time stamp.
333 
334  // timeval stores a time as seconds and microseconds.
335 
336  struct timeval timeVal_;
337 
338  // timespec stores a time as seconds and nanoseconds.
339 
340  struct timespec timeSpec_;
341 
342  // A generic representation of time as seconds and nanoseconds.
343 
344  unsigned long seconds_;
345  unsigned long nanoSeconds_;
346 
347  // An equivalent representation of time as MJD day number,
348  // seconds and nanoseconds.
349 
350  unsigned long mjdDays_;
351  unsigned long mjdSeconds_;
352  unsigned long mjdNanoSeconds_;
353 
354  //------------------------------------------------------------
355  // Methods to update all represenations of time.
356  //------------------------------------------------------------
357 
358  public:
359 
363  void setSeconds(unsigned long seconds);
364 
372  void setMicroSeconds(unsigned long microSeconds);
373 
374  private:
375 
382  void setNanoSeconds(unsigned long nanoSeconds);
383 
384  }; // End class TimeVal
385  }; // End namespace util
386 }; // End namespace sza
387 
388 #endif // End #ifndef
void setToCurrentTime(clockid_t clock=CLOCK_REALTIME)
Fill this structure with the current time from the specified clock.
double getFractionalMjdDay()
Return the feactional part only of the mjd day as a double.
double getTimeInSeconds()
Return the time in our timeval struct as fractional seconds.
A class for managing timeval/timespec structs.
Definition: TimeVal.h:53
const TimeVal operator-(const TimeVal &tVal)
Subtract two TimeVal objects.
unsigned long getMjdDays()
Return the mjd day number corresponding to this time.
void reset()
Reset our internal timeval struct to stored values.
friend std::ostream & operator<<(std::ostream &os, TimeVal &tVal)
Allows cout &lt;&lt; timeVal.
unsigned long getSeconds()
Return just the integer seconds.
unsigned long getMjdMilliSeconds()
Return the mjd milliseconds corresponding to this time.
double getTimeInMicroSeconds()
Return the total time in microseconds.
unsigned int getMjdId(unsigned nanoSecondInterval)
Get a unique identifier based on the MJD.
unsigned long getMjdSeconds()
Return the mjd seconds corresponding to this time.
struct timeval * timeVal()
Return a pointer to our internal timeval struct.
double getFractionalTimeInSeconds()
Return just the fractional seconds, as a double.
double getElapsedTimeInMicroSeconds()
Return the time in our timeVal struct as integer microseconds.
void setMicroSeconds(unsigned long microSeconds)
Set the microseconds in this struct.
unsigned long getMicroSeconds()
Return just the integer micro seconds.
void setTime(unsigned long seconds, unsigned long microSeconds, unsigned long nanoSeconds)
Set the time.
void incrementNanoSeconds(unsigned nanoSeconds)
Increment the time by nanoseconds.
const TimeVal operator+(const TimeVal &tVal)
Add two TimeVal objects.
unsigned long getTimeInNanoSeconds()
Return the total time as integer nanoseconds.
struct timespec * timeSpec()
Return a pointer to our internal timespec struct.
void incrementSeconds(double seconds)
Increment the time by fractional seconds.
double getElapsedTimeInSeconds()
Return the time elapsed as fractional seconds.
TimeVal()
Constructors with no initialization.
double getTimeInMilliSeconds()
Return the total time in milliseconds.
unsigned long getElapsedTimeInNanoSeconds()
Return the time in our timeVal struct as integer nanoseconds.
double getMjd()
Return the complete mjd day as a double.
void setMjd(unsigned long days, unsigned long seconds, unsigned long nanoSeconds)
Set the time, as an MJD.
unsigned long getMjdNanoSeconds()
Return the mjd nanoseconds corresponding to this time.
double getTimeInMjdDays()
Return the total time in MJD days.
void setSeconds(unsigned long seconds)
Set the seconds in this object.
unsigned long getNanoSeconds()
Return just the integer nano seconds.