aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/include/Poco/Clock.h
blob: 43bb22f640666f2d9a148288aa102cf909d6be7b (plain) (blame)
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
//
// Clock.h
//
// Library: Foundation
// Package: DateTime
// Module:  Clock
//
// Definition of the Clock class.
//
// Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_Clock_INCLUDED
#define Foundation_Clock_INCLUDED


#include "Poco/Foundation.h"


namespace Poco {


class Foundation_API Clock
	/// A Clock stores a monotonic* clock value
	/// with (theoretical) microseconds resolution.
	/// Clocks can be compared with each other
	/// and simple arithmetics are supported.
	///
	/// [*] Note that Clock values are only monotonic if
	/// the operating system provides a monotonic clock.
	/// The monotonic() function can be used to check whether
	/// the system's clock is monotonic.
	///
	/// Monotonic Clock is available on Windows, Linux, OS X 
	/// and on POSIX platforms supporting clock_gettime() with CLOCK_MONOTONIC.	  
	///
	/// Clock values are relative to a system-dependent epoch time
	/// (usually the system's startup time) and have no relation
	/// to the time of day.
{
public:
	typedef Int64 ClockVal;
		/// Monotonic clock value in microsecond resolution.

	typedef Int64 ClockDiff;
		/// Difference between two ClockVal values in microseconds.

	static const ClockVal CLOCKVAL_MIN; /// Minimum clock value.
	static const ClockVal CLOCKVAL_MAX; /// Maximum clock value.

	Clock();
		/// Creates a Clock with the current system clock value.
		
	Clock(ClockVal tv);
		/// Creates a Clock from the given clock value.
		
	Clock(const Clock& other);
		/// Copy constructor.
		
	~Clock();
		/// Destroys the Clock.
		
	Clock& operator = (const Clock& other);
	Clock& operator = (ClockVal tv);
	
	void swap(Clock& clock);
		/// Swaps the Clock with another one.
	
	void update();
		/// Updates the Clock with the current system clock.

	bool operator == (const Clock& ts) const;
	bool operator != (const Clock& ts) const;
	bool operator >  (const Clock& ts) const;
	bool operator >= (const Clock& ts) const;
	bool operator <  (const Clock& ts) const;
	bool operator <= (const Clock& ts) const;
	
	Clock  operator +  (ClockDiff d) const;
	Clock  operator -  (ClockDiff d) const;
	ClockDiff operator - (const Clock& ts) const;
	Clock& operator += (ClockDiff d);
	Clock& operator -= (ClockDiff d);
	
	ClockVal microseconds() const;
		/// Returns the clock value expressed in microseconds
		/// since the system-specific epoch time (usually system
		/// startup).

	ClockVal raw() const;
		/// Returns the clock value expressed in microseconds
		/// since the system-specific epoch time (usually system
		/// startup).
		///
		/// Same as microseconds().
	
	ClockDiff elapsed() const;
		/// Returns the time elapsed since the time denoted by
		/// the Clock instance. Equivalent to Clock() - *this.
	
	bool isElapsed(ClockDiff interval) const;
		/// Returns true iff the given interval has passed
		/// since the time denoted by the Clock instance.
	
	static ClockDiff resolution();
		/// Returns the resolution in units per second.
		/// Since the Clock clas has microsecond resolution,
		/// the returned value is always 1000000.
		
	static ClockDiff accuracy();
		/// Returns the system's clock accuracy in microseconds.
		
	static bool monotonic();
		/// Returns true iff the system's clock is monotonic.

private:
	ClockVal _clock;
};


//
// inlines
//
inline bool Clock::operator == (const Clock& ts) const
{
	return _clock == ts._clock;
}


inline bool Clock::operator != (const Clock& ts) const
{
	return _clock != ts._clock;
}


inline bool Clock::operator >  (const Clock& ts) const
{
	return _clock > ts._clock;
}


inline bool Clock::operator >= (const Clock& ts) const
{
	return _clock >= ts._clock;
}


inline bool Clock::operator <  (const Clock& ts) const
{
	return _clock < ts._clock;
}


inline bool Clock::operator <= (const Clock& ts) const
{
	return _clock <= ts._clock;
}


inline Clock Clock::operator + (Clock::ClockDiff d) const
{
	return Clock(_clock + d);
}


inline Clock Clock::operator - (Clock::ClockDiff d) const
{
	return Clock(_clock - d);
}


inline Clock::ClockDiff Clock::operator - (const Clock& ts) const
{
	return _clock - ts._clock;
}


inline Clock& Clock::operator += (Clock::ClockDiff d)
{
	_clock += d;
	return *this;
}


inline Clock& Clock::operator -= (Clock::ClockDiff d)
{
	_clock -= d;
	return *this;
}


inline Clock::ClockVal Clock::microseconds() const
{
	return _clock;
}


inline Clock::ClockDiff Clock::elapsed() const
{
	Clock now;
	return now - *this;
}


inline bool Clock::isElapsed(Clock::ClockDiff interval) const
{
	Clock now;
	Clock::ClockDiff diff = now - *this;
	return diff >= interval;
}


inline Clock::ClockDiff Clock::resolution()
{
	return 1000000;
}


inline void swap(Clock& s1, Clock& s2)
{
	s1.swap(s2);
}


inline Clock::ClockVal Clock::raw() const
{
	return _clock;
}


} // namespace Poco


#endif // Foundation_Clock_INCLUDED