TimingUtilitiesTests.cpp
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <NovaTimingUtilities/TimingUtilities.h>
3 
4 namespace NDTU = novadaq::timeutils;
5 
6 class TimingUtilitiesTests : public CppUnit::TestFixture
7 {
14 
15 public:
16  void setUp();
17  void tearDown();
18 
19  void testBasicFunctions();
21  void testTMStruct();
22  void testLeapSeconds();
23 
24 private:
25 
26 };
27 
28 void
30 {
31  // flush stdout to get progress markers to display in a timely way
32  std::cout.flush();
33 }
34 
35 void
37 {
38  // flush stdout to get progress markers to display in a timely way
39  std::cout.flush();
40 }
41 
42 void
44 {
45  struct timeval unixTime;
46  uint64_t novaTime;
47 
48  // test the conversion of NOvA time to UNIX time at boundary conditions
49  novaTime = 0;
50  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime, unixTime));
51  CPPUNIT_ASSERT(unixTime.tv_sec == 1262304000);
52  CPPUNIT_ASSERT(unixTime.tv_usec == 0);
53  novaTime = 64000000;
54  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime, unixTime));
55  CPPUNIT_ASSERT(unixTime.tv_sec == (1 + 1262304000));
56  CPPUNIT_ASSERT(unixTime.tv_usec == 0);
57 
58  // test a round trip conversion, novaTime -> unixTime -> novaTime
59  // (Note that the starting time needs to be something that can be
60  // accurately represented in both units)
61  uint64_t sampleNovaTime = (uint64_t)((double) 64000000 * 123456789.5);
62  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(sampleNovaTime, unixTime));
63  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTime, novaTime));
64  CPPUNIT_ASSERT(novaTime == sampleNovaTime);
65 
66  // test the fetching of the current NOvA time
67  gettimeofday(&unixTime, 0);
68  uint64_t beforeTime;
69  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTime, beforeTime));
70  uint64_t currentNovaTime;
71  CPPUNIT_ASSERT(NDTU::getCurrentNovaTime(currentNovaTime));
72  gettimeofday(&unixTime, 0);
73  uint64_t afterTime;
74  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTime, afterTime));
75  CPPUNIT_ASSERT(beforeTime <= currentNovaTime);
76  CPPUNIT_ASSERT(currentNovaTime <= afterTime);
77 }
78 
79 void
81 {
82  uint16_t sec = 0;
83  uint16_t min = 0;
84  uint16_t hour = 0;
85  uint16_t mday = 1;
86  uint16_t mon = 0;
87  uint32_t year = 110;
88  uint64_t novaTime;
89  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(sec,min,hour,mday,mon,year, novaTime));
90  CPPUNIT_ASSERT(novaTime == 0);
91 
92  uint16_t osec, omin, ohour, omday, omon;
93  uint32_t oyear;
94  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,osec,omin,ohour,omday,omon,oyear));
95  CPPUNIT_ASSERT(sec == osec);
96  CPPUNIT_ASSERT(min == omin);
97  CPPUNIT_ASSERT(hour == ohour);
98  CPPUNIT_ASSERT(mday == omday);
99  CPPUNIT_ASSERT(mon == omon);
100  CPPUNIT_ASSERT(year == oyear);
101 
102  // A time earlier than nova epoch should fail to convert
103  sec = 0;
104  min = 0;
105  hour = 0;
106  mday = 1;
107  mon = 0;
108  year = 109;
109  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(sec,min,hour,mday,mon,year,novaTime) == false);
110 
111  // try Feb 4, 2010, 10:11:12 (UTC)
112  sec = 12;
113  min = 11;
114  hour = 10;
115  mday = 4;
116  mon = 1;
117  year = 110;
118  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(sec,min,hour,mday,mon,year,novaTime));
119  uint64_t novaTimeCompare = 2974272llu*64000000llu;
120  CPPUNIT_ASSERT(novaTime == novaTimeCompare);
121  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,osec,omin,ohour,omday,omon,oyear));
122  CPPUNIT_ASSERT(sec == osec);
123  CPPUNIT_ASSERT(min == omin);
124  CPPUNIT_ASSERT(hour == ohour);
125  CPPUNIT_ASSERT(mday == omday);
126  CPPUNIT_ASSERT(mon == omon);
127  CPPUNIT_ASSERT(year == oyear);
128 
129 }
130 
131 void
133 {
134 
135  tm timeTM;
136  timeTM.tm_sec = 0;
137  timeTM.tm_min = 0;
138  timeTM.tm_hour = 0;
139  timeTM.tm_mday = 1;
140  timeTM.tm_mon = 0;
141  timeTM.tm_year = 110;
142  uint64_t novaTime;
143  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(timeTM, novaTime));
144  CPPUNIT_ASSERT(novaTime == 0);
145 
146  tm outputTimeTM;
147  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,outputTimeTM));
148 
149  CPPUNIT_ASSERT(timeTM.tm_sec == outputTimeTM.tm_sec);
150  CPPUNIT_ASSERT(timeTM.tm_min == outputTimeTM.tm_min);
151  CPPUNIT_ASSERT(timeTM.tm_hour == outputTimeTM.tm_hour);
152  CPPUNIT_ASSERT(timeTM.tm_mday == outputTimeTM.tm_mday);
153  CPPUNIT_ASSERT(timeTM.tm_mon == outputTimeTM.tm_mon);
154  CPPUNIT_ASSERT(timeTM.tm_year == outputTimeTM.tm_year);
155 
156  // A time earlier than nova epoch should fail to convert
157  timeTM.tm_sec = 0;
158  timeTM.tm_min = 0;
159  timeTM.tm_hour = 0;
160  timeTM.tm_mday = 1;
161  timeTM.tm_mon = 0;
162  timeTM.tm_year = 109;
163  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(timeTM,novaTime) == false);
164 
165  // try Feb 4, 2010, 10:11:12 (UTC)
166  timeTM.tm_sec = 12;
167  timeTM.tm_min = 11;
168  timeTM.tm_hour = 10;
169  timeTM.tm_mday = 4;
170  timeTM.tm_mon = 1;
171  timeTM.tm_year = 110;
172  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(timeTM,novaTime));
173  uint64_t novaTimeCompare = 2974272llu*64000000llu;
174  CPPUNIT_ASSERT(novaTime == novaTimeCompare);
175  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,outputTimeTM));
176 
177  CPPUNIT_ASSERT(timeTM.tm_sec == outputTimeTM.tm_sec);
178  CPPUNIT_ASSERT(timeTM.tm_min == outputTimeTM.tm_min);
179  CPPUNIT_ASSERT(timeTM.tm_hour == outputTimeTM.tm_hour);
180  CPPUNIT_ASSERT(timeTM.tm_mday == outputTimeTM.tm_mday);
181  CPPUNIT_ASSERT(timeTM.tm_mon == outputTimeTM.tm_mon);
182  CPPUNIT_ASSERT(timeTM.tm_year == outputTimeTM.tm_year);
183 
184 }
185 
186 void
188 {
189  struct timespec unixTimeSpec;
190  struct timeval unixTimeVal;
191  struct tm unixTMTime;
192  uint64_t novaTime;
193  uint64_t expectedNovaTime;
194  struct timespec reconstructedUnixTimeSpec;
195  struct timeval reconstructedUnixTimeVal;
196  struct tm reconstructedUnixTMTime;
197 
198  // ****************************************
199  // *** One second before the time of the first leap second
200  // *** 30-Jun-2012 23:59:59.0
201  // ****************************************
202  expectedNovaTime = 5042995136000000;
203 
204  unixTMTime.tm_sec = 59;
205  unixTMTime.tm_min = 59;
206  unixTMTime.tm_hour = 23;
207  unixTMTime.tm_mday = 30;
208  unixTMTime.tm_mon = 5;
209  unixTMTime.tm_year = 112;
210  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTMTime, novaTime));
211  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
212 
213  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
214  reconstructedUnixTMTime));
215  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_sec == unixTMTime.tm_sec);
216  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_min == unixTMTime.tm_min);
217  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_hour == unixTMTime.tm_hour);
218  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mday == unixTMTime.tm_mday);
219  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mon == unixTMTime.tm_mon);
220  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_year == unixTMTime.tm_year);
221 
222  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(59, 59, 23, 30, 5, 112,
223  novaTime));
224  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
225 
226  // ****************************************
227  // *** Half a second before the time of the first leap second
228  // *** 30-Jun-2012 23:59:59.5
229  // ****************************************
230  expectedNovaTime += 32000000;
231 
232  unixTimeSpec.tv_sec = 1341100799;
233  unixTimeSpec.tv_nsec = 500000000;
234  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeSpec, novaTime));
235  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
236 
237  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
238  reconstructedUnixTimeSpec));
239  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_sec == unixTimeSpec.tv_sec);
240  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_nsec == unixTimeSpec.tv_nsec);
241 
242  unixTimeVal.tv_sec = 1341100799;
243  unixTimeVal.tv_usec = 500000;
244  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeVal, novaTime));
245  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
246 
247  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
248  reconstructedUnixTimeVal));
249  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_sec == unixTimeVal.tv_sec);
250  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_usec == unixTimeVal.tv_usec);
251 
252  // ****************************************
253  // *** Exactly at the time of the first leap second
254  // *** 01-Ju1-2012 00:00:00.0
255  // ****************************************
256  expectedNovaTime += 32000000 + 64000000;
257 
258  unixTimeSpec.tv_sec = 1341100800;
259  unixTimeSpec.tv_nsec = 0;
260  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeSpec, novaTime));
261  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
262 
263  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
264  reconstructedUnixTimeSpec));
265  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_sec == unixTimeSpec.tv_sec);
266  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_nsec == unixTimeSpec.tv_nsec);
267 
268  unixTimeVal.tv_sec = 1341100800;
269  unixTimeVal.tv_usec = 0;
270  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeVal, novaTime));
271  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
272 
273  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
274  reconstructedUnixTimeVal));
275  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_sec == unixTimeVal.tv_sec);
276  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_usec == unixTimeVal.tv_usec);
277 
278  unixTMTime.tm_sec = 0;
279  unixTMTime.tm_min = 0;
280  unixTMTime.tm_hour = 0;
281  unixTMTime.tm_mday = 1;
282  unixTMTime.tm_mon = 6;
283  unixTMTime.tm_year = 112;
284  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTMTime, novaTime));
285  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
286 
287  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
288  reconstructedUnixTMTime));
289  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_sec == unixTMTime.tm_sec);
290  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_min == unixTMTime.tm_min);
291  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_hour == unixTMTime.tm_hour);
292  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mday == unixTMTime.tm_mday);
293  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mon == unixTMTime.tm_mon);
294  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_year == unixTMTime.tm_year);
295 
296  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(0, 0, 0, 1, 6, 112,
297  novaTime));
298  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
299 
300  // ****************************************
301  // *** Half a second after the time of the first leap second
302  // *** 01-Ju1-2012 00:00:00.5
303  // ****************************************
304  expectedNovaTime += 32000000;
305 
306  unixTimeSpec.tv_sec = 1341100800;
307  unixTimeSpec.tv_nsec = 500000000;
308  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeSpec, novaTime));
309  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
310 
311  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
312  reconstructedUnixTimeSpec));
313  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_sec == unixTimeSpec.tv_sec);
314  CPPUNIT_ASSERT(reconstructedUnixTimeSpec.tv_nsec == unixTimeSpec.tv_nsec);
315 
316  unixTimeVal.tv_sec = 1341100800;
317  unixTimeVal.tv_usec = 500000;
318  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTimeVal, novaTime));
319  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
320 
321  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
322  reconstructedUnixTimeVal));
323  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_sec == unixTimeVal.tv_sec);
324  CPPUNIT_ASSERT(reconstructedUnixTimeVal.tv_usec == unixTimeVal.tv_usec);
325 
326  // ****************************************
327  // *** One second after the time of the first leap second
328  // *** 01-Ju1-2012 00:00:01.0
329  // ****************************************
330  expectedNovaTime += 32000000;
331 
332  unixTMTime.tm_sec = 1;
333  unixTMTime.tm_min = 0;
334  unixTMTime.tm_hour = 0;
335  unixTMTime.tm_mday = 1;
336  unixTMTime.tm_mon = 6;
337  unixTMTime.tm_year = 112;
338  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(unixTMTime, novaTime));
339  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
340 
341  CPPUNIT_ASSERT(NDTU::convertNovaTimeToUnixTime(novaTime,
342  reconstructedUnixTMTime));
343  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_sec == unixTMTime.tm_sec);
344  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_min == unixTMTime.tm_min);
345  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_hour == unixTMTime.tm_hour);
346  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mday == unixTMTime.tm_mday);
347  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_mon == unixTMTime.tm_mon);
348  CPPUNIT_ASSERT(reconstructedUnixTMTime.tm_year == unixTMTime.tm_year);
349 
350  CPPUNIT_ASSERT(NDTU::convertUnixTimeToNovaTime(1, 0, 0, 1, 6, 112,
351  novaTime));
352  CPPUNIT_ASSERT(novaTime == expectedNovaTime);
353 }
354 
356 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaTimingUtilities");
bool convertNovaTimeToUnixTime(uint64_t const &inputNovaTime, struct timespec &outputUnixTime)
bool getCurrentNovaTime(uint64_t &outputNovaTime)
CPPUNIT_TEST(testBasicFunctions)
OStream cout
Definition: OStream.cxx:6
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(TimingUtilitiesTests,"NovaTimingUtilities")
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaTimingUtilities")
CPPUNIT_TEST_SUITE(TimingUtilitiesTests)
bool convertUnixTimeToNovaTime(struct timespec const &inputUnixTime, uint64_t &outputNovaTime)
static constexpr Double_t year
Definition: Munits.h:185