TimeStamp.h
Go to the documentation of this file.
1 #ifndef _utl_TimeStamp_h_
2 #define _utl_TimeStamp_h_
3 
4 #include <string>
5 #include <ostream>
6 
7 #include <utl/AugerUnits.h>
8 
9 
10 struct tm;
11 
12 namespace utl {
13 
14  class TimeInterval;
15 
16 
110  class TimeStamp {
111 
112  public:
114 
116  TimeStamp(const long sec, const double nsec = 0)
117  { SetNormalized(sec, nsec); }
118 
120  void SetGPSTime(const unsigned long sec, const double nsec = 0)
121  { SetNormalized(sec, nsec); }
122 
124  unsigned long GetGPSSecond() const { return fGPSSecond; }
125 
127  double GetGPSNanoSecond() const { return fGPSNanoSecond; }
128 
129  // ---------------------
130  // Arithmetic operations
131  // ---------------------
132 
134  TimeStamp operator+(const TimeInterval& ti) const;
135 
137  TimeStamp& operator+=(const TimeInterval& ti);
138 
140  TimeInterval operator-(const TimeStamp& ts) const;
141 
143  TimeStamp operator-(const TimeInterval& ti) const;
144 
146  TimeStamp& operator-=(const TimeInterval& ti);
147 
148  // ------------------
149  // Boolean operations
150  // ------------------
151 
152  bool operator==(const TimeStamp& ts) const
153  { return (fGPSSecond == ts.fGPSSecond &&
155 
156  bool operator!=(const TimeStamp& ts) const
157  { return !operator==(ts); }
158 
159  bool operator>(const TimeStamp& ts) const
160  { return (fGPSSecond > ts.fGPSSecond ||
161  (fGPSSecond == ts.fGPSSecond &&
163 
164  bool operator>=(const TimeStamp& ts) const
165  { return !operator<(ts); }
166 
167  bool operator<(const TimeStamp& ts) const
168  { return (fGPSSecond < ts.fGPSSecond ||
169  (fGPSSecond == ts.fGPSSecond &&
171 
172  bool operator<=(const TimeStamp& ts) const
173  { return !operator>(ts); }
174 
175  explicit operator bool() const { return fGPSSecond || fGPSNanoSecond; }
176 
177  static TimeStamp Max() { return TimeStamp(2147483647UL, 999999999.9999999); }
178 
179  private:
180  // with this we will have a y2k problem in approx. 136 years -
181  // or for some operations in 68 years - from 1980 (GPS epoch)
182  unsigned long fGPSSecond;
184 
186  void SetNormalized(long sec, double nsec);
187 
188  };
189 
190 
193 
194 
195  std::ostream& operator<<(std::ostream& os, const TimeStamp& ts);
196 
197 }
198 
199 
200 // the following has to be done in this manner in order to keep
201 // some important operators inline
202 
203 #include <utl/AugerUnits.h>
204 #include <utl/TimeInterval.h>
205 
206 namespace utl {
207 
208  inline
209  TimeStamp
211  const
212  {
213  // proper normalization is done by the TimeStamp ctor
214  return
215  TimeStamp(long(fGPSSecond + ti.GetSecond()),
217  }
218 
219 
220  inline
221  TimeStamp&
223  {
226  return *this;
227  }
228 
229 
230  inline
233  const
234  {
235  return
236  TimeInterval(
237  (long(fGPSSecond) - long(ts.fGPSSecond)) * second +
239  );
240  }
241 
242 
243  inline
244  TimeStamp
246  const
247  {
248  // proper normalization is done by the TimeStamp ctor
249  return
250  TimeStamp(long(fGPSSecond) - long(ti.GetSecond()),
252  }
253 
254 
255  inline
256  TimeStamp&
258  {
259  SetNormalized(long(fGPSSecond) - long(ti.GetSecond()),
261  return *this;
262  }
263 
264 }
265 
266 
267 #endif
static TimeStamp Max()
Definition: TimeStamp.h:177
constexpr double second
Definition: AugerUnits.h:145
unsigned long fGPSSecond
Definition: TimeStamp.h:182
TimeInterval operator-(const TimeStamp &ts) const
[TimeStamp] - [TimeStamp] = [TimeInterval]
Definition: TimeStamp.h:232
TimeStamp & operator-=(const TimeInterval &ti)
[TimeStamp] -= [TimeInterval] = [TimeStamp]
Definition: TimeStamp.h:257
long GetSecond() const
Get the seconds floor for the interval.
Definition: TimeInterval.cc:13
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Stream & operator<<(Stream &s, MessageLoggerConfig &mlc)
Applies the configuration to the given stream.
void SetGPSTime(const unsigned long sec, const double nsec=0)
Set GPS second and (optionally) nanosecond.
Definition: TimeStamp.h:120
bool operator>(const TimeStamp &ts) const
Definition: TimeStamp.h:159
constexpr double nanosecond
Definition: AugerUnits.h:143
bool operator<=(const TimeStamp &ts) const
Definition: TimeStamp.h:172
void SetNormalized(long sec, double nsec)
Take care of positive nanosecond and GPS epoch.
Definition: TimeStamp.cc:15
bool operator!=(const TimeStamp &ts) const
Definition: TimeStamp.h:156
double fGPSNanoSecond
Definition: TimeStamp.h:183
bool operator==(const TimeStamp &ts) const
Definition: TimeStamp.h:152
bool operator>=(const TimeStamp &ts) const
Definition: TimeStamp.h:164
A TimeInterval is used to represent time elapsed between two events.
Definition: TimeInterval.h:43
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
double GetGPSNanoSecond() const
GPS nanosecond.
Definition: TimeStamp.h:127
TimeStamp GetCurrentSystemTime()
get current time as reported by system
Definition: TimeStamp.cc:46
bool operator<(const TimeStamp &ts) const
Definition: TimeStamp.h:167
TimeStamp operator+(const TimeInterval &ti) const
[TimeStamp] + [TimeInterval] = [TimeStamp]
Definition: TimeStamp.h:210
double GetNanoSecond() const
Get integer number of nanoseconds past seconds boundary.
Definition: TimeInterval.cc:25
TimeStamp & operator+=(const TimeInterval &ti)
[TimeStamp] += [TimeInterval] = [TimeStamp]
Definition: TimeStamp.h:222
TimeStamp(const long sec, const double nsec=0)
Construct from GPS second and nanosecond.
Definition: TimeStamp.h:116

, generated on Tue Sep 26 2023.