1 // duration.hpp --------------------------------------------------------------//
3 // Copyright 2008 Howard Hinnant
4 // Copyright 2008 Beman Dawes
5 // Copyright 2009-2012 Vicente J. Botet Escriba
7 // Distributed under the Boost Software License, Version 1.0.
8 // See http://www.boost.org/LICENSE_1_0.txt
12 This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
13 Many thanks to Howard for making his code available under the Boost license.
14 The original code was modified to conform to Boost conventions and to section
15 20.9 Time utilities [time] of the C++ committee's working paper N2798.
16 See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
18 time2_demo contained this comment:
20 Much thanks to Andrei Alexandrescu,
30 #ifndef BOOST_CHRONO_TIME_POINT_HPP
31 #define BOOST_CHRONO_TIME_POINT_HPP
33 #include <boost/chrono/duration.hpp>
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
41 //----------------------------------------------------------------------------//
43 // 20.9 Time utilities [time] //
46 //----------------------------------------------------------------------------//
51 template <class Clock, class Duration = typename Clock::duration>
58 // common_type trait specializations
60 template <class Clock, class Duration1, class Duration2>
61 struct common_type<chrono::time_point<Clock, Duration1>,
62 chrono::time_point<Clock, Duration2> >;
65 //----------------------------------------------------------------------------//
66 // 20.9.2.3 Specializations of common_type [time.traits.specializations] //
67 //----------------------------------------------------------------------------//
70 template <class Clock, class Duration1, class Duration2>
71 struct common_type<chrono::time_point<Clock, Duration1>,
72 chrono::time_point<Clock, Duration2> >
74 typedef chrono::time_point<Clock,
75 typename common_type<Duration1, Duration2>::type> type;
82 // time_point arithmetic
83 template <class Clock, class Duration1, class Rep2, class Period2>
84 inline BOOST_CONSTEXPR
86 typename common_type<Duration1, duration<Rep2, Period2> >::type>
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
93 typename common_type<duration<Rep1, Period1>, Duration2>::type>
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
100 typename common_type<Duration1, duration<Rep2, Period2> >::type>
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
108 const time_point<Clock, Duration1>& lhs,
109 const time_point<Clock,
112 // time_point comparisons
113 template <class Clock, class Duration1, class Duration2>
114 inline BOOST_CONSTEXPR
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
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
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
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
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
141 const time_point<Clock, Duration1>& lhs,
142 const time_point<Clock, Duration2>& rhs);
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);
149 //----------------------------------------------------------------------------//
151 // 20.9.4 Class template time_point [time.point] //
153 //----------------------------------------------------------------------------//
155 template <class Clock, class Duration>
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));
162 typedef Duration duration;
163 typedef typename duration::rep rep;
164 typedef typename duration::period period;
165 typedef Duration difference_type;
171 BOOST_FORCEINLINE BOOST_CONSTEXPR
172 time_point() : d_(duration::zero())
174 BOOST_FORCEINLINE BOOST_CONSTEXPR
175 explicit time_point(const duration& d)
180 template <class Duration2>
181 BOOST_FORCEINLINE BOOST_CONSTEXPR
182 time_point(const time_point<clock, Duration2>& t
183 , typename boost::enable_if
185 boost::is_convertible<Duration2, duration>
188 : d_(t.time_since_epoch())
194 duration time_since_epoch() const
201 #ifdef BOOST_CHRONO_EXTENSIONS
203 time_point operator+() const {return *this;}
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_--);}
211 time_point& operator+=(const rep& r) {d_ += duration(r); return *this;}
212 time_point& operator-=(const rep& r) {d_ -= duration(r); return *this;}
216 time_point& operator+=(const duration& d) {d_ += d; return *this;}
217 time_point& operator-=(const duration& d) {d_ -= d; return *this;}
221 static BOOST_CHRONO_LIB_CONSTEXPR time_point
222 min BOOST_PREVENT_MACRO_SUBSTITUTION ()
224 return time_point((duration::min)());
226 static BOOST_CHRONO_LIB_CONSTEXPR time_point
227 max BOOST_PREVENT_MACRO_SUBSTITUTION ()
229 return time_point((duration::max)());
233 //----------------------------------------------------------------------------//
234 // 20.9.4.5 time_point non-member arithmetic [time.point.nonmember] //
235 //----------------------------------------------------------------------------//
237 // time_point operator+(time_point x, duration y);
239 template <class Clock, class Duration1, class Rep2, class Period2>
240 inline BOOST_CONSTEXPR
242 typename common_type<Duration1, duration<Rep2, Period2> >::type>
243 operator+(const time_point<Clock, Duration1>& lhs,
244 const duration<Rep2, Period2>& rhs)
246 typedef typename common_type<Duration1, duration<Rep2, Period2> >::type CDuration;
251 return TimeResult(lhs.time_since_epoch() + CDuration(rhs));
254 // time_point operator+(duration x, time_point y);
256 template <class Rep1, class Period1, class Clock, class Duration2>
257 inline BOOST_CONSTEXPR
259 typename common_type<duration<Rep1, Period1>, Duration2>::type>
260 operator+(const duration<Rep1, Period1>& lhs,
261 const time_point<Clock, Duration2>& rhs)
266 // time_point operator-(time_point x, duration y);
268 template <class Clock, class Duration1, class Rep2, class Period2>
269 inline BOOST_CONSTEXPR
271 typename common_type<Duration1, duration<Rep2, Period2> >::type>
272 operator-(const time_point<Clock, Duration1>& lhs,
273 const duration<Rep2, Period2>& rhs)
278 // duration operator-(time_point x, time_point y);
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)
286 return lhs.time_since_epoch() - rhs.time_since_epoch();
289 //----------------------------------------------------------------------------//
290 // 20.9.4.6 time_point comparisons [time.point.comparisons] //
291 //----------------------------------------------------------------------------//
295 template <class Clock, class Duration1, class Duration2>
296 inline BOOST_CONSTEXPR
298 operator==(const time_point<Clock, Duration1>& lhs,
299 const time_point<Clock, Duration2>& rhs)
301 return lhs.time_since_epoch() == rhs.time_since_epoch();
306 template <class Clock, class Duration1, class Duration2>
307 inline BOOST_CONSTEXPR
309 operator!=(const time_point<Clock, Duration1>& lhs,
310 const time_point<Clock, Duration2>& rhs)
312 return !(lhs == rhs);
317 template <class Clock, class Duration1, class Duration2>
318 inline BOOST_CONSTEXPR
320 operator<(const time_point<Clock, Duration1>& lhs,
321 const time_point<Clock, Duration2>& rhs)
323 return lhs.time_since_epoch() < rhs.time_since_epoch();
328 template <class Clock, class Duration1, class Duration2>
329 inline BOOST_CONSTEXPR
331 operator>(const time_point<Clock, Duration1>& lhs,
332 const time_point<Clock, Duration2>& rhs)
339 template <class Clock, class Duration1, class Duration2>
340 inline BOOST_CONSTEXPR
342 operator<=(const time_point<Clock, Duration1>& lhs,
343 const time_point<Clock, Duration2>& rhs)
350 template <class Clock, class Duration1, class Duration2>
351 inline BOOST_CONSTEXPR
353 operator>=(const time_point<Clock, Duration1>& lhs,
354 const time_point<Clock, Duration2>& rhs)
359 //----------------------------------------------------------------------------//
360 // 20.9.4.7 time_point_cast [time.point.cast] //
361 //----------------------------------------------------------------------------//
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)
368 return time_point<Clock, ToDuration>(
369 duration_cast<ToDuration>(t.time_since_epoch()));
372 } // namespace chrono
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
380 #endif // BOOST_CHRONO_TIME_POINT_HPP