-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTimeSpec.cpp
More file actions
636 lines (524 loc) Β· 20.8 KB
/
TimeSpec.cpp
File metadata and controls
636 lines (524 loc) Β· 20.8 KB
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
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// MLB Utility Library Module File
// ////////////////////////////////////////////////////////////////////////////
/*
File Name : %M%
File Version : %I%
Last Extracted : %D% %T%
Last Updated : %E% %U%
File Description : Implementation of the TimeSpec class.
Revision History : 1998-04-08 --- Creation.
Michael L. Brock
Copyright Michael L. Brock 1998 - 2018.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Required include files...
// ////////////////////////////////////////////////////////////////////////////
#include <Utility/TimeSupport.hpp>
#include <Utility/Utility_Exception.hpp>
// ////////////////////////////////////////////////////////////////////////////
// Note: TimeSpec::AddSecondsInternal() is implemented in Time.cpp.
namespace MLB {
namespace Utility {
namespace {
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Used to get high-granularity times under Windows...
// ////////////////////////////////////////////////////////////////////////////
#ifdef _Windows
typedef union {
FILETIME struct_data;
unsigned __int64 scalar_data;
} WIN32_FILETIME_64;
# ifdef __MINGW32__
# define WIN32_FILETIME_EPOCH_BIAS 116444736000000000LL
# else
# define WIN32_FILETIME_EPOCH_BIAS 116444736000000000i64
# endif // # ifdef __MINGW32__
#endif // #ifdef _Windows
// ////////////////////////////////////////////////////////////////////////////
} // Anonymous namespace
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::TimeSpec()
{
*this = TimeSpec::Now();
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::TimeSpec(time_t in_secs, long in_nsecs)
{
tv_sec = static_cast<long>(in_secs);
tv_nsec = in_nsecs;
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::TimeSpec(const timespec &in_time)
:timespec(in_time)
{
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::TimeSpec(const timeval &in_time)
{
tv_sec = in_time.tv_sec;
tv_nsec = in_time.tv_usec * 1000;
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::TimeSpec(const std::string &in_date)
:timespec(FromString(in_date))
{
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec::~TimeSpec()
{
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator < (const TimeSpec &other) const
{
return(Compare(this, &other) < 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator > (const TimeSpec &other) const
{
return(Compare(this, &other) > 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator <= (const TimeSpec &other) const
{
return(Compare(this, &other) <= 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator >= (const TimeSpec &other) const
{
return(Compare(this, &other) >= 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator == (const TimeSpec &other) const
{
return(Compare(this, &other) == 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::operator != (const TimeSpec &other) const
{
return(Compare(this, &other) != 0);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
int TimeSpec::Compare(const TimeSpec &other) const
{
return(Compare(this, &other));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::SetToNow()
{
Now().swap(*this);
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::SetToMinimumValue()
{
tv_sec = 0;
tv_nsec = 0;
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::SetToMaximumValue()
{
tv_sec = std::numeric_limits<long>::max();
tv_nsec = 999999999;
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
bool TimeSpec::IsZero() const
{
return((tv_sec == 0) && (tv_nsec == 0));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::AddMilliseconds(int msecs_to_add)
{
int secs_to_add = msecs_to_add / 1000;
int new_nsec = tv_nsec + ((msecs_to_add % 1000) * 1000000);
if (new_nsec >= 1000000000) {
secs_to_add += new_nsec / 1000000000;
new_nsec %= 1000000000;
}
else if (new_nsec < 0) {
secs_to_add += -1 + (new_nsec / 1000000000);
new_nsec = 1000000000 + (new_nsec % 1000000000);
}
if (secs_to_add) {
try {
AddSeconds(secs_to_add);
}
catch (const std::exception &except) {
Rethrow(except, "Unable to add the requested number of milliseconds "
"(" + AnyToString(msecs_to_add) + "): " +
std::string(except.what()));
}
}
tv_nsec = new_nsec;
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::AddMicroseconds(int usecs_to_add)
{
int secs_to_add = usecs_to_add / 1000000;
int new_nsec = tv_nsec + ((usecs_to_add % 1000000) * 1000);
if (new_nsec >= 1000000000) {
secs_to_add += new_nsec / 1000000000;
new_nsec %= 1000000000;
}
else if (new_nsec < 0) {
secs_to_add += -1 + (new_nsec / 1000000000);
new_nsec = 1000000000 + (new_nsec % 1000000000);
}
if (secs_to_add) {
try {
AddSeconds(secs_to_add);
}
catch (const std::exception &except) {
Rethrow(except, "Unable to add the requested number of microseconds "
"(" + AnyToString(usecs_to_add) + "): " +
std::string(except.what()));
}
}
tv_nsec = new_nsec;
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec &TimeSpec::AddNanoseconds(int nsecs_to_add)
{
int secs_to_add = nsecs_to_add / 1000000000;
int new_nsec = tv_nsec + (nsecs_to_add % 1000000000);
if (new_nsec >= 1000000000) {
secs_to_add += new_nsec / 1000000000;
new_nsec %= 1000000000;
}
else if (new_nsec < 0) {
secs_to_add += -1 + (new_nsec / 1000000000);
new_nsec = 1000000000 + (new_nsec % 1000000000);
}
if (secs_to_add) {
try {
AddSeconds(secs_to_add);
}
catch (const std::exception &except) {
Rethrow(except, "Unable to add the requested number of nanoseconds "
"(" + AnyToString(nsecs_to_add) + "): " +
std::string(except.what()));
}
}
tv_nsec = new_nsec;
return(*this);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
void TimeSpec::swap(TimeSpec &other)
{
std::swap(tv_sec, other.tv_sec);
std::swap(tv_nsec, other.tv_nsec);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::ToString(char *buffer, unsigned int max_length) const
{
return(FormatString(TimeTM::TimeUTC(tv_sec), buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string TimeSpec::ToString(unsigned int max_length) const
{
char buffer[Length_TimeSpec + 1];
return(ToString(buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string &TimeSpec::ToString(std::string &out_string,
unsigned int max_length) const
{
char buffer[Length_TimeSpec + 1];
return(out_string.assign(ToString(buffer, max_length)));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::ToStringLocal(char *buffer, unsigned int max_length) const
{
return(FormatString(TimeTM::TimeLocal(tv_sec), buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string TimeSpec::ToStringLocal(unsigned int max_length) const
{
char buffer[Length_TimeSpec + 1];
return(ToStringLocal(buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string &TimeSpec::ToStringLocal(std::string &out_string,
unsigned int max_length) const
{
char buffer[Length_TimeSpec + 1];
return(out_string.assign(ToStringLocal(buffer, max_length)));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::ToStringInterval(char *buffer, unsigned int max_length) const
{
return(FormatStringInterval(buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string TimeSpec::ToStringInterval(unsigned int max_length) const
{
char buffer[LengthInterval_TimeSpec + 1];
return(ToStringInterval(buffer, max_length));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::string &TimeSpec::ToStringInterval(std::string &out_string,
unsigned int max_length) const
{
char buffer[LengthInterval_TimeSpec + 1];
return(out_string.assign(ToStringInterval(buffer, max_length)));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeT TimeSpec::ToTimeT() const
{
return(TimeT(tv_sec));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeVal TimeSpec::ToTimeVal() const
{
return(TimeVal(tv_sec, tv_nsec / 1000));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
unsigned long long TimeSpec::ToTicks() const
{
return(ToNanoseconds());
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
unsigned long long TimeSpec::ToNanoseconds() const
{
return((static_cast<unsigned long long>(tv_sec) *
static_cast<unsigned long long>(1000000000)) +
static_cast<unsigned long long>(tv_nsec));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
double TimeSpec::GetDoubleEquivalent() const
{
return(static_cast<double>(tv_sec) +
(static_cast<double>(tv_nsec) / 1000000000.0));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
double TimeSpec::GetDoubleTicks() const
{
return((static_cast<double>(tv_sec) * 1000000000.0) +
static_cast<double>(tv_nsec));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
signed long long TimeSpec::GetDifferenceTicks(const TimeSpec &time_1,
const TimeSpec &time_2)
{
return(static_cast<signed long long>(time_1.ToNanoseconds()) -
static_cast<signed long long>(time_2.ToNanoseconds()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
double TimeSpec::GetDifferenceTicksDouble(const TimeSpec &time_1,
const TimeSpec &time_2)
{
return(time_1.GetDoubleTicks() - time_2.GetDoubleTicks());
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
double TimeSpec::GetDifferenceDouble(const TimeSpec &time_1,
const TimeSpec &time_2)
{
return(time_1.GetDoubleEquivalent() - time_2.GetDoubleEquivalent());
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::GetDifference(const TimeSpec &time_1, const TimeSpec &time_2)
{
signed long long tmp_value = GetDifferenceTicks(time_1, time_2);
return(TimeSpec(static_cast<long>(tmp_value / 1000000000LL),
static_cast<long>(tmp_value % 1000000000LL)));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::GetDifferenceAbs(const TimeSpec &time_1,
const TimeSpec &time_2)
{
return((time_1 >= time_2) ? GetDifference(time_1, time_2) :
GetDifference(time_2, time_1));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::FromString(const char *in_date)
{
TimeSpec out_date(0, 0);
ParseFromString(in_date, out_date.tv_sec, out_date.tv_nsec, 9);
return(out_date);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::FromString(const std::string &in_date)
{
return(TimeSpec::FromString(in_date.c_str()));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::FromNanoseconds(unsigned long long nsecs)
{
return(TimeSpec(static_cast<time_t>(nsecs /
static_cast<unsigned long long>(1000000000)),
static_cast<long>(nsecs % static_cast<unsigned long long>(1000000000))));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::Now()
{
struct timespec out_time;
#ifdef __MSDOS__
struct timeb struct_timeb;
ftime(&struct_timeb);
out_time.tv_sec = struct_timeb.time;
out_time.tv_nsec = ((long) struct_timeb.millitm) * 1000000L;
#elif _Windows
# ifdef __MINGW32__
WIN32_FILETIME_64 sys_time;
GetSystemTimeAsFileTime(&sys_time.struct_data);
out_time.tv_sec = ((long) ((sys_time.scalar_data -
WIN32_FILETIME_EPOCH_BIAS) / 10000000LL));
out_time.tv_nsec = ((long) ((sys_time.scalar_data * 100LL) %
1000000000LL));
# else
WIN32_FILETIME_64 sys_time;
GetSystemTimeAsFileTime(&sys_time.struct_data);
out_time.tv_sec = ((long) ((sys_time.scalar_data -
WIN32_FILETIME_EPOCH_BIAS) / 10000000i64));
out_time.tv_nsec = ((long) ((sys_time.scalar_data * 100i64) %
1000000000i64));
# endif // # ifdef __MINGW32__
#elif _POSIX_TIMERS
clock_gettime(CLOCK_REALTIME, &out_time);
#else
TimeVal tmp_timeval;
::gettimeofday(&tmp_timeval, NULL);
out_time.tv_sec = tmp_timeval.tv_sec;
out_time.tv_nsec = tmp_timeval.tv_usec * 1000L;
#endif // #ifdef __MSDOS__
return(TimeSpec(out_time));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
int TimeSpec::Compare(const TimeSpec *lhs, const TimeSpec *rhs)
{
return(
((int) (lhs->tv_sec > rhs->tv_sec) ? 1 :
((lhs->tv_sec < rhs->tv_sec) ? -1 :
((lhs->tv_nsec > rhs->tv_nsec) ? 1 :
((lhs->tv_nsec < rhs->tv_nsec) ? -1 : 0)))));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::GetMinimumValue()
{
TimeSpec tmp_datum(0, 0);
return(tmp_datum.SetToMinimumValue());
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
TimeSpec TimeSpec::GetMaximumValue()
{
TimeSpec tmp_datum(0, 0);
return(tmp_datum.SetToMaximumValue());
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::FormatString(const TimeTM &in_tm, char *buffer,
unsigned int max_length) const
{
if (max_length < Length_TimeSpec) {
char tmp_buffer[Length_TimeSpec + 1];
return(nstrcpy(buffer, FormatString(in_tm, tmp_buffer), max_length));
}
// This gets YYYY-MM-DD hh:mm:ss
in_tm.ToString(buffer);
return(AppendFractionalPortion(Length_TimeT, buffer));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::FormatStringInterval(char *buffer,
unsigned int max_length) const
{
if (max_length < LengthInterval_TimeSpec) {
char tmp_buffer[LengthInterval_TimeSpec + 1];
return(nstrcpy(buffer, FormatStringInterval(tmp_buffer), max_length));
}
// This gets dddddd hh:mm:ss
TimeT tmp_time_t(tv_sec);
tmp_time_t.ToStringInterval(buffer);
return(AppendFractionalPortion(LengthInterval_TimeT, buffer));
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
char *TimeSpec::AppendFractionalPortion(unsigned int idx, char *buffer) const
{
// Sanity insurance
unsigned long nsecs =
static_cast<unsigned long>(this->tv_nsec) % 1000000000L;
// Now append the period followed by the number of nanoseconds
buffer[idx++] = '.';
buffer[idx++] = ((char) ('0' + ((char) (nsecs / 100000000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 100000000L) / 10000000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 10000000L) / 1000000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 1000000L) / 100000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 100000L) / 10000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 10000L) / 1000L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 1000L) / 100L))));
buffer[idx++] = ((char) ('0' + ((char) ((nsecs % 100L) / 10L))));
buffer[idx++] = ((char) ('0' + ((char) (nsecs % 10L))));
buffer[idx++] = '\0';
return(buffer);
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
std::ostream & operator << (std::ostream &o_str, const TimeSpec &datum)
{
char tmp_buffer[Length_TimeSpec + 1];
o_str <<
datum.ToString(tmp_buffer, sizeof(tmp_buffer) - 1);
return(o_str);
}
// ////////////////////////////////////////////////////////////////////////////
} // namespace Utility
} // namespace MLB