Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JDAQUTCExtended.hh
Go to the documentation of this file.
1#ifndef __JDAQUTCEXTENDED__
2#define __JDAQUTCEXTENDED__
3
4/**
5 * \author rbruijn
6 */
7
8#include <cstdint>
9#include <istream>
10#include <ostream>
11#include <iomanip>
12#include <limits>
13
15
16
17namespace KM3NETDAQ {
18
19 /**
20 * Data structure for UTC time.
21 */
23 {
24 public:
25
28 friend JWriter& operator<<(JWriter&, const JDAQUTCExtended&);
29
30
31 /**
32 * Default constructor.
33 */
38
39
40 /**
41 * Constructor.
42 *
43 * \param seconds seconds [s]
44 * \param cycles cycles [16 ns]
45 */
46 JDAQUTCExtended(const uint32_t seconds,
47 const uint32_t cycles):
48 UTC_seconds(seconds),
50 {}
51
52
53 /**
54 * Constructor.
55 *
56 * \param nanoseconds time [ns]
57 */
58 JDAQUTCExtended(const double nanoseconds)
59 {
60 setTimeNanoSecond(nanoseconds);
61 }
62
63
64 /**
65 * Virtual destructor.
66 */
68 {}
69
70
71 /**
72 * Get White Rabbit status.
73 *
74 * \return true if okay; else false
75 */
76 bool getWRStatus() const
77 {
78 return (UTC_seconds & ~getMask()) != 0;
79 }
80
81
82 /**
83 * Get major time.
84 *
85 * \return time [s]
86 */
87 uint32_t getUTCseconds() const
88 {
89 return (UTC_seconds & getMask());
90 }
91
92
93 /**
94 * Get minor time.
95 *
96 * \return time [16 ns]
97 */
98 uint32_t getUTC16nanosecondcycles() const
99 {
101 }
102
103
104 /**
105 * Get time (limited to 16 ns cycles).
106 *
107 * \return time [ns]
108 */
109 double getTimeNanoSecond() const
110 {
111 return getUTCseconds() * 1.0e9 + getUTC16nanosecondcycles() * getTick();
112 }
113
114
115 /**
116 * Set time.
117 *
118 * \param utc_ns time [ns]
119 */
120 void setTimeNanoSecond(const double utc_ns)
121 {
122 UTC_seconds = (uint32_t) ( utc_ns * 1.0e-9);
123 UTC_16nanosecondcycles = (uint32_t) ((utc_ns - UTC_seconds*1.0e9) / getTick());
124 }
125
126
127 /**
128 * Add time.
129 *
130 * \param t_ns time [ns]
131 */
132 void addTimeNanoSecond(const double t_ns)
133 {
134 const double x_ns = (double) getUTC16nanosecondcycles() * (double) getTick() + t_ns;
135 const uint32_t t_s = (uint32_t) (x_ns * 1.0e-9);
136
137 UTC_seconds += t_s;
138 UTC_16nanosecondcycles = (uint32_t) ((x_ns - t_s*1.0e9) / getTick());
139 }
140
141
142 /**
143 * Get minimum possible value.
144 *
145 * \return minimum possible value
146 */
148 {
149 return JDAQUTCExtended(0,0);
150 }
151
152
153 /**
154 * Get maximum possible value.
155 *
156 * \return maximum possible value
157 */
159 {
160 return JDAQUTCExtended(std::numeric_limits<uint32_t>::max(),
161 std::numeric_limits<uint32_t>::max());
162 }
163
164
165 /**
166 * Get mask for seconds data.
167 *
168 * \return mask
169 */
170 static uint32_t getMask()
171 {
172 return 0x7FFFFFFF;
173 }
174
175
176 /**
177 * Get number of nano-seconds per tick.
178 *
179 * \return time [ns]
180 */
181 static double getTick()
182 {
183 return 16.0;
184 }
185
186
187 /**
188 * Get arbitrary offset (e.g. for accuracy of time differences).
189 *
190 * \return UTC time
191 */
193 {
194 static JDAQUTCExtended utc(1600000000, 0);
195
196 return utc;
197 }
198
199
200 /**
201 * Read UTC time.
202 *
203 * \param in intput stream
204 * \param utc UTC extended time
205 * \return intput stream
206 */
207 friend inline std::istream& operator>>(std::istream& in, JDAQUTCExtended& utc)
208 {
209 in >> utc.UTC_seconds;
210 in.get();
211 in >> utc.UTC_16nanosecondcycles;
212
213 return in;
214 }
215
216
217 /**
218 * Write UTC time.
219 *
220 * \param out output stream
221 * \param utc UTC extended time
222 * \return output stream
223 */
224 friend inline std::ostream& operator<<(std::ostream& out, const JDAQUTCExtended& utc)
225 {
226 using namespace std;
227
228 const char c = out.fill();
229
230 out << setw(10) << utc.getUTCseconds();
231 out << ':';
232 out << setw(10) << setfill('0') << utc.getUTC16nanosecondcycles() << setfill(c);
233
234 return out;
235 }
236
237
239
240
241 protected:
242 uint32_t UTC_seconds;
244 };
245
246
247 /**
248 * Less than operator for UTC times.
249 *
250 * \param first UTC time
251 * \param second UTC time
252 * \result true if first UTC time earlier than second UTC time; else false
253 */
254 inline bool operator<(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
255 {
256 if (first.getUTCseconds() == second.getUTCseconds())
257 return first.getUTC16nanosecondcycles() < second.getUTC16nanosecondcycles();
258 else
259 return first.getUTCseconds() < second.getUTCseconds();
260 }
261
262
263 /**
264 * Greater than operator for UTC times.
265 *
266 * \param first UTC time
267 * \param second UTC time
268 * \result true if first UTC time later than second UTC time; else false
269 */
270 inline bool operator>(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
271 {
272 return (second < first);
273 }
274
275
276 /**
277 * Less than or equal operator for UTC times.
278 *
279 * \param first UTC time
280 * \param second UTC time
281 * \result true if first UTC time earlier than second UTC time; else false
282 */
283 inline bool operator<=(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
284 {
285 return !(second < first);
286 }
287
288
289 /**
290 * Greater than or equal operator for UTC times.
291 *
292 * \param first UTC time
293 * \param second UTC time
294 * \result true if first UTC time earlier than second UTC time; else false
295 */
296 inline bool operator>=(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
297 {
298 return !(first < second);
299 }
300
301
302 /**
303 * Equal operator for UTC times.
304 *
305 * \param first UTC time
306 * \param second UTC time
307 * \result true if first UTC time equal second UTC time; else false
308 */
309 inline bool operator==(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
310 {
311 return (first.getUTCseconds() == second.getUTCseconds() &&
313 }
314
315
316 /**
317 * Not equal operator for UTC times.
318 *
319 * \param first UTC time
320 * \param second UTC time
321 * \result true if first UTC time not equal second UTC time; else false
322 */
323 inline bool operator!=(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
324 {
325 return !(first == second);
326 }
327
328
329 /**
330 * Get time difference between two UTC times.
331 *
332 * \param first UTC time
333 * \param second UTC time
334 * \result time difference [s]
335 */
336 inline double getTimeDifference(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
337 {
338 const double utc_s = ((double) second.getUTCseconds() - (double) first.getUTCseconds());
339 const double utc_ns = ((double) second.getUTC16nanosecondcycles() - (double) first.getUTC16nanosecondcycles()) * JDAQUTCExtended::getTick();
340
341 return utc_s + utc_ns*1.0e-9;
342 }
343}
344
345#endif
Interface for binary input.
Interface for binary output.
Data structure for UTC time.
void addTimeNanoSecond(const double t_ns)
Add time.
static uint32_t getMask()
Get mask for seconds data.
uint32_t getUTC16nanosecondcycles() const
Get minor time.
friend std::istream & operator>>(std::istream &in, JDAQUTCExtended &utc)
Read UTC time.
static JDAQUTCExtended max()
Get maximum possible value.
friend JWriter & operator<<(JWriter &, const JDAQUTCExtended &)
Write UTC to output.
void setTimeNanoSecond(const double utc_ns)
Set time.
virtual ~JDAQUTCExtended()
Virtual destructor.
bool getWRStatus() const
Get White Rabbit status.
JDAQUTCExtended(const uint32_t seconds, const uint32_t cycles)
Constructor.
friend JReader & operator>>(JReader &, JDAQUTCExtended &)
Read UTC from input.
static double getTick()
Get number of nano-seconds per tick.
friend std::ostream & operator<<(std::ostream &out, const JDAQUTCExtended &utc)
Write UTC time.
static JDAQUTCExtended min()
Get minimum possible value.
uint32_t getUTCseconds() const
Get major time.
static const JDAQUTCExtended & getInstance()
Get arbitrary offset (e.g.
double getTimeNanoSecond() const
Get time (limited to 16 ns cycles).
JDAQUTCExtended(const double nanoseconds)
Constructor.
ClassDef(JDAQUTCExtended, 1)
JDAQUTCExtended()
Default constructor.
bool operator==(const Head &first, const Head &second)
Equal operator.
Definition JHead.hh:1801
bool operator<(const Head &first, const Head &second)
Less than operator.
Definition JHead.hh:1817
bool operator!=(const JTag &first, const JTag &second)
Not equal operator for JTag.
Definition JTag.hh:291
KM3NeT DAQ data structures and auxiliaries.
Definition DataQueue.cc:39
bool operator>=(const JDAQUTCExtended &first, const JDAQUTCExtended &second)
Greater than or equal operator for UTC times.
size_t getSizeof< JDAQUTCExtended >()
Get size of type.
double getTimeDifference(const JDAQChronometer &first, const JDAQChronometer &second)
Get time difference between two chronometers.
bool operator<=(const JDAQUTCExtended &first, const JDAQUTCExtended &second)
Less than or equal operator for UTC times.
bool operator>(const JDAQUTCExtended &first, const JDAQUTCExtended &second)
Greater than operator for UTC times.