-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimestamp
70 lines (58 loc) · 2.65 KB
/
timestamp
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_PLATFORM_TIMESTAMP_HPP
#define GTL_PLATFORM_TIMESTAMP_HPP
// Summary: Function to get a monotonically increasing timestamp.
#if ((defined(linux) || defined(__linux) || defined(__linux__)) || (__APPLE__))
#include <ctime>
#endif
#if (defined(_WIN32) || defined(_WIN64))
#if (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64))
#define _AMD64_
#elif (defined(i386) || defined(__i386) || defined(__i386__) || defined(__i386__) || defined(_M_IX86))
#define _X86_
#elif (defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT))
#define _ARM_
#endif
#pragma warning(push, 0)
#include <realtimeapiset.h>
#pragma comment( lib, "mincore.lib" )
#pragma warning(pop)
#endif
namespace gtl {
/// @brief Get a timestamp representing now.
/// @return A representation of the time now using a monotonically increasing clock.
/// @note The clock will continue incrementing when the computer is sleeping.
/// @warning The timestamp epoch is not related to the unix epoch.
inline long long int timestamp();
inline long long int timestamp() {
#if (defined(linux) || defined(__linux) || defined(__linux__))
struct timespec ts;
int return_code = clock_gettime(CLOCK_BOOTTIME, &ts);
if (return_code != 0) {
return 0;
}
return (1000000000ll * static_cast<long long int>(ts.tv_sec) + static_cast<long long int>(ts.tv_nsec));
#elif (defined(_WIN32) || defined(_WIN64))
unsigned long long int centinanoseconds;
QueryInterruptTimePrecise(¢inanoseconds);
return static_cast<long long int>(centinanoseconds) * 100ll;
#elif (defined(__APPLE__))
return static_cast<long long int>(clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW));
#else
#error "Failed to define timestamp function."
#endif
}
}
#endif // GTL_PLATFORM_TIMESTAMP_HPP