vernier 1.4.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ #ifndef TIMESTAMP_HH
2
+ #define TIMESTAMP_HH
3
+
4
+ #include <iostream>
5
+ #include <stdint.h>
6
+ #include <sys/time.h>
7
+ #include <unistd.h>
8
+
9
+ class TimeStamp {
10
+ static const uint64_t nanoseconds_per_second = 1000000000;
11
+ uint64_t value_ns;
12
+
13
+ TimeStamp(uint64_t value_ns) : value_ns(value_ns) {}
14
+
15
+ public:
16
+ TimeStamp() : value_ns(0) {}
17
+
18
+ static TimeStamp Now() {
19
+ struct timespec ts;
20
+ clock_gettime(CLOCK_MONOTONIC, &ts);
21
+ return TimeStamp(ts.tv_sec * nanoseconds_per_second + ts.tv_nsec);
22
+ }
23
+
24
+ static TimeStamp NowRealtime() {
25
+ struct timespec ts;
26
+ clock_gettime(CLOCK_REALTIME, &ts);
27
+ return TimeStamp(ts.tv_sec * nanoseconds_per_second + ts.tv_nsec);
28
+ }
29
+
30
+ static TimeStamp Zero() {
31
+ return TimeStamp(0);
32
+ }
33
+
34
+ // SleepUntil a specified timestamp
35
+ // Highly accurate manual sleep time
36
+ static void SleepUntil(const TimeStamp &target_time) {
37
+ if (target_time.zero()) return;
38
+ struct timespec ts = target_time.timespec();
39
+
40
+ int res;
41
+ do {
42
+ // do nothing until it's time :)
43
+ sleep(0);
44
+ } while (target_time > TimeStamp::Now());
45
+ }
46
+
47
+ static TimeStamp from_seconds(uint64_t s) {
48
+ return TimeStamp::from_milliseconds(s * 1000);
49
+ }
50
+
51
+ static TimeStamp from_milliseconds(uint64_t ms) {
52
+ return TimeStamp::from_microseconds(ms * 1000);
53
+ }
54
+
55
+ static TimeStamp from_microseconds(uint64_t us) {
56
+ return TimeStamp::from_nanoseconds(us * 1000);
57
+ }
58
+
59
+ static TimeStamp from_nanoseconds(uint64_t ns) {
60
+ return TimeStamp(ns);
61
+ }
62
+
63
+ TimeStamp operator-(const TimeStamp &other) const {
64
+ TimeStamp result = *this;
65
+ return result -= other;
66
+ }
67
+
68
+ TimeStamp &operator-=(const TimeStamp &other) {
69
+ if (value_ns > other.value_ns) {
70
+ value_ns = value_ns - other.value_ns;
71
+ } else {
72
+ // underflow
73
+ value_ns = 0;
74
+ }
75
+ return *this;
76
+ }
77
+
78
+ TimeStamp operator+(const TimeStamp &other) const {
79
+ TimeStamp result = *this;
80
+ return result += other;
81
+ }
82
+
83
+ TimeStamp &operator+=(const TimeStamp &other) {
84
+ uint64_t new_value = value_ns + other.value_ns;
85
+ value_ns = new_value;
86
+ return *this;
87
+ }
88
+
89
+ bool operator<(const TimeStamp &other) const {
90
+ return value_ns < other.value_ns;
91
+ }
92
+
93
+ bool operator<=(const TimeStamp &other) const {
94
+ return value_ns <= other.value_ns;
95
+ }
96
+
97
+ bool operator>(const TimeStamp &other) const {
98
+ return value_ns > other.value_ns;
99
+ }
100
+
101
+ bool operator>=(const TimeStamp &other) const {
102
+ return value_ns >= other.value_ns;
103
+ }
104
+
105
+ bool operator==(const TimeStamp &other) const {
106
+ return value_ns == other.value_ns;
107
+ }
108
+
109
+ bool operator!=(const TimeStamp &other) const {
110
+ return value_ns != other.value_ns;
111
+ }
112
+
113
+ uint64_t nanoseconds() const {
114
+ return value_ns;
115
+ }
116
+
117
+ uint64_t microseconds() const {
118
+ return value_ns / 1000;
119
+ }
120
+
121
+ bool zero() const {
122
+ return value_ns == 0;
123
+ }
124
+
125
+ struct timespec timespec() const {
126
+ struct timespec ts;
127
+ ts.tv_sec = nanoseconds() / nanoseconds_per_second;
128
+ ts.tv_nsec = (nanoseconds() % nanoseconds_per_second);
129
+ return ts;
130
+ }
131
+ };
132
+
133
+ inline std::ostream& operator<<(std::ostream& os, const TimeStamp& info) {
134
+ os << info.nanoseconds() << "ns";
135
+ return os;
136
+ }
137
+
138
+ #endif