-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_utilities.hpp
652 lines (582 loc) · 17.5 KB
/
time_utilities.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/**
* @file
*
* Utility classes around timespec and timeval data structures.
* This approach attempts to keep time data as a timeval (or timespec)
* data structure. This keeps the data in the most precise format
* available. Converting to an integer, long, or double will
* lose precision, and by using the functions in this file simply
* isn't needed.
*
* This header requires C++11 support.
*
* Naming convention is Pascal case, with the dreaded "C" prefix
* in front of classes, mostly because it's a more suscint way to
* denote that this is a wrapper class around already existing
* structures.
*
* MIT License
*
* Copyright (c) 2016, Michael Becker (michael.f.becker@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.man
*/
#ifndef TIME_UTILITIES_HPP__
#define TIME_UTILITIES_HPP__
#include <iostream>
#include <ctime>
#ifdef USING_TIMEVAL
#include <sys/time.h>
#endif
/**
* Various time conversions.
*/
#define NS_IN_SECOND (1000000000L)
#define US_IN_SECOND (1000000L)
#define MS_IN_SECOND (1000L)
#define NS_IN_MS (1000L * 1000L)
#define US_IN_MS (1000L)
/**
* Wrapper class for working with c style timespec structures.
*/
class CTimeSpec
{
public:
/**
* Default null ctor.
*/
CTimeSpec()
: ts {0, 0}
{}
/**
* ctor
* @param ms total milliseconds used to init the class.
*/
CTimeSpec(unsigned int ms)
: ts {ms / MS_IN_SECOND, (ms % MS_IN_SECOND) * NS_IN_MS}
{}
/**
* ctor
* @param t timespec structure used to init the class.
* This ctor guarantees that the structure is normalized.
*/
CTimeSpec(struct timespec t)
{
while (t.tv_nsec >= NS_IN_SECOND) {
t.tv_sec++;
t.tv_nsec -= NS_IN_SECOND;
}
while (t.tv_nsec < 0) {
t.tv_sec--;
t.tv_nsec += NS_IN_SECOND;
}
ts = t;
}
/**
* ctor
* @param sec seconds used to init the sec portion.
* @param nsec nseconds used to init the nsec portion.
* This ctor guarantees that the structure is normalized.
*/
CTimeSpec(time_t sec, long nsec)
{
while (nsec >= NS_IN_SECOND) {
sec++;
nsec -= NS_IN_SECOND;
}
while (nsec < 0) {
sec--;
nsec += NS_IN_SECOND;
}
ts.tv_sec = sec;
ts.tv_nsec = nsec;
}
#ifdef USING_TIMEVAL
/**
* ctor - Create a CTimeSpec from a timeval struct.
* @param tv timeval structure used to init the class.
* This ctor guarantees that the structure is normalized.
*/
CTimeSpec(struct timeval tv)
{
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
while (ts.tv_nsec >= NS_IN_SECOND) {
ts.tv_sec++;
ts.tv_nsec -= NS_IN_SECOND;
}
while (ts.tv_nsec < 0) {
ts.tv_sec--;
ts.tv_nsec += NS_IN_SECOND;
}
}
#endif
/**
* Static factory returning a CTimeSpec that represents "now"
* in wall clock time. See CLOCK_REALTIME.
*/
static CTimeSpec Now()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return CTimeSpec {ts};
}
/**
* Static factory returning a CTimeSpec that represents "now"
* in monotonic time. See CLOCK_MONOTONIC.
*/
static CTimeSpec NowMonotonic()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return CTimeSpec {ts};
}
/**
* Static factory returning a CTimeSpec that represents "now"
* in monotonic raw time. See CLOCK_MONOTONIC_RAW.
*/
static CTimeSpec NowMonotonicRaw()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return CTimeSpec {ts};
}
/**
* Utility function to return a copy of the internal
* timespec structure.
*/
struct timespec c_timespec()
{
return ts;
}
/**
* Adds a CTimeSpec to this one.
* Guarantees the result is normalized.
*/
CTimeSpec& operator+=(const CTimeSpec& rhs)
{
ts.tv_sec += rhs.ts.tv_sec;
ts.tv_nsec += rhs.ts.tv_nsec;
if (ts.tv_nsec >= NS_IN_SECOND){
ts.tv_sec++;
ts.tv_nsec -= NS_IN_SECOND;
}
return *this;
}
/**
* Subtracts a CTimeSpec to this one.
* Guarantees the result is normalized.
*/
CTimeSpec& operator-=(const CTimeSpec& rhs)
{
ts.tv_sec -= rhs.ts.tv_sec;
ts.tv_nsec -= rhs.ts.tv_nsec;
if (ts.tv_nsec < 0){
ts.tv_sec--;
ts.tv_nsec += NS_IN_SECOND;
}
return *this;
}
/**
* Compares a CTimeSpecs against this one
* for non-equality.
*/
bool operator!=(const CTimeSpec& rhs) const
{
if (ts.tv_sec != rhs.ts.tv_sec)
return true;
else if (ts.tv_nsec != rhs.ts.tv_nsec)
return true;
else
return false;
}
/**
* Compares a CTimeSpecs against this one
* for equality.
*/
bool operator==(const CTimeSpec& rhs) const
{
return !(*this != rhs);
}
/**
* Compares a CTimeSpecs against this one
* for less than.
*/
bool operator<(const CTimeSpec& rhs) const
{
if (ts.tv_sec < rhs.ts.tv_sec)
return true;
else if (ts.tv_nsec < rhs.ts.tv_nsec)
return true;
else
return false;
}
/**
* Compares a CTimeSpecs against this one
* for greater than.
*/
bool operator>(const CTimeSpec& rhs) const
{
if (ts.tv_sec > rhs.ts.tv_sec)
return true;
else if (ts.tv_nsec > rhs.ts.tv_nsec)
return true;
else
return false;
}
/**
* Compares a CTimeSpecs against this one
* for less than or equals to.
*/
bool operator<=(const CTimeSpec& rhs) const
{
if (*this < rhs)
return true;
else if (*this == rhs)
return true;
else
return false;
}
/**
* Compares a CTimeSpecs against this one
* for greater than or equals to.
*/
bool operator>=(const CTimeSpec& rhs) const
{
if (*this > rhs)
return true;
else if (*this == rhs)
return true;
else
return false;
}
private:
/**
* The internal data struct this class is wrapping.
*/
struct timespec ts;
/**
* Output operator for std::ostreams.
*/
friend std::ostream& operator<< (std::ostream& os, const CTimeSpec& ts)
{
os << "(" << ts.ts.tv_sec << " sec, " << ts.ts.tv_nsec << " nsec)";
return os;
}
/**
* Adds two CTimeSpecs and returns a new one which is the sum.
*/
friend CTimeSpec operator+ (const CTimeSpec& lhs, const CTimeSpec& rhs)
{
struct timespec sum;
sum.tv_sec = lhs.ts.tv_sec + rhs.ts.tv_sec;
sum.tv_nsec = lhs.ts.tv_nsec + rhs.ts.tv_nsec;
if (sum.tv_nsec >= NS_IN_SECOND){
sum.tv_sec++;
sum.tv_nsec -= NS_IN_SECOND;
}
return CTimeSpec(sum);
}
/**
* Subtracts two CTimeSpecs and returns a new one which is the
* difference.
*/
friend CTimeSpec operator- (const CTimeSpec& lhs, const CTimeSpec& rhs)
{
struct timespec difference;
difference.tv_sec = lhs.ts.tv_sec - rhs.ts.tv_sec;
difference.tv_nsec = lhs.ts.tv_nsec - rhs.ts.tv_nsec;
if (difference.tv_nsec < 0){
difference.tv_sec--;
difference.tv_nsec += NS_IN_SECOND;
}
return CTimeSpec(difference);
}
};
/*
* The struct timeval structure is not part of POSIX, however it is
* used a lot in Linux / BSD / *nix code, and the structure is very
* similar to the timespec structure. Define USING_TIMEVAL to get the
* corresponding APIs.
*/
#ifdef USING_TIMEVAL
/**
* Wrapper class for working with c style timeval structures.
*/
class CTimeVal
{
public:
/**
* Default null ctor.
*/
CTimeVal()
: tv {0, 0}
{}
/**
* ctor
* @param ms total milliseconds used to init the class.
*/
CTimeVal(unsigned int ms)
: tv {ms / MS_IN_SECOND, (ms % MS_IN_SECOND) * US_IN_MS}
{}
/**
* ctor
* @param t timeval structure used to init the class.
* This ctor guarantees that the structure is normalized.
*/
CTimeVal(struct timeval t)
{
while (t.tv_usec >= US_IN_SECOND) {
t.tv_sec++;
t.tv_usec -= US_IN_SECOND;
}
while (t.tv_usec < 0) {
t.tv_sec--;
t.tv_usec += US_IN_SECOND;
}
tv = t;
}
/**
* ctor
* @param sec seconds used to init the sec portion.
* @param usec microseconds used to init the usec portion.
* This ctor guarantees that the structure is normalized.
*/
CTimeVal(time_t sec, long usec)
{
while (usec >= US_IN_SECOND) {
sec++;
usec -= US_IN_SECOND;
}
while (usec < 0) {
sec--;
usec += US_IN_SECOND;
}
tv.tv_sec = sec;
tv.tv_usec = usec;
}
/**
* ctor - create a CTimeVal class from a timespec struct.
* @param t timeval structure used to init the class.
* This ctor guarantees that the structure is normalized.
*/
CTimeVal(struct timespec ts)
{
tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;
while (tv.tv_usec >= US_IN_SECOND) {
tv.tv_sec++;
tv.tv_usec -= US_IN_SECOND;
}
while (tv.tv_usec < 0) {
tv.tv_sec--;
tv.tv_usec += US_IN_SECOND;
}
}
/**
* ctor - create a CTimeVal class from a CTimeSpec class.
* @param s Instance of a CTimeSPec class.
*/
CTimeVal(CTimeSpec s)
: CTimeVal(s.c_timespec())
{}
/**
* Static factory returning a CTimeVal that represents "now"
* in wall clock time. See CLOCK_REALTIME.
*/
static CTimeVal Now()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return CTimeVal {ts};
}
/**
* Static factory returning a CTimeVal that represents "now"
* in monotonic time. See CLOCK_MONOTONIC.
*/
static CTimeVal NowMonotonic()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return CTimeVal {ts};
}
/**
* Static factory returning a CTimeVal that represents "now"
* in monotonic raw time. See CLOCK_MONOTONIC_RAW.
*/
static CTimeVal NowMonotonicRaw()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return CTimeVal {ts};
}
/**
* Utility function to return a copy of the internal
* timeval structure.
*/
struct timeval c_timeval()
{
return tv;
}
/**
* Adds a CTimeVal to this one.
* Guarantees the result is normalized.
*/
CTimeVal& operator+=(const CTimeVal& rhs)
{
tv.tv_sec += rhs.tv.tv_sec;
tv.tv_usec += rhs.tv.tv_usec;
if (tv.tv_usec >= US_IN_SECOND){
tv.tv_sec++;
tv.tv_usec -= US_IN_SECOND;
}
return *this;
}
/**
* Subtracts a CTimeVal to this one.
* Guarantees the result is normalized.
*/
CTimeVal& operator-=(const CTimeVal& rhs)
{
tv.tv_sec -= rhs.tv.tv_sec;
tv.tv_usec -= rhs.tv.tv_usec;
if (tv.tv_usec < 0){
tv.tv_sec--;
tv.tv_usec += US_IN_SECOND;
}
return *this;
}
/**
* Compares a CTimeVals against this one
* for non-equality.
*/
bool operator!=(const CTimeVal& rhs) const
{
if (tv.tv_sec != rhs.tv.tv_sec)
return true;
else if (tv.tv_usec != rhs.tv.tv_usec)
return true;
else
return false;
}
/**
* Compares a CTimeVals against this one
* for equality.
*/
bool operator==(const CTimeVal& rhs) const
{
return !(*this != rhs);
}
/**
* Compares a CTimeVals against this one
* for less than.
*/
bool operator<(const CTimeVal& rhs) const
{
if (tv.tv_sec < rhs.tv.tv_sec)
return true;
else if (tv.tv_usec < rhs.tv.tv_usec)
return true;
else
return false;
}
/**
* Compares a CTimeVals against this one
* for greater than.
*/
bool operator>(const CTimeVal& rhs) const
{
if (tv.tv_sec > rhs.tv.tv_sec)
return true;
else if (tv.tv_usec > rhs.tv.tv_usec)
return true;
else
return false;
}
/**
* Compares a CTimeVals against this one
* for less than or equals to.
*/
bool operator<=(const CTimeVal& rhs) const
{
if (*this < rhs)
return true;
else if (*this == rhs)
return true;
else
return false;
}
/**
* Compares a CTimeVals against this one
* for greater than or equals to.
*/
bool operator>=(const CTimeVal& rhs) const
{
if (*this > rhs)
return true;
else if (*this == rhs)
return true;
else
return false;
}
private:
/**
* The internal data struct this class is wrapping.
*/
struct timeval tv;
/**
* Output operator for std::ostreams.
*/
friend std::ostream& operator<< (std::ostream& os, const CTimeVal& tv)
{
os << "(" << tv.tv.tv_sec << " sec, " << tv.tv.tv_usec << " usec)";
return os;
}
/**
* Adds two CTimeVals and returns a new one which is the sum.
*/
friend CTimeVal operator+ (const CTimeVal& lhs, const CTimeVal& rhs)
{
struct timeval sum;
sum.tv_sec = lhs.tv.tv_sec + rhs.tv.tv_sec;
sum.tv_usec = lhs.tv.tv_usec + rhs.tv.tv_usec;
if (sum.tv_usec >= US_IN_SECOND){
sum.tv_sec++;
sum.tv_usec -= US_IN_SECOND;
}
return CTimeVal(sum);
}
/**
* Subtracts two CTimeVals and returns a new one which is the
* difference.
*/
friend CTimeVal operator- (const CTimeVal& lhs, const CTimeVal& rhs)
{
struct timeval difference;
difference.tv_sec = lhs.tv.tv_sec - rhs.tv.tv_sec;
difference.tv_usec = lhs.tv.tv_usec - rhs.tv.tv_usec;
if (difference.tv_usec < 0){
difference.tv_sec--;
difference.tv_usec += US_IN_SECOND;
}
return CTimeVal(difference);
}
};
#endif /* USING_TIMEVAL */
#endif