/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/TimeUtil.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/TimeUtil.hh Source File
Ratpac-two
TimeUtil.hh
1 #ifndef __RAT_TimeUtil__
2 #define __RAT_TimeUtil__
3 
4 #include <TTimeStamp.h>
5 
6 namespace RAT {
7 // I am going to type the wrong number of zeros if I don't do this
8 // and I want an integer, not a double
9 const long TIME_UTIL_BILLION = 1000000000L;
10 
11 // Return a new TTimeStamp offset by nanoseconds. nanoseconds can be negative
12 inline TTimeStamp AddNanoseconds(const TTimeStamp &a, const long nanoseconds) {
13  time_t new_sec = a.GetSec();
14  long new_nsec = a.GetNanoSec() + nanoseconds;
15  // Normalize
16  if (new_nsec < 0) {
17  new_sec -= 1 + (-new_nsec / TIME_UTIL_BILLION);
18  new_nsec = TIME_UTIL_BILLION - (-new_nsec % TIME_UTIL_BILLION);
19  } else if (new_nsec >= TIME_UTIL_BILLION) {
20  new_sec += new_nsec / TIME_UTIL_BILLION;
21  new_nsec = new_nsec % TIME_UTIL_BILLION;
22  }
23  return TTimeStamp(new_sec, new_nsec);
24 }
25 
26 // Get time difference (a - b) in nanoseconds. Note that maximum deltaT is
27 // +/-2^63 = 292 years
28 inline long TimeDifference(const TTimeStamp &a, const TTimeStamp &b) {
29  return (a.GetSec() - b.GetSec()) * TIME_UTIL_BILLION + (a.GetNanoSec() - b.GetNanoSec());
30 }
31 } // namespace RAT
32 
33 #endif
Definition: CCCrossSecMessenger.hh:29