stream  0.10.0
stream analysis framework
TimeStamp.h
1 #ifndef BASE_TIMESTAMP_H
2 #define BASE_TIMESTAMP_H
3 
4 
5 #include <cstdint>
6 
7 
8 namespace base {
9 
21  typedef uint64_t LocalStamp_t;
22 
23 
27  typedef double GlobalTime_t;
28 
29 
36  protected:
37  int64_t fT0;
38  uint64_t fWrapSize;
39  uint64_t fHalfWrapSize;
40  uint64_t fValueMask;
41 
42  uint64_t fCurrentWrap;
43  LocalStamp_t fRef;
44  int64_t fConvRef;
45 
46  double fCoef;
47 
48  public:
49 
52  fT0(0),
53  fWrapSize(2),
54  fHalfWrapSize(1),
55  fValueMask(1),
56  fCurrentWrap(0),
57  fRef(0),
58  fConvRef(0),
59  fCoef(1.)
60  {
61  }
62 
65 
67  void SetT0(int64_t t0) { fT0 = t0; }
68 
70  void SetTimeSystem(unsigned wrapbits, double coef)
71  {
72  fWrapSize = ((uint64_t) 1) << wrapbits;
74  fValueMask = fWrapSize - 1;
75  fCoef = coef;
76 
77  // TODO: should it be done here???
78  MoveRef(0);
79  }
80 
83  int64_t distance(LocalStamp_t t1, LocalStamp_t t2) const
84  {
85  uint64_t diff = (t2 - t1) & fValueMask;
86  return diff < fWrapSize/2 ? (int64_t) diff : (int64_t)diff - fWrapSize;
87  }
88 
90  uint64_t abs_distance(LocalStamp_t t1, LocalStamp_t t2)
91  {
92  uint64_t diff = (t2 - t1) & fValueMask;
93  return diff < fWrapSize/2 ? diff : fWrapSize - diff;
94  }
95 
98  double ToSeconds(LocalStamp_t stamp) const
99  {
100  // simplest way just return stamp*fCoef,
101  // but one should account all possible wraps
102 
103  // first estimate distance to the reference
104  // stamp could be left or right
105  int64_t dist = distance(fRef, stamp);
106 
107  // one we found distance, we could calculate full stamp as
108  // return (fCurrentWrap + fRef + dist - fT0) * fCoef;
109  return (fConvRef + dist) * fCoef;
110  }
111 
113  void MoveRef(LocalStamp_t newref)
114  {
115  // when new reference smaller than previous
116  // one should check if distance small that we could believe it is normal
117  if (newref < fRef) {
118  int64_t shift = distance(fRef, newref);
119  if ((shift>0) && (shift < (int64_t) fWrapSize/2))
121  }
122 
123  fRef = newref;
124 
125  // precalculate value, which will be used for conversion
126  fConvRef = int64_t(fCurrentWrap + fRef) - fT0;
127  }
128  };
129 
130 }
131 
132 #endif
LocalStampConverter class should perform conversion of time stamps to time in seconds.
Definition: TimeStamp.h:35
uint64_t fHalfWrapSize
! fWrapSize/2 - used very often in calculations
Definition: TimeStamp.h:39
LocalStamp_t fRef
! reference time, used to detect wraps of timestamp
Definition: TimeStamp.h:43
uint64_t fWrapSize
! value of time stamp which wraps - MUST be power of 2
Definition: TimeStamp.h:38
uint64_t fValueMask
! mask to extract bits related to stamp
Definition: TimeStamp.h:40
~LocalStampConverter()
destructor
Definition: TimeStamp.h:64
void SetTimeSystem(unsigned wrapbits, double coef)
Set major timing parameters - wrap value and coefficient.
Definition: TimeStamp.h:70
double fCoef
! time coefficient to convert to seconds
Definition: TimeStamp.h:46
LocalStampConverter()
constructor
Definition: TimeStamp.h:51
int64_t fT0
! time stamp, used as t0 for time production
Definition: TimeStamp.h:37
int64_t distance(LocalStamp_t t1, LocalStamp_t t2) const
Method calculates distance between two stamps In simplest case just should return t2-t1.
Definition: TimeStamp.h:83
void SetT0(int64_t t0)
set T0
Definition: TimeStamp.h:67
int64_t fConvRef
! value used for time conversion
Definition: TimeStamp.h:44
void MoveRef(LocalStamp_t newref)
Move reference to the new position.
Definition: TimeStamp.h:113
uint64_t fCurrentWrap
! summed wraps since begin
Definition: TimeStamp.h:42
double ToSeconds(LocalStamp_t stamp) const
Method convert time stamp to seconds, taking into account probable wrap relative to fRef value.
Definition: TimeStamp.h:98
uint64_t abs_distance(LocalStamp_t t1, LocalStamp_t t2)
Method returns abs(t1-t2)
Definition: TimeStamp.h:90