1// duration.hpp --------------------------------------------------------------//
2
3// Copyright 2008 Howard Hinnant
4// Copyright 2008 Beman Dawes
5// Copyright 2009-2012 Vicente J. Botet Escriba
6
7// Distributed under the Boost Software License, Version 1.0.
8// See http://www.boost.org/LICENSE_1_0.txt
9
10/*
11
12This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
13Many thanks to Howard for making his code available under the Boost license.
14The original code was modified to conform to Boost conventions and to section
1520.9 Time utilities [time] of the C++ committee's working paper N2798.
16See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
17
18time2_demo contained this comment:
19
20 Much thanks to Andrei Alexandrescu,
21 Walter Brown,
22 Peter Dimov,
23 Jeff Garland,
24 Terry Golubiewski,
25 Daniel Krugler,
26 Anthony Williams.
27*/
28
29
30#ifndef BOOST_CHRONO_TIME_POINT_HPP
31#define BOOST_CHRONO_TIME_POINT_HPP
32
33#include <boost/chrono/duration.hpp>
34#include <iostream>
35
36#ifndef BOOST_CHRONO_HEADER_ONLY
37// this must occur after all of the includes and before any code appears:
38#include <boost/config/abi_prefix.hpp> // must be the last #include
39#endif
40
41//----------------------------------------------------------------------------//
42// //
43// 20.9 Time utilities [time] //
44// synopsis //
45// //
46//----------------------------------------------------------------------------//
47
48namespace boost {
49namespace chrono {
50
51 template <class Clock, class Duration = typename Clock::duration>
52 class time_point;
53
54
55} // namespace chrono
56
57
58// common_type trait specializations
59
60template <class Clock, class Duration1, class Duration2>
61 struct common_type<chrono::time_point<Clock, Duration1>,
62 chrono::time_point<Clock, Duration2> >;
63
64
65//----------------------------------------------------------------------------//
66// 20.9.2.3 Specializations of common_type [time.traits.specializations] //
67//----------------------------------------------------------------------------//
68
69
70template <class Clock, class Duration1, class Duration2>
71struct common_type<chrono::time_point<Clock, Duration1>,
72 chrono::time_point<Clock, Duration2> >
73{
74 typedef chrono::time_point<Clock,
75 typename common_type<Duration1, Duration2>::type> type;
76};
77
78
79
80namespace chrono {
81
82 // time_point arithmetic
83 template <class Clock, class Duration1, class Rep2, class Period2>
84 inline BOOST_CONSTEXPR
85 time_point<Clock,
86 typename common_type<Duration1, duration<Rep2, Period2> >::type>
87 operator+(
88 const time_point<Clock, Duration1>& lhs,
89 const duration<Rep2, Period2>& rhs);
90 template <class Rep1, class Period1, class Clock, class Duration2>
91 inline BOOST_CONSTEXPR
92 time_point<Clock,
93 typename common_type<duration<Rep1, Period1>, Duration2>::type>
94 operator+(
95 const duration<Rep1, Period1>& lhs,
96 const time_point<Clock, Duration2>& rhs);
97 template <class Clock, class Duration1, class Rep2, class Period2>
98 inline BOOST_CONSTEXPR
99 time_point<Clock,
100 typename common_type<Duration1, duration<Rep2, Period2> >::type>
101 operator-(
102 const time_point<Clock, Duration1>& lhs,
103 const duration<Rep2, Period2>& rhs);
104 template <class Clock, class Duration1, class Duration2>
105 inline BOOST_CONSTEXPR
106 typename common_type<Duration1, Duration2>::type
107 operator-(
108 const time_point<Clock, Duration1>& lhs,
109 const time_point<Clock,
110 Duration2>& rhs);
111
112 // time_point comparisons
113 template <class Clock, class Duration1, class Duration2>
114 inline BOOST_CONSTEXPR
115 bool operator==(
116 const time_point<Clock, Duration1>& lhs,
117 const time_point<Clock, Duration2>& rhs);
118 template <class Clock, class Duration1, class Duration2>
119 inline BOOST_CONSTEXPR
120 bool operator!=(
121 const time_point<Clock, Duration1>& lhs,
122 const time_point<Clock, Duration2>& rhs);
123 template <class Clock, class Duration1, class Duration2>
124 inline BOOST_CONSTEXPR
125 bool operator< (
126 const time_point<Clock, Duration1>& lhs,
127 const time_point<Clock, Duration2>& rhs);
128 template <class Clock, class Duration1, class Duration2>
129 inline BOOST_CONSTEXPR
130 bool operator<=(
131 const time_point<Clock, Duration1>& lhs,
132 const time_point<Clock, Duration2>& rhs);
133 template <class Clock, class Duration1, class Duration2>
134 inline BOOST_CONSTEXPR
135 bool operator> (
136 const time_point<Clock, Duration1>& lhs,
137 const time_point<Clock, Duration2>& rhs);
138 template <class Clock, class Duration1, class Duration2>
139 inline BOOST_CONSTEXPR
140 bool operator>=(
141 const time_point<Clock, Duration1>& lhs,
142 const time_point<Clock, Duration2>& rhs);
143
144 // time_point_cast
145 template <class ToDuration, class Clock, class Duration>
146 inline BOOST_CONSTEXPR
147 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
148
149//----------------------------------------------------------------------------//
150// //
151// 20.9.4 Class template time_point [time.point] //
152// //
153//----------------------------------------------------------------------------//
154
155 template <class Clock, class Duration>
156 class time_point
157 {
158 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::detail::is_duration<Duration>::value,
159 BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION, (Duration));
160 public:
161 typedef Clock clock;
162 typedef Duration duration;
163 typedef typename duration::rep rep;
164 typedef typename duration::period period;
165 typedef Duration difference_type;
166
167 private:
168 duration d_;
169
170 public:
171 BOOST_FORCEINLINE BOOST_CONSTEXPR
172 time_point() : d_(duration::zero())
173 {}
174 BOOST_FORCEINLINE BOOST_CONSTEXPR
175 explicit time_point(const duration& d)
176 : d_(d)
177 {}
178
179 // conversions
180 template <class Duration2>
181 BOOST_FORCEINLINE BOOST_CONSTEXPR
182 time_point(const time_point<clock, Duration2>& t
183 , typename boost::enable_if
184 <
185 boost::is_convertible<Duration2, duration>
186 >::type* = 0
187 )
188 : d_(t.time_since_epoch())
189 {
190 }
191 // observer
192
193 BOOST_CONSTEXPR
194 duration time_since_epoch() const
195 {
196 return d_;
197 }
198
199 // arithmetic
200
201#ifdef BOOST_CHRONO_EXTENSIONS
202 BOOST_CONSTEXPR
203 time_point operator+() const {return *this;}
204 BOOST_CONSTEXPR
205 time_point operator-() const {return time_point(-d_);}
206 time_point& operator++() {++d_; return *this;}
207 time_point operator++(int) {return time_point(d_++);}
208 time_point& operator--() {--d_; return *this;}
209 time_point operator--(int) {return time_point(d_--);}
210
211 time_point& operator+=(const rep& r) {d_ += duration(r); return *this;}
212 time_point& operator-=(const rep& r) {d_ -= duration(r); return *this;}
213
214#endif
215
216 time_point& operator+=(const duration& d) {d_ += d; return *this;}
217 time_point& operator-=(const duration& d) {d_ -= d; return *this;}
218
219 // special values
220
221 static BOOST_CHRONO_LIB_CONSTEXPR time_point
222 min BOOST_PREVENT_MACRO_SUBSTITUTION ()
223 {
224 return time_point((duration::min)());
225 }
226 static BOOST_CHRONO_LIB_CONSTEXPR time_point
227 max BOOST_PREVENT_MACRO_SUBSTITUTION ()
228 {
229 return time_point((duration::max)());
230 }
231 };
232
233//----------------------------------------------------------------------------//
234// 20.9.4.5 time_point non-member arithmetic [time.point.nonmember] //
235//----------------------------------------------------------------------------//
236
237 // time_point operator+(time_point x, duration y);
238
239 template <class Clock, class Duration1, class Rep2, class Period2>
240 inline BOOST_CONSTEXPR
241 time_point<Clock,
242 typename common_type<Duration1, duration<Rep2, Period2> >::type>
243 operator+(const time_point<Clock, Duration1>& lhs,
244 const duration<Rep2, Period2>& rhs)
245 {
246 typedef typename common_type<Duration1, duration<Rep2, Period2> >::type CDuration;
247 typedef time_point<
248 Clock,
249 CDuration
250 > TimeResult;
251 return TimeResult(lhs.time_since_epoch() + CDuration(rhs));
252 }
253
254 // time_point operator+(duration x, time_point y);
255
256 template <class Rep1, class Period1, class Clock, class Duration2>
257 inline BOOST_CONSTEXPR
258 time_point<Clock,
259 typename common_type<duration<Rep1, Period1>, Duration2>::type>
260 operator+(const duration<Rep1, Period1>& lhs,
261 const time_point<Clock, Duration2>& rhs)
262 {
263 return rhs + lhs;
264 }
265
266 // time_point operator-(time_point x, duration y);
267
268 template <class Clock, class Duration1, class Rep2, class Period2>
269 inline BOOST_CONSTEXPR
270 time_point<Clock,
271 typename common_type<Duration1, duration<Rep2, Period2> >::type>
272 operator-(const time_point<Clock, Duration1>& lhs,
273 const duration<Rep2, Period2>& rhs)
274 {
275 return lhs + (-rhs);
276 }
277
278 // duration operator-(time_point x, time_point y);
279
280 template <class Clock, class Duration1, class Duration2>
281 inline BOOST_CONSTEXPR
282 typename common_type<Duration1, Duration2>::type
283 operator-(const time_point<Clock, Duration1>& lhs,
284 const time_point<Clock, Duration2>& rhs)
285 {
286 return lhs.time_since_epoch() - rhs.time_since_epoch();
287 }
288
289//----------------------------------------------------------------------------//
290// 20.9.4.6 time_point comparisons [time.point.comparisons] //
291//----------------------------------------------------------------------------//
292
293 // time_point ==
294
295 template <class Clock, class Duration1, class Duration2>
296 inline BOOST_CONSTEXPR
297 bool
298 operator==(const time_point<Clock, Duration1>& lhs,
299 const time_point<Clock, Duration2>& rhs)
300 {
301 return lhs.time_since_epoch() == rhs.time_since_epoch();
302 }
303
304 // time_point !=
305
306 template <class Clock, class Duration1, class Duration2>
307 inline BOOST_CONSTEXPR
308 bool
309 operator!=(const time_point<Clock, Duration1>& lhs,
310 const time_point<Clock, Duration2>& rhs)
311 {
312 return !(lhs == rhs);
313 }
314
315 // time_point <
316
317 template <class Clock, class Duration1, class Duration2>
318 inline BOOST_CONSTEXPR
319 bool
320 operator<(const time_point<Clock, Duration1>& lhs,
321 const time_point<Clock, Duration2>& rhs)
322 {
323 return lhs.time_since_epoch() < rhs.time_since_epoch();
324 }
325
326 // time_point >
327
328 template <class Clock, class Duration1, class Duration2>
329 inline BOOST_CONSTEXPR
330 bool
331 operator>(const time_point<Clock, Duration1>& lhs,
332 const time_point<Clock, Duration2>& rhs)
333 {
334 return rhs < lhs;
335 }
336
337 // time_point <=
338
339 template <class Clock, class Duration1, class Duration2>
340 inline BOOST_CONSTEXPR
341 bool
342 operator<=(const time_point<Clock, Duration1>& lhs,
343 const time_point<Clock, Duration2>& rhs)
344 {
345 return !(rhs < lhs);
346 }
347
348 // time_point >=
349
350 template <class Clock, class Duration1, class Duration2>
351 inline BOOST_CONSTEXPR
352 bool
353 operator>=(const time_point<Clock, Duration1>& lhs,
354 const time_point<Clock, Duration2>& rhs)
355 {
356 return !(lhs < rhs);
357 }
358
359//----------------------------------------------------------------------------//
360// 20.9.4.7 time_point_cast [time.point.cast] //
361//----------------------------------------------------------------------------//
362
363 template <class ToDuration, class Clock, class Duration>
364 inline BOOST_CONSTEXPR
365 time_point<Clock, ToDuration>
366 time_point_cast(const time_point<Clock, Duration>& t)
367 {
368 return time_point<Clock, ToDuration>(
369 duration_cast<ToDuration>(t.time_since_epoch()));
370 }
371
372} // namespace chrono
373} // namespace boost
374
375#ifndef BOOST_CHRONO_HEADER_ONLY
376// the suffix header occurs after all of our code:
377#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
378#endif
379
380#endif // BOOST_CHRONO_TIME_POINT_HPP
381

source code of boost/boost/chrono/time_point.hpp