1 | // |
---|
2 | // Timespan.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Timespan.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: DateTime |
---|
8 | // Module: Timespan |
---|
9 | // |
---|
10 | // Definition of the Timespan class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Timespan_INCLUDED |
---|
40 | #define Foundation_Timespan_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Timestamp.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API Timespan |
---|
51 | /// A class that represents time spans up to microsecond resolution. |
---|
52 | { |
---|
53 | public: |
---|
54 | typedef Timestamp::TimeDiff TimeDiff; |
---|
55 | |
---|
56 | Timespan(); |
---|
57 | /// Creates a zero Timespan. |
---|
58 | |
---|
59 | Timespan(TimeDiff microseconds); |
---|
60 | /// Creates a Timespan. |
---|
61 | |
---|
62 | Timespan(long seconds, long microseconds); |
---|
63 | /// Creates a Timespan. Useful for creating |
---|
64 | /// a Timespan from a struct timeval. |
---|
65 | |
---|
66 | Timespan(int days, int hours, int minutes, int seconds, int microseconds); |
---|
67 | /// Creates a Timespan. |
---|
68 | |
---|
69 | Timespan(const Timespan& timespan); |
---|
70 | /// Creates a Timespan from another one. |
---|
71 | |
---|
72 | ~Timespan(); |
---|
73 | /// Destroys the Timespan. |
---|
74 | |
---|
75 | Timespan& operator = (const Timespan& timespan); |
---|
76 | /// Assignment operator. |
---|
77 | |
---|
78 | Timespan& operator = (TimeDiff microseconds); |
---|
79 | /// Assignment operator. |
---|
80 | |
---|
81 | Timespan& assign(int days, int hours, int minutes, int seconds, int microseconds); |
---|
82 | /// Assigns a new span. |
---|
83 | |
---|
84 | Timespan& assign(long seconds, long microseconds); |
---|
85 | /// Assigns a new span. Useful for assigning |
---|
86 | /// from a struct timeval. |
---|
87 | |
---|
88 | void swap(Timespan& timespan); |
---|
89 | /// Swaps the Timespan with another one. |
---|
90 | |
---|
91 | bool operator == (const Timespan& ts) const; |
---|
92 | bool operator != (const Timespan& ts) const; |
---|
93 | bool operator > (const Timespan& ts) const; |
---|
94 | bool operator >= (const Timespan& ts) const; |
---|
95 | bool operator < (const Timespan& ts) const; |
---|
96 | bool operator <= (const Timespan& ts) const; |
---|
97 | |
---|
98 | bool operator == (TimeDiff microseconds) const; |
---|
99 | bool operator != (TimeDiff microseconds) const; |
---|
100 | bool operator > (TimeDiff microseconds) const; |
---|
101 | bool operator >= (TimeDiff microseconds) const; |
---|
102 | bool operator < (TimeDiff microseconds) const; |
---|
103 | bool operator <= (TimeDiff microseconds) const; |
---|
104 | |
---|
105 | Timespan operator + (const Timespan& d) const; |
---|
106 | Timespan operator - (const Timespan& d) const; |
---|
107 | Timespan& operator += (const Timespan& d); |
---|
108 | Timespan& operator -= (const Timespan& d); |
---|
109 | |
---|
110 | Timespan operator + (TimeDiff microseconds) const; |
---|
111 | Timespan operator - (TimeDiff microseconds) const; |
---|
112 | Timespan& operator += (TimeDiff microseconds); |
---|
113 | Timespan& operator -= (TimeDiff microseconds); |
---|
114 | |
---|
115 | int days() const; |
---|
116 | /// Returns the number of days. |
---|
117 | |
---|
118 | int hours() const; |
---|
119 | /// Returns the number of hours (0 to 23). |
---|
120 | |
---|
121 | int totalHours() const; |
---|
122 | /// Returns the total number of hours. |
---|
123 | |
---|
124 | int minutes() const; |
---|
125 | /// Returns the number of minutes (0 to 59). |
---|
126 | |
---|
127 | int totalMinutes() const; |
---|
128 | /// Returns the total number of minutes. |
---|
129 | |
---|
130 | int seconds() const; |
---|
131 | /// Returns the number of seconds (0 to 59). |
---|
132 | |
---|
133 | int totalSeconds() const; |
---|
134 | /// Returns the total number of seconds. |
---|
135 | |
---|
136 | int milliseconds() const; |
---|
137 | /// Returns the number of milliseconds (0 to 999). |
---|
138 | |
---|
139 | TimeDiff totalMilliseconds() const; |
---|
140 | /// Returns the total number of milliseconds. |
---|
141 | |
---|
142 | int microseconds() const; |
---|
143 | /// Returns the fractions of a millisecond |
---|
144 | /// in microseconds (0 to 999). |
---|
145 | |
---|
146 | int useconds() const; |
---|
147 | /// Returns the fractions of a second |
---|
148 | /// in microseconds (0 to 999999). |
---|
149 | |
---|
150 | TimeDiff totalMicroseconds() const; |
---|
151 | /// Returns the total number of microseconds. |
---|
152 | |
---|
153 | static const TimeDiff MILLISECONDS; /// The number of microseconds in a millisecond. |
---|
154 | static const TimeDiff SECONDS; /// The number of microseconds in a second. |
---|
155 | static const TimeDiff MINUTES; /// The number of microseconds in a minute. |
---|
156 | static const TimeDiff HOURS; /// The number of microseconds in a hour. |
---|
157 | static const TimeDiff DAYS; /// The number of microseconds in a day. |
---|
158 | |
---|
159 | private: |
---|
160 | TimeDiff _span; |
---|
161 | }; |
---|
162 | |
---|
163 | |
---|
164 | // |
---|
165 | // inlines |
---|
166 | // |
---|
167 | inline int Timespan::days() const |
---|
168 | { |
---|
169 | return int(_span/DAYS); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | inline int Timespan::hours() const |
---|
174 | { |
---|
175 | return int((_span/HOURS) % 24); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | inline int Timespan::totalHours() const |
---|
180 | { |
---|
181 | return int(_span/HOURS); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | inline int Timespan::minutes() const |
---|
186 | { |
---|
187 | return int((_span/MINUTES) % 60); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | inline int Timespan::totalMinutes() const |
---|
192 | { |
---|
193 | return int(_span/MINUTES); |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | inline int Timespan::seconds() const |
---|
198 | { |
---|
199 | return int((_span/SECONDS) % 60); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | inline int Timespan::totalSeconds() const |
---|
204 | { |
---|
205 | return int(_span/SECONDS); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | inline int Timespan::milliseconds() const |
---|
210 | { |
---|
211 | return int((_span/MILLISECONDS) % 1000); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | inline Timespan::TimeDiff Timespan::totalMilliseconds() const |
---|
216 | { |
---|
217 | return _span/MILLISECONDS; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | inline int Timespan::microseconds() const |
---|
222 | { |
---|
223 | return int(_span % 1000); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | inline int Timespan::useconds() const |
---|
228 | { |
---|
229 | return int(_span % 1000000); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | inline Timespan::TimeDiff Timespan::totalMicroseconds() const |
---|
234 | { |
---|
235 | return _span; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | inline bool Timespan::operator == (const Timespan& ts) const |
---|
240 | { |
---|
241 | return _span == ts._span; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | inline bool Timespan::operator != (const Timespan& ts) const |
---|
246 | { |
---|
247 | return _span != ts._span; |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | inline bool Timespan::operator > (const Timespan& ts) const |
---|
252 | { |
---|
253 | return _span > ts._span; |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | inline bool Timespan::operator >= (const Timespan& ts) const |
---|
258 | { |
---|
259 | return _span >= ts._span; |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | inline bool Timespan::operator < (const Timespan& ts) const |
---|
264 | { |
---|
265 | return _span < ts._span; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | inline bool Timespan::operator <= (const Timespan& ts) const |
---|
270 | { |
---|
271 | return _span <= ts._span; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | inline bool Timespan::operator == (TimeDiff microseconds) const |
---|
276 | { |
---|
277 | return _span == microseconds; |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | inline bool Timespan::operator != (TimeDiff microseconds) const |
---|
282 | { |
---|
283 | return _span != microseconds; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | inline bool Timespan::operator > (TimeDiff microseconds) const |
---|
288 | { |
---|
289 | return _span > microseconds; |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | inline bool Timespan::operator >= (TimeDiff microseconds) const |
---|
294 | { |
---|
295 | return _span >= microseconds; |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | inline bool Timespan::operator < (TimeDiff microseconds) const |
---|
300 | { |
---|
301 | return _span < microseconds; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | inline bool Timespan::operator <= (TimeDiff microseconds) const |
---|
306 | { |
---|
307 | return _span <= microseconds; |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | inline void swap(Timespan& s1, Timespan& s2) |
---|
312 | { |
---|
313 | s1.swap(s2); |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | } // namespace Poco |
---|
318 | |
---|
319 | |
---|
320 | #endif // Foundation_Timespan_INCLUDED |
---|