|
8 | 8 |
|
9 | 9 | #include "awint.hpp"
|
10 | 10 |
|
11 |
| -constexpr long _Nsec_per_sec = 1000000000L; |
12 |
| -constexpr long _Nsec_per_msec = 1000000L; |
13 |
| -constexpr int _Msec_per_sec = 1000; |
14 |
| - |
15 |
| -static void _timespec64_normalize(_timespec64* xt) { // adjust so that 0 <= tv_nsec < 1 000 000 000 |
16 |
| - while (xt->tv_nsec < 0) { // normalize target time |
17 |
| - xt->tv_sec -= 1; |
18 |
| - xt->tv_nsec += _Nsec_per_sec; |
19 |
| - } |
20 |
| - while (_Nsec_per_sec <= xt->tv_nsec) { // normalize target time |
21 |
| - xt->tv_sec += 1; |
22 |
| - xt->tv_nsec -= _Nsec_per_sec; |
23 |
| - } |
24 |
| -} |
25 |
| - |
26 |
| -// return _timespec64 object holding difference between xt and now, treating negative difference as 0 |
27 |
| -static _timespec64 _timespec64_diff(const _timespec64* xt, const _timespec64* now) { |
28 |
| - _timespec64 diff = *xt; |
29 |
| - _timespec64_normalize(&diff); |
30 |
| - if (diff.tv_nsec < now->tv_nsec) { // avoid underflow |
31 |
| - diff.tv_sec -= now->tv_sec + 1; |
32 |
| - diff.tv_nsec += _Nsec_per_sec - now->tv_nsec; |
33 |
| - } else { // no underflow |
34 |
| - diff.tv_sec -= now->tv_sec; |
35 |
| - diff.tv_nsec -= now->tv_nsec; |
| 11 | +namespace { |
| 12 | + constexpr long _Nsec_per_sec = 1000000000L; |
| 13 | + constexpr long _Nsec_per_msec = 1000000L; |
| 14 | + constexpr int _Msec_per_sec = 1000; |
| 15 | + |
| 16 | + void _timespec64_normalize(_timespec64* xt) { // adjust so that 0 <= tv_nsec < 1 000 000 000 |
| 17 | + while (xt->tv_nsec < 0) { // normalize target time |
| 18 | + xt->tv_sec -= 1; |
| 19 | + xt->tv_nsec += _Nsec_per_sec; |
| 20 | + } |
| 21 | + while (_Nsec_per_sec <= xt->tv_nsec) { // normalize target time |
| 22 | + xt->tv_sec += 1; |
| 23 | + xt->tv_nsec -= _Nsec_per_sec; |
| 24 | + } |
36 | 25 | }
|
37 | 26 |
|
38 |
| - if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec <= 0)) { // time is zero |
39 |
| - diff.tv_sec = 0; |
40 |
| - diff.tv_nsec = 0; |
| 27 | + // return _timespec64 object holding difference between xt and now, treating negative difference as 0 |
| 28 | + _timespec64 _timespec64_diff(const _timespec64* xt, const _timespec64* now) { |
| 29 | + _timespec64 diff = *xt; |
| 30 | + _timespec64_normalize(&diff); |
| 31 | + if (diff.tv_nsec < now->tv_nsec) { // avoid underflow |
| 32 | + diff.tv_sec -= now->tv_sec + 1; |
| 33 | + diff.tv_nsec += _Nsec_per_sec - now->tv_nsec; |
| 34 | + } else { // no underflow |
| 35 | + diff.tv_sec -= now->tv_sec; |
| 36 | + diff.tv_nsec -= now->tv_nsec; |
| 37 | + } |
| 38 | + |
| 39 | + if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec <= 0)) { // time is zero |
| 40 | + diff.tv_sec = 0; |
| 41 | + diff.tv_nsec = 0; |
| 42 | + } |
| 43 | + return diff; |
41 | 44 | }
|
42 |
| - return diff; |
43 |
| -} |
44 |
| - |
45 |
| -constexpr long long _Epoch = 0x19DB1DED53E8000LL; |
46 |
| -constexpr long _Nsec100_per_sec = _Nsec_per_sec / 100; |
| 45 | +} // namespace |
47 | 46 |
|
48 | 47 | _EXTERN_C
|
49 | 48 |
|
50 | 49 | _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() noexcept {
|
51 | 50 | // get system time in 100-nanosecond intervals since the epoch
|
| 51 | + constexpr long long _Epoch = 0x19DB1DED53E8000LL; |
| 52 | + |
52 | 53 | FILETIME ft;
|
53 | 54 | __crtGetSystemTimePreciseAsFileTime(&ft);
|
54 | 55 | return ((static_cast<long long>(ft.dwHighDateTime)) << 32) + static_cast<long long>(ft.dwLowDateTime) - _Epoch;
|
55 | 56 | }
|
56 | 57 |
|
57 | 58 | // Used by several src files, but not dllexported.
|
58 | 59 | void _Timespec64_get_sys(_timespec64* xt) noexcept { // get system time with nanosecond resolution
|
| 60 | + constexpr long _Nsec100_per_sec = _Nsec_per_sec / 100; |
| 61 | + |
59 | 62 | unsigned long long now = _Xtime_get_ticks();
|
60 | 63 | xt->tv_sec = static_cast<__time64_t>(now / _Nsec100_per_sec);
|
61 | 64 | xt->tv_nsec = static_cast<long>(now % _Nsec100_per_sec) * 100;
|
|
0 commit comments