1//
2// Clock.h
3//
4// Library: Foundation
5// Package: DateTime
6// Module: Clock
7//
8// Definition of the Clock class.
9//
10// Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_Clock_INCLUDED
18#define Foundation_Clock_INCLUDED
19
20
21#include "Poco/Foundation.h"
22
23
24namespace Poco {
25
26
27class Foundation_API Clock
28 /// A Clock stores a monotonic* clock value
29 /// with (theoretical) microseconds resolution.
30 /// Clocks can be compared with each other
31 /// and simple arithmetic is supported.
32 ///
33 /// [*] Note that Clock values are only monotonic if
34 /// the operating system provides a monotonic clock.
35 /// The monotonic() function can be used to check whether
36 /// the system's clock is monotonic.
37 ///
38 /// Monotonic Clock is available on Windows, Linux, OS X
39 /// and on POSIX platforms supporting clock_gettime() with CLOCK_MONOTONIC.
40 ///
41 /// Clock values are relative to a system-dependent epoch time
42 /// (usually the system's startup time) and have no relation
43 /// to the time of day.
44{
45public:
46 typedef Int64 ClockVal;
47 /// Monotonic clock value in microsecond resolution.
48
49 typedef Int64 ClockDiff;
50 /// Difference between two ClockVal values in microseconds.
51
52 static const ClockVal CLOCKVAL_MIN; /// Minimum clock value.
53 static const ClockVal CLOCKVAL_MAX; /// Maximum clock value.
54
55 Clock();
56 /// Creates a Clock with the current system clock value.
57
58 Clock(ClockVal tv);
59 /// Creates a Clock from the given clock value.
60
61 Clock(const Clock& other);
62 /// Copy constructor.
63
64 ~Clock();
65 /// Destroys the Clock.
66
67 Clock& operator = (const Clock& other);
68 Clock& operator = (ClockVal tv);
69
70 void swap(Clock& clock);
71 /// Swaps the Clock with another one.
72
73 void update();
74 /// Updates the Clock with the current system clock.
75
76 bool operator == (const Clock& ts) const;
77 bool operator != (const Clock& ts) const;
78 bool operator > (const Clock& ts) const;
79 bool operator >= (const Clock& ts) const;
80 bool operator < (const Clock& ts) const;
81 bool operator <= (const Clock& ts) const;
82
83 Clock operator + (ClockDiff d) const;
84 Clock operator - (ClockDiff d) const;
85 ClockDiff operator - (const Clock& ts) const;
86 Clock& operator += (ClockDiff d);
87 Clock& operator -= (ClockDiff d);
88
89 ClockVal microseconds() const;
90 /// Returns the clock value expressed in microseconds
91 /// since the system-specific epoch time (usually system
92 /// startup).
93
94 ClockVal raw() const;
95 /// Returns the clock value expressed in microseconds
96 /// since the system-specific epoch time (usually system
97 /// startup).
98 ///
99 /// Same as microseconds().
100
101 ClockDiff elapsed() const;
102 /// Returns the time elapsed since the time denoted by
103 /// the Clock instance. Equivalent to Clock() - *this.
104
105 bool isElapsed(ClockDiff interval) const;
106 /// Returns true iff the given interval has passed
107 /// since the time denoted by the Clock instance.
108
109 static ClockDiff resolution();
110 /// Returns the resolution in units per second.
111 /// Since the Clock class has microsecond resolution,
112 /// the returned value is always 1000000.
113
114 static ClockDiff accuracy();
115 /// Returns the system's clock accuracy in microseconds.
116
117 static bool monotonic();
118 /// Returns true iff the system's clock is monotonic.
119
120private:
121 ClockVal _clock;
122};
123
124
125//
126// inlines
127//
128inline bool Clock::operator == (const Clock& ts) const
129{
130 return _clock == ts._clock;
131}
132
133
134inline bool Clock::operator != (const Clock& ts) const
135{
136 return _clock != ts._clock;
137}
138
139
140inline bool Clock::operator > (const Clock& ts) const
141{
142 return _clock > ts._clock;
143}
144
145
146inline bool Clock::operator >= (const Clock& ts) const
147{
148 return _clock >= ts._clock;
149}
150
151
152inline bool Clock::operator < (const Clock& ts) const
153{
154 return _clock < ts._clock;
155}
156
157
158inline bool Clock::operator <= (const Clock& ts) const
159{
160 return _clock <= ts._clock;
161}
162
163
164inline Clock Clock::operator + (Clock::ClockDiff d) const
165{
166 return Clock(_clock + d);
167}
168
169
170inline Clock Clock::operator - (Clock::ClockDiff d) const
171{
172 return Clock(_clock - d);
173}
174
175
176inline Clock::ClockDiff Clock::operator - (const Clock& ts) const
177{
178 return _clock - ts._clock;
179}
180
181
182inline Clock& Clock::operator += (Clock::ClockDiff d)
183{
184 _clock += d;
185 return *this;
186}
187
188
189inline Clock& Clock::operator -= (Clock::ClockDiff d)
190{
191 _clock -= d;
192 return *this;
193}
194
195
196inline Clock::ClockVal Clock::microseconds() const
197{
198 return _clock;
199}
200
201
202inline Clock::ClockDiff Clock::elapsed() const
203{
204 Clock now;
205 return now - *this;
206}
207
208
209inline bool Clock::isElapsed(Clock::ClockDiff interval) const
210{
211 Clock now;
212 Clock::ClockDiff diff = now - *this;
213 return diff >= interval;
214}
215
216
217inline Clock::ClockDiff Clock::resolution()
218{
219 return 1000000;
220}
221
222
223inline void swap(Clock& s1, Clock& s2)
224{
225 s1.swap(s2);
226}
227
228
229inline Clock::ClockVal Clock::raw() const
230{
231 return _clock;
232}
233
234
235} // namespace Poco
236
237
238#endif // Foundation_Clock_INCLUDED
239