testTime.cc
Go to the documentation of this file.
1 
13 #if __cplusplus < 201103L && !defined(__clang__)
14 #include <ext/algorithm>
15 #endif
16 #include <unistd.h>
17 #include <boost/lexical_cast.hpp>
18 
19 #include <cppunit/extensions/HelperMacros.h>
20 
21 #include <utl/TimeStamp.h>
22 #include <utl/UTCDateTime.h>
23 #include <utl/TimeInterval.h>
24 #include <utl/TimeRange.h>
25 #include <utl/Stopwatch.h>
26 #include <utl/RealTimeStopwatch.h>
27 #include <utl/AugerUnits.h>
28 #include <tst/Verify.h>
29 #include <utl/AugerException.h>
30 #include <utl/UTCDate.h>
31 #include <utl/LeapSeconds.h>
32 
33 using namespace utl;
34 using namespace std;
35 using namespace tst;
36 using boost::lexical_cast;
37 
38 
42 class TestTime : public CppUnit::TestFixture {
43 
44  CPPUNIT_TEST_SUITE(TestTime);
45  CPPUNIT_TEST(TestTimeStampInitialization);
46  CPPUNIT_TEST_EXCEPTION(TestTimeStampInitException, OutOfBoundException);
47  CPPUNIT_TEST_EXCEPTION(TestLeapException1, OutOfBoundException);
48  CPPUNIT_TEST_EXCEPTION(TestLeapException2, OutOfBoundException);
49  CPPUNIT_TEST_EXCEPTION(TestLeapException3, OutOfBoundException);
50  CPPUNIT_TEST_EXCEPTION(TestLeapException4, OutOfBoundException);
51  CPPUNIT_TEST_EXCEPTION(TestLeapException5, OutOfBoundException);
52  CPPUNIT_TEST(TestLeap1);
53  CPPUNIT_TEST(TestLeap2);
54  CPPUNIT_TEST(TestLeap3);
55  CPPUNIT_TEST(TestLeap4);
56  CPPUNIT_TEST(TestLeap5);
57  CPPUNIT_TEST(TestLeap6);
58  CPPUNIT_TEST(TestTimeIntervalInitialization);
59  CPPUNIT_TEST(TestLeapSecondBoundary);
60  CPPUNIT_TEST(TestArithmetic);
61  CPPUNIT_TEST(TestBool);
62  CPPUNIT_TEST(TestTimeRange);
63  CPPUNIT_TEST(TestTimeRangeSearch);
64  CPPUNIT_TEST(TestStopwatch);
65  CPPUNIT_TEST(TestRealTimeStopwatch);
66  CPPUNIT_TEST(TestUTCToGPSLeapSeconds);
67  CPPUNIT_TEST(TestConversions);
68  CPPUNIT_TEST(TestConversions2);
69  CPPUNIT_TEST(TestConversions3);
70  CPPUNIT_TEST(TestConversions4);
71  CPPUNIT_TEST(TestConversions5);
72  CPPUNIT_TEST(TestTimeConsistency);
73  CPPUNIT_TEST_SUITE_END();
74 
84 
85 public:
87  fLeapMinusOne(UTCDateTime(1998, 12, 31, 23, 59, 59).GetTimeStamp()),
88  fLeap(UTCDateTime(1998, 12, 31, 23, 59, 60).GetTimeStamp()),
89  fLeapPlusOne(UTCDateTime(1999, 1, 1, 0, 0, 0).GetTimeStamp()),
90  fNoLeapMinusOne(UTCDateTime(1999, 12, 31, 23, 59, 59).GetTimeStamp()),
91  fNoLeap(UTCDateTime(2000, 1, 1, 0, 0, 0).GetTimeStamp()),
92  fNoLeapPlusOne(UTCDateTime(2000, 1, 1, 0, 0, 1).GetTimeStamp()),
93  fZero(0),
94  fOneSecond(1*second),
95  fTwoSeconds(2*second)
96  { }
97 
98  void setUp() { }
99 
100  void tearDown() { }
101 
102  void
104  {
105  // Initialize time interval and check number of seconds, nanoseconds
106  //
107  TimeInterval t1(1000*second + 987654321*nanosecond);
108  CPPUNIT_ASSERT(Verify<Equal>(t1.GetSecond(), 1000L));
109  CPPUNIT_ASSERT(Verify<CloseTo>(t1.GetNanoSecond(), 987654321.));
110 
111  // Initialize a negative time interval and check seconds in interval
112  // and number of nanoseconds past second boundary.
113  //
114  TimeInterval t2(-10*second + 5e8*nanosecond);
115  CPPUNIT_ASSERT(Verify<CloseTo>(t2.GetInterval(), -9.5*second));
116  CPPUNIT_ASSERT(Verify<Equal>(t2.GetSecond(), -10L));
117  CPPUNIT_ASSERT(Verify<CloseTo>(t2.GetNanoSecond(), 500000000.));
118 
119  CPPUNIT_ASSERT(Verify<Equal>(TimeInterval::Max(),
120  TimeInterval(4.294967296e9)));
121  }
122 
123  void
125  {
126  // GPS Epoch is 6 Jan 1980. There are 6 leap years and 13 leap seconds
127  // between then and 2003. Initialize a TimeStamp to seconds since GPS Epoch
128  // (initialization to GPS second)
129  const TimeStamp tt1((365*23 + 6)*24*3600 + 13);
130  const UTCDateTime t1(tt1);
131  CPPUNIT_ASSERT(Verify<Equal>(tt1.GetGPSSecond(),
132  t1.GetTimeStamp().GetGPSSecond()));
133  CPPUNIT_ASSERT(Verify<Equal>(t1.GetInAugerFormat(),
134  string("00:00:00 06 JAN 2003")));
135  CPPUNIT_ASSERT(Verify<Equal>(t1.GetInXMLFormat(),
136  string("2003-01-06T00:00:00Z")));
137 
138  CPPUNIT_ASSERT(Verify<Equal>(t1.GetYear(), 2003));
139  CPPUNIT_ASSERT(Verify<Equal>(t1.GetMonth(), 1));
140  CPPUNIT_ASSERT(Verify<Equal>(t1.GetDay(), 6));
141  CPPUNIT_ASSERT(Verify<Equal>(t1.GetHour(), 0));
142  CPPUNIT_ASSERT(Verify<Equal>(t1.GetMinute(), 0));
143  CPPUNIT_ASSERT(Verify<Equal>(t1.GetSecond(), 0));
144  CPPUNIT_ASSERT(Verify<CloseTo>(t1.GetNanosecond(), 0.));
145 
146  // Initialization to UTC
147  const TimeStamp t2(UTCDateTime(2003, 1, 6, 0, 0, 0).GetTimeStamp());
148  CPPUNIT_ASSERT(Verify<Equal>(t2.GetGPSSecond(), tt1.GetGPSSecond()));
149 
150  // Initialization of GPS nanosecond
151  const TimeStamp t3(0, 100);
152  CPPUNIT_ASSERT(Verify<CloseTo>(t3.GetGPSNanoSecond(), 100.));
153 
154  // test methods that return year, month, day, etc.
155  const TimeStamp tBrokenDown(UTCDateTime(2005, 3, 12, 20, 21, 22, 100).GetTimeStamp());
156  const UTCDateTime tb(tBrokenDown);
157  CPPUNIT_ASSERT(Verify<Equal>(tb.GetYear(), 2005));
158  CPPUNIT_ASSERT(Verify<Equal>(tb.GetMonth(), 3));
159  CPPUNIT_ASSERT(Verify<Equal>(tb.GetDay(), 12));
160  CPPUNIT_ASSERT(Verify<Equal>(tb.GetHour(), 20));
161  CPPUNIT_ASSERT(Verify<Equal>(tb.GetMinute(), 21));
162  CPPUNIT_ASSERT(Verify<Equal>(tb.GetSecond(), 22));
163  CPPUNIT_ASSERT(Verify<CloseTo>(tb.GetNanosecond(), 100.));
164 
165  const TimeStamp t4(t3);
166  CPPUNIT_ASSERT(Verify<Equal>(t3, t4));
167  }
168 
169  void
171  {
172  // Try to initialize to a time before GPS epoch
173  [[maybe_unused]] TimeStamp beforeGPS(UTCDateTime(1979, 1, 1).GetTimeStamp());
174  }
175 
176  void TestLeapException1() { Expected(); UTCDateTime(1981, 12, 31, 23, 59, 60); }
177  void TestLeapException2() { Expected(); UTCDateTime(1982, 12, 31, 23, 59, 60); }
178  void TestLeapException3() { Expected(); UTCDateTime(1983, 12, 31, 23, 59, 60); }
179  void TestLeapException4() { Expected(); UTCDateTime(1984, 12, 31, 23, 59, 60); }
180  void TestLeapException5() { Expected(); UTCDateTime(2009, 12, 31, 23, 59, 60); }
181 
182  void TestLeap1() { UTCDateTime(1981, 6, 30, 23, 59, 60); }
183  void TestLeap2() { UTCDateTime(1982, 6, 30, 23, 59, 60); }
184  void TestLeap3() { UTCDateTime(1983, 6, 30, 23, 59, 60); }
185  void TestLeap4() { UTCDateTime(1985, 6, 30, 23, 59, 60); }
186  void TestLeap5() { UTCDateTime(1987, 12, 31, 23, 59, 60); }
187  void TestLeap6() { UTCDateTime(2008, 12, 31, 23, 59, 60); }
188 
189  void
191  {
192  // Test for case when leap second is present
193  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fLeapMinusOne).GetInAugerFormat(),
194  string("23:59:59 31 DEC 1998")));
195  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fLeap).GetInAugerFormat(),
196  string("23:59:60 31 DEC 1998")));
197  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fLeapPlusOne).GetInAugerFormat(),
198  string("00:00:00 01 JAN 1999")));
199  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fLeapMinusOne + fOneSecond).GetInAugerFormat(),
200  string("23:59:60 31 DEC 1998")));
201 
202  // check that gps second increments through leap second
203  CPPUNIT_ASSERT(Verify<Equal>(fLeapMinusOne.GetGPSSecond(),
204  fLeap.GetGPSSecond() - 1));
205  CPPUNIT_ASSERT(Verify<Equal>(fLeap.GetGPSSecond(),
206  fLeapPlusOne.GetGPSSecond() - 1));
207  CPPUNIT_ASSERT(Verify<Equal>(fNoLeapMinusOne.GetGPSSecond(),
208  fNoLeap.GetGPSSecond() - 1));
209  CPPUNIT_ASSERT(Verify<Equal>(fNoLeap.GetGPSSecond(),
210  fNoLeapPlusOne.GetGPSSecond() - 1));
211 
212  // Test for case when leap second is not present
213  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fNoLeapMinusOne + fOneSecond).GetInAugerFormat(),
214  UTCDateTime(fNoLeap).GetInAugerFormat()));
215  CPPUNIT_ASSERT(Verify<Equal>(UTCDateTime(fNoLeap + fOneSecond).GetInAugerFormat(),
216  UTCDateTime(fNoLeapPlusOne).GetInAugerFormat()));
217  }
218 
219  void
221  {
222  // [TimeStamp] - [TimeStamp] = [TimeInterval]
223  CPPUNIT_ASSERT(Verify<Equal>((fLeapPlusOne - fLeapMinusOne).GetSecond(),
224  2L));
225 
226  // [TimeStamp] - [TimeInterval] = [TimeStamp]
227  CPPUNIT_ASSERT(Verify<Equal>(fLeapMinusOne - fOneSecond,
228  TimeStamp(UTCDateTime(1998, 12, 31, 23, 59, 58).GetTimeStamp())));
229 
230  // [TimeStamp] + [TimeInterval] = [TimeStamp]
231  CPPUNIT_ASSERT(Verify<Equal>(fLeapPlusOne + fOneSecond,
232  TimeStamp(UTCDateTime(1999, 1, 1, 0, 0, 1).GetTimeStamp())));
233 
234  // [TimeInterval] / [double] = [TimeInterval]
235  CPPUNIT_ASSERT(Verify<CloseTo>((fOneSecond/2.0).GetInterval(),
236  0.5*second));
237 
238  // [TimeInterval] + [TimeInterval] = [TimeInterval]
239  CPPUNIT_ASSERT(Verify<CloseTo>((fOneSecond + fOneSecond).GetInterval(),
240  2*second));
241 
242  // [TimeInterval] - [TimeInterval] = [TimeInterval]
243  CPPUNIT_ASSERT(Verify<CloseTo>((fTwoSeconds - fOneSecond).GetInterval(),
244  1*second));
245 
246  // [TimeInterval] * [double] = [TimeInterval]
247  CPPUNIT_ASSERT(Verify<CloseTo>(((fOneSecond)*10.0).GetInterval(),
248  10*second));
249 
250  // [TimeInterval] = double (assignment to a double)
251  TimeInterval ti = 35*second;
252  CPPUNIT_ASSERT(Verify<CloseTo>(ti.GetInterval(), 35*second));
253 
254  // add ns near a second boundary and check for proper rollover
255  TimeStamp ta(100, 999999998);
256  CPPUNIT_ASSERT(Verify<CloseTo>(ta.GetGPSNanoSecond(), 999999998.));
257  ta += 10*ns;
258  CPPUNIT_ASSERT(Verify<Equal>(ta.GetGPSSecond(), 101UL));
259  CPPUNIT_ASSERT(Verify<CloseTo>(ta.GetGPSNanoSecond(), 8.));
260 
261  // subtract ns near a second boundary and check for proper rollover
262  TimeStamp tb(100, 8);
263  CPPUNIT_ASSERT(Verify<CloseTo>(ta.GetGPSNanoSecond(), 8.));
264  tb -= 10*ns;
265  CPPUNIT_ASSERT(Verify<Equal>(tb.GetGPSSecond(), 99UL));
266  CPPUNIT_ASSERT(Verify<CloseTo>(tb.GetGPSNanoSecond(), 999999998.));
267 
268  // add >1 ns near second boundary and check for rollover
269  TimeStamp tc(100, 999999998.);
270  tc += 1*second + 10*nanosecond;
271  CPPUNIT_ASSERT(Verify<Equal>(tc.GetGPSSecond(), 102UL));
272  CPPUNIT_ASSERT(Verify<CloseTo>(tc.GetGPSNanoSecond(), 8.));
273 
274  // subtract > 1ns near a second boundary and check for proper rollover
275  TimeStamp td(100, 8);
276  td -= 1*second + 10*nanosecond;
277  CPPUNIT_ASSERT(Verify<Equal>(td.GetGPSSecond(), 98UL));
278  CPPUNIT_ASSERT(Verify<CloseTo>(td.GetGPSNanoSecond(), 999999998.));
279 
280  // check for rollover upon construction
281  TimeStamp te(0, 1000000001);
282  CPPUNIT_ASSERT(Verify<Equal>(te.GetGPSSecond(), 1UL));
283  CPPUNIT_ASSERT(Verify<CloseTo>(te.GetGPSNanoSecond(), 1.));
284 
285  // check for rollover upon initialization after construction
286  TimeStamp tf;
287  tf.SetGPSTime(0, 1000000001);
288  CPPUNIT_ASSERT(Verify<Equal>(tf.GetGPSSecond(), 1UL));
289  CPPUNIT_ASSERT(Verify<CloseTo>(tf.GetGPSNanoSecond(), 1.));
290  }
291 
292  void
294  {
295  CPPUNIT_ASSERT(fLeapMinusOne < fLeap);
296  CPPUNIT_ASSERT(fLeapPlusOne > fLeap);
297  CPPUNIT_ASSERT(fLeap == fLeap);
298  CPPUNIT_ASSERT(fLeap != fLeapPlusOne);
299  CPPUNIT_ASSERT(fLeap >= fLeap);
300  CPPUNIT_ASSERT(fLeap <= fLeap);
301 
302  CPPUNIT_ASSERT(fZero.GetInterval() <= numeric_limits<double>::min());
303  CPPUNIT_ASSERT(fOneSecond.GetInterval() >= numeric_limits<double>::min());
304  CPPUNIT_ASSERT(fTwoSeconds > fOneSecond);
305  CPPUNIT_ASSERT(fOneSecond < fTwoSeconds);
306  CPPUNIT_ASSERT(fOneSecond <= fTwoSeconds);
307  CPPUNIT_ASSERT(fOneSecond <= fOneSecond);
308  CPPUNIT_ASSERT(fTwoSeconds >= fOneSecond);
309  CPPUNIT_ASSERT(fTwoSeconds >= fTwoSeconds);
310  }
311 
312  void
314  {
315  TimeRange tr1(TimeStamp(100, 0), TimeStamp(200, 500));
316  TimeRange tr2 = tr1;
317 
318  // operator==, operator!=
319  CPPUNIT_ASSERT(tr1 == tr2);
320  CPPUNIT_ASSERT(!(tr1 != tr2));
321 
322  CPPUNIT_ASSERT(tr1.HasCommonTime(tr2));
323  CPPUNIT_ASSERT(tr2.HasCommonTime(tr1));
324 
325  tr2 = TimeRange(TimeStamp(300, 10), TimeStamp(400, 1500));
326 
327  CPPUNIT_ASSERT(tr1 != tr2);
328  CPPUNIT_ASSERT(!(tr1 == tr2));
329 
330  CPPUNIT_ASSERT(!tr1.HasCommonTime(tr2));
331  CPPUNIT_ASSERT(!tr2.HasCommonTime(tr1));
332 
333  // operator<, operator>
334  CPPUNIT_ASSERT(tr1 < tr2);
335  CPPUNIT_ASSERT(tr2 > tr1);
336  CPPUNIT_ASSERT(!(tr1 > tr2));
337  CPPUNIT_ASSERT(!(tr2 < tr1));
338 
339  // SetTimeRange
340  tr1.SetTimeRange(tr2.GetStartTime(), tr2.GetStopTime());
341  CPPUNIT_ASSERT(tr1 == tr2);
342  CPPUNIT_ASSERT(!(tr1 != tr2));
343 
344  // intersection
345  tr1.SetTimeRange(TimeStamp(250, 0), TimeStamp(301, 0));
346 
347  CPPUNIT_ASSERT(tr1.HasCommonTime(tr2));
348  CPPUNIT_ASSERT(tr2.HasCommonTime(tr1));
349 
350  tr1.SetTimeRange(TimeStamp(399, 0), TimeStamp(401, 0));
351 
352  CPPUNIT_ASSERT(tr1.HasCommonTime(tr2));
353  CPPUNIT_ASSERT(tr2.HasCommonTime(tr1));
354 
355  tr1.SetTimeRange(TimeStamp(200, 20), TimeStamp(5432, 10));
356 
357  CPPUNIT_ASSERT(tr1.HasCommonTime(tr2));
358  CPPUNIT_ASSERT(tr2.HasCommonTime(tr1));
359  }
360 
361  void
363  {
364  const TimeInterval dt(100);
365  const TimeStamp t0(100, 0);
366  vector<TimeRange> trs;
367  for (int i = 20-2; i >= 0; i-=2)
368  trs.push_back(TimeRange(t0 + double(i)*dt, t0 + double(i+1)*dt));
369 
370  CPPUNIT_ASSERT(!is_sorted(trs.begin(), trs.end()));
371 
372  sort(trs.begin(), trs.end());
373 
374  CPPUNIT_ASSERT(is_sorted(trs.begin(), trs.end()));
375 
376  const TimeStamp t1(t0 + 5.2*dt);
377 
378  vector<TimeRange>::const_iterator it1 =
379  lower_bound(trs.begin(), trs.end(), TimeRange(t1, t1));
380 
381  CPPUNIT_ASSERT(it1 != trs.begin());
382  CPPUNIT_ASSERT(it1 != trs.end());
383 
384  CPPUNIT_ASSERT(!it1->IsInRange(t1));
385 
386  --it1;
387 
388  CPPUNIT_ASSERT(!it1->IsInRange(t1));
389 
390  const TimeStamp t2(t0 + 6.2*dt);
391 
392  vector<TimeRange>::const_iterator it2 =
393  lower_bound(trs.begin(), trs.end(), TimeRange(t2, t2));
394 
395  CPPUNIT_ASSERT(it2 != trs.begin());
396  CPPUNIT_ASSERT(it2 != trs.end());
397 
398  CPPUNIT_ASSERT(!it2->IsInRange(t2));
399 
400  --it2;
401 
402  CPPUNIT_ASSERT(it2->IsInRange(t2));
403  }
404 
405  void
407  {
408  Stopwatch sw;
409  sw.Reset();
410  sw.Start();
411  [[maybe_unused]] double sum = 0;
412  do {
413  for (int i = 0; i < 100000; ++i)
414  sum += i*i;
415  } while (sw.GetCPUTime() < 1.23456789012345*second);
416  sw.Stop();
417  CPPUNIT_ASSERT(sw.GetCPUTime() > 0);
418  CPPUNIT_ASSERT(sw.GetCPUTime(Stopwatch::eTotal) >= sw.GetCPUTime(Stopwatch::eUser));
419  CPPUNIT_ASSERT(sw.GetCPUTime(Stopwatch::eTotal) >= sw.GetCPUTime(Stopwatch::eSystem));
420  }
421 
422  void
424  {
426  sw.Reset();
427  sw.Start();
428  cout << "\nusleep(10000)..." << flush;
429  do
430  usleep(10000);
431  while (sw.GetTime() < 1.23456789012345*second);
432  const double time = sw.Stop();
433  cout << endl;
434  CPPUNIT_ASSERT(1.234*second < time && time < 1.4*second);
435  CPPUNIT_ASSERT(Verify<Equal>(time, sw.GetTime()));
436  cout << "usleep(1000000)..." << flush;
437  usleep(1000000);
438  const double time2 = sw.GetTime();
439  cout << endl;
440  CPPUNIT_ASSERT(Verify<Equal>(time, time2));
441  sw.Start();
442  cout << "usleep(1000)..." << flush;
443  usleep(1000);
444  const double time3 = sw.Stop();
445  cout << endl;
446  CPPUNIT_ASSERT(Verify<GreaterOrEqual>(time3, time));
447  sw.Reset();
448  CPPUNIT_ASSERT(Verify<Equal>(sw.GetTime(), 0.));
449  for (int i = 0; i < 10; ++i) {
450  cout << "usleep(200000)..." << flush;
451  sw.Start();
452  usleep(200000);
453  sw.Stop();
454  cout << "\nusleep(10000)..." << flush;
455  usleep(100000);
456  cout << endl;
457  }
458  cout << "\ntime " << sw.GetTime() << endl;
459  }
460 
461  void
463  {
464  const UTCDate gpsEpoch = UTCDate::GetGPSEpoch();
465  const int gpsUnixSec = gpsEpoch.GetUnixSecond();
466 
467  struct UTCDateDays {
468  UTCDate fUTCDate;
469  int fDays;
470  };
471 
472  const UTCDateDays utcDays[] = {
473  // these dates are the UTC insertions of leap second
474  { UTCDate(1981, UTCDate::eJul, 1), 361 + 0*365 + 0 + 181 },
475  { UTCDate(1982, UTCDate::eJul, 1), 361 + 1*365 + 0 + 181 },
476  { UTCDate(1983, UTCDate::eJul, 1), 361 + 2*365 + 0 + 181 },
477  { UTCDate(1985, UTCDate::eJul, 1), 361 + 4*365 + 1 + 181 },
478  { UTCDate(1988, UTCDate::eJan, 1), 361 + 7*365 + 1 },
479  { UTCDate(1990, UTCDate::eJan, 1), 361 + 9*365 + 2 },
480  { UTCDate(1991, UTCDate::eJan, 1), 361 + 10*365 + 2 },
481  { UTCDate(1992, UTCDate::eJul, 1), 361 + 11*365 + 3 + 181 },
482  { UTCDate(1993, UTCDate::eJul, 1), 361 + 12*365 + 3 + 181 },
483  { UTCDate(1994, UTCDate::eJul, 1), 361 + 13*365 + 3 + 181 },
484  { UTCDate(1996, UTCDate::eJan, 1), 361 + 15*365 + 3 },
485  { UTCDate(1997, UTCDate::eJul, 1), 361 + 16*365 + 4 + 181 },
486  { UTCDate(1999, UTCDate::eJan, 1), 361 + 18*365 + 4 },
487  { UTCDate(2006, UTCDate::eJan, 1), 361 + 25*365 + 6 },
488  { UTCDate(2009, UTCDate::eJan, 1), 361 + 28*365 + 7 }
489  };
490 
491  const unsigned int secPerDay = 24 * 60 * 60;
492  const int n = sizeof(utcDays) / sizeof(utcDays[0]);
493  for (int i = 0; i < n; ++i) {
494  const time_t unixSecond = utcDays[i].fUTCDate.GetUnixSecond();
495  const int days = (unixSecond - gpsUnixSec) / secPerDay;
496  const int rem = (unixSecond - gpsUnixSec) % secPerDay;
497  const int check = utcDays[i].fDays;
498  CPPUNIT_ASSERT(Verify<Equal>(days, check));
499  CPPUNIT_ASSERT(Verify<Equal>(rem, 0));
500  const unsigned int gpsSecond = unixSecond - gpsUnixSec + i;
501  time_t unixSecond2;
502  bool isLeap;
503  // -1
504  isLeap = LeapSeconds::GetInstance().ConvertGPSToUnix(gpsSecond-1, unixSecond2);
505  CPPUNIT_ASSERT(!isLeap);
506  CPPUNIT_ASSERT(Verify<Equal>(unixSecond2, unixSecond-1));
507  // 0
508  isLeap = LeapSeconds::GetInstance().ConvertGPSToUnix(gpsSecond, unixSecond2);
509  CPPUNIT_ASSERT(isLeap);
510  CPPUNIT_ASSERT(Verify<Equal>(unixSecond2, unixSecond));
511  // +1
512  isLeap = LeapSeconds::GetInstance().ConvertGPSToUnix(gpsSecond+1, unixSecond2);
513  CPPUNIT_ASSERT(!isLeap);
514  CPPUNIT_ASSERT(Verify<Equal>(unixSecond2, unixSecond));
515  }
516  }
517 
518  void
520  {
521  const UTCDateTime utc =
522  boost::lexical_cast<UTCDateTime>("2002-05-12T00:23:45.000001500Z");
523  CPPUNIT_ASSERT(Verify<Equal>(utc.GetYear(), 2002));
524  CPPUNIT_ASSERT(Verify<Equal>(utc.GetMonth(), 5));
525  CPPUNIT_ASSERT(Verify<Equal>(utc.GetDay(), 12));
526  CPPUNIT_ASSERT(Verify<Equal>(utc.GetHour(), 0));
527  CPPUNIT_ASSERT(Verify<Equal>(utc.GetMinute(), 23));
528  CPPUNIT_ASSERT(Verify<Equal>(utc.GetSecond(), 45));
529  CPPUNIT_ASSERT(Verify<CloseTo>(utc.GetNanosecond(), 1500.));
530  const TimeStamp ts = utc.GetTimeStamp();
531  CPPUNIT_ASSERT(Verify<CloseTo>(ts.GetGPSNanoSecond(), 1500.));
532  const UTCDateTime utc2(ts);
533  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetYear(), 2002));
534  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetMonth(), 5));
535  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetDay(), 12));
536  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetHour(), 0));
537  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetMinute(), 23));
538  CPPUNIT_ASSERT(Verify<Equal>(utc2.GetSecond(), 45));
539  CPPUNIT_ASSERT(Verify<CloseTo>(utc2.GetNanosecond(), 1500.));
540  CPPUNIT_ASSERT(Verify<Equal>(utc, utc2));
541  const TimeStamp ts2 = utc2.GetTimeStamp();
542  CPPUNIT_ASSERT(Verify<Equal>(ts, ts2));
543  }
544 
545  void
547  {
548  const char* const date[][2] = {
549  { "2002-05-12T00:23:45.000001500Z", "2002-05-12T00:23:45.000001500" },
550  { "2002-05-12T00:23:45Z", "2002-05-12T00:23:45" },
551  { "2002-05-12T00:23:45.000001500Z", "2002-05-12T00:23:45.000001500" },
552  { "2002-05-12T00:23:45Z", "2002-05-12T00:23:45" }
553  };
554 
555  const int n = 2;
556  for (int i = 0; i < n; ++i) {
557  const UTCDateTime utc1 =
558  boost::lexical_cast<UTCDateTime>(date[i][0]);
559  const UTCDateTime utc2 =
560  boost::lexical_cast<UTCDateTime>(date[i][1]);
561  CPPUNIT_ASSERT(Verify<Equal>(utc1, utc2));
562  const TimeStamp ts = utc1.GetTimeStamp();
563  const UTCDateTime utc3(ts);
564  const TimeStamp ts2 = utc3.GetTimeStamp();
565  CPPUNIT_ASSERT(Verify<Equal>(ts, ts2));
566  }
567  }
568 
569  void
571  {
572  const int n = 3;
573  const UTCDateTime date[][2] = {
574  { UTCDateTime(1998, 12, 31), UTCDateTime(1999, 1, 2) },
575  { UTCDateTime(2005, 12, 31), UTCDateTime(2006, 1, 2) },
576  { UTCDateTime(2009, 12, 31), UTCDateTime(2009, 1, 2) }
577  };
578  const TimeInterval one(1*second);
579  for (int i = 0; i < n; i++) {
580  const unsigned int start = date[i][0].GetTimeStamp().GetGPSSecond();
581  const unsigned int stop = date[i][1].GetTimeStamp().GetGPSSecond();
582  TimeStamp run(start);
583  int year = 0;
584  int month = 0;
585  for (unsigned long gps = start; gps <= stop; ++gps) {
586  const UTCDateTime utc(TimeStamp(gps, 0U));
587  if (year != utc.GetYear()) {
588  year = utc.GetYear();
589  cout << '\n' << year << ':';
590  }
591  if (month != utc.GetMonth()) {
592  month = utc.GetMonth();
593  cout << ' ' << month << flush;
594  }
595  CPPUNIT_ASSERT(Verify<Equal>(gps, utc.GetTimeStamp().GetGPSSecond()));
596  CPPUNIT_ASSERT(Verify<Equal>(gps, run.GetGPSSecond()));
597  run += one;
598  }
599  }
600  cout << endl;
601  }
602 
603  void
605  {
606  const unsigned long int gps1 = 1055178059;
607  time_t unix1 = 0;
608  LeapSeconds::GetInstance().ConvertGPSToUnix(gps1, unix1);
609  unsigned long gps2 = 0;
610  LeapSeconds::GetInstance().ConvertUnixToGPS(unix1, gps2);
611  CPPUNIT_ASSERT(Verify<Equal>(gps1, gps2));
612  }
613 
614  void
616  {
617  const UTCDateTime dt(2014, 10, 19, 7, 46, 31);
618  const TimeStamp ts = dt.GetTimeStamp();
619  const unsigned long gps = 1097740007;
620  CPPUNIT_ASSERT(Verify<Equal>(ts.GetGPSSecond(), gps));
621  }
622 
623  void
625  {
626  {
627  const TimeStamp tsMax = TimeStamp::Max();
628  const UTCDateTime utcdtMax(tsMax);
629  const TimeStamp ts = utcdtMax.GetTimeStamp();
630  CPPUNIT_ASSERT(Verify<Equal>(tsMax, ts));
631  const UTCDateTime dt(ts);
632  CPPUNIT_ASSERT(Verify<Equal>(utcdtMax, dt));
633  const UTCDateTime utcdtMax2 = UTCDateTime::Max();
634  CPPUNIT_ASSERT(Verify<Equal>(utcdtMax, utcdtMax2));
635  const TimeStamp tsMax2 = utcdtMax2.GetTimeStamp();
636  CPPUNIT_ASSERT(Verify<Equal>(tsMax, tsMax2));
637  const TimeStamp tsMax1 = tsMax - TimeInterval(1);
638  CPPUNIT_ASSERT(tsMax > tsMax1);
639  const TimeStamp tsMax0 = tsMax1 + TimeInterval(1);
640  CPPUNIT_ASSERT(Verify<Equal>(tsMax, tsMax0));
641 
642  const TimeStamp empty;
643  const TimeInterval big = tsMax - empty;
644  const TimeStamp maybe = empty + big;
645  CPPUNIT_ASSERT(Verify<CloseTo>(tsMax - maybe, TimeInterval(0)));
646 
647  const UTCDateTime utempty(empty);
648  const TimeStamp empty2 = utempty.GetTimeStamp();
649  CPPUNIT_ASSERT(Verify<Equal>(empty, empty2));
650  const UTCDateTime utempty2(empty2);
651  CPPUNIT_ASSERT(Verify<Equal>(utempty, utempty2));
652 
653  CPPUNIT_ASSERT(Verify<Equal>(
654  empty,
655  UTCDateTime::GetGPSEpoch().GetTimeStamp()
656  ));
657  CPPUNIT_ASSERT(Verify<Equal>(
658  empty,
659  UTCDateTime::Min().GetTimeStamp()
660  ));
661  }
662  // This would fail since UTC epoch is 10 years earlier than GPS epoch
663  /*{
664  const UTCDateTime utczero;
665  const TimeStamp tszero = utczero.GetTimeStamp();
666  const UTCDateTime utczero2(tszero);
667  CPPUNIT_ASSERT(Verify<Equal>(utczero, utczero2));
668  const TimeStamp tszero2 = utczero2.GetTimeStamp();
669  CPPUNIT_ASSERT(Verify<Equal>(tszero, tszero2));
670  }*/
671  }
672 
673 };
674 
675 
677 
678 // Configure (x)emacs for this file ...
679 // Local Variables:
680 // mode: c++
681 // compile-command: "make -C .. -k testTime && ../testTime"
682 // End:
std::time_t GetUnixSecond() const
Relative to Unix epoch (1 Jan 1970 00:00:00 UTC) without leap corrections.
Definition: UTCDate.h:68
void TestConversions5()
Definition: testTime.cc:615
static TimeStamp Max()
Definition: TimeStamp.h:177
constexpr double second
Definition: AugerUnits.h:145
void TestLeapException2()
Definition: testTime.cc:177
void TestLeapException5()
Definition: testTime.cc:180
void TestLeap4()
Definition: testTime.cc:185
static UTCDateTime Min()
Definition: UTCDateTime.h:72
TimeInterval fZero
Definition: testTime.cc:81
TestTime()
Definition: testTime.cc:86
double GetNanosecond() const
Definition: UTCDateTime.h:60
void TestLeap6()
Definition: testTime.cc:187
TimeStamp fLeap
Definition: testTime.cc:76
Time interval defined by two TimeStamps.
Definition: TimeRange.h:23
int GetHour() const
Definition: UTCDateTime.h:54
std::string GetInAugerFormat() const
Definition: UTCDateTime.cc:78
TimeStamp fLeapPlusOne
Definition: testTime.cc:77
void setUp()
Definition: testTime.cc:98
TimeStamp fLeapMinusOne
Definition: testTime.cc:75
void TestLeapException4()
Definition: testTime.cc:179
void TestArithmetic()
Definition: testTime.cc:220
int GetYear() const
Definition: UTCDate.h:44
int GetMinute() const
Definition: UTCDateTime.h:56
void TestTimeRange()
Definition: testTime.cc:313
long GetSecond() const
Get the seconds floor for the interval.
Definition: TimeInterval.cc:13
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
#define U
void TestRealTimeStopwatch()
Definition: testTime.cc:423
TimeStamp fNoLeap
Definition: testTime.cc:79
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Exception for reporting variable out of valid range.
static UTCDateTime GetGPSEpoch()
Definition: UTCDateTime.h:68
void SetTimeRange(const TimeStamp &start, const TimeStamp &stop)
Set time range.
Definition: TimeRange.h:39
static UTCDateTime Max()
Definition: UTCDateTime.cc:156
void TestConversions3()
Definition: testTime.cc:570
void TestLeapException1()
Definition: testTime.cc:176
void SetGPSTime(const unsigned long sec, const double nsec=0)
Set GPS second and (optionally) nanosecond.
Definition: TimeStamp.h:120
int GetSecond() const
Definition: UTCDateTime.h:58
TimeInterval fOneSecond
Definition: testTime.cc:82
constexpr double nanosecond
Definition: AugerUnits.h:143
static UTCDate GetGPSEpoch()
Definition: UTCDate.h:63
void TestUTCToGPSLeapSeconds()
Definition: testTime.cc:462
void TestTimeStampInitException()
Definition: testTime.cc:170
void Reset()
Definition: Stopwatch.cc:21
void TestConversions4()
Definition: testTime.cc:604
void TestLeapSecondBoundary()
Definition: testTime.cc:190
void Expected()
Print `Expected&#39; for expected failures.
Definition: Verify.h:85
a t3: algo, second, usecond and a vector of &lt;t3stat&gt;
Definition: XbT2.h:29
void tearDown()
Definition: testTime.cc:100
void TestTimeStampInitialization()
Definition: testTime.cc:124
a second level trigger
Definition: XbT2.h:8
void TestTimeIntervalInitialization()
Definition: testTime.cc:103
fwk::VModule::ResultFlag(fwk::VModule::* run)(evt::Event &)
Definition: fwkPython.cc:59
void Start()
Definition: Stopwatch.cc:29
double GetInterval() const
Get the time interval as a double (in Auger base units)
Definition: TimeInterval.h:69
void TestConversions2()
Definition: testTime.cc:546
void TestLeap5()
Definition: testTime.cc:186
int GetMonth() const
Definition: UTCDate.h:46
void TestLeap3()
Definition: testTime.cc:184
A TimeInterval is used to represent time elapsed between two events.
Definition: TimeInterval.h:43
int GetDay() const
Definition: UTCDate.h:48
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
double GetGPSNanoSecond() const
GPS nanosecond.
Definition: TimeStamp.h:127
TimeInterval fTwoSeconds
Definition: testTime.cc:83
void TestConversions()
Definition: testTime.cc:519
constexpr double ns
Definition: AugerUnits.h:162
TimeStamp fNoLeapMinusOne
Definition: testTime.cc:78
void TestTimeRangeSearch()
Definition: testTime.cc:362
double Stop()
returns time since last call to Start()
double GetCPUTime(const CPUTime kind=eTotal)
Definition: Stopwatch.cc:52
double GetNanoSecond() const
Get integer number of nanoseconds past seconds boundary.
Definition: TimeInterval.cc:25
std::string GetInXMLFormat() const
Definition: UTCDateTime.cc:94
void TestStopwatch()
Definition: testTime.cc:406
void TestTimeConsistency()
Definition: testTime.cc:624
void TestBool()
Definition: testTime.cc:293
bool HasCommonTime(const TimeRange &tr) const
Definition: TimeRange.h:75
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115
TimeStamp fNoLeapPlusOne
Definition: testTime.cc:80
void TestLeap2()
Definition: testTime.cc:183
void TestLeapException3()
Definition: testTime.cc:178
const double year
Definition: GalacticUnits.h:22
static TimeInterval Max()
Definition: TimeInterval.h:145
void TestLeap1()
Definition: testTime.cc:182

, generated on Tue Sep 26 2023.