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>
35 #ifndef BOOST_CHRONO_HEADER_ONLY
36 // this must occur after all of the includes and before any code appears:
37 #include <boost/config/abi_prefix.hpp> // must be the last #include
40 //----------------------------------------------------------------------------//
42 // 20.9 Time utilities [time] //
45 //----------------------------------------------------------------------------//
50 template <class Clock, class Duration = typename Clock::duration>
57 // common_type trait specializations
59 template <class Clock, class Duration1, class Duration2>
60 struct common_type<chrono::time_point<Clock, Duration1>,
61 chrono::time_point<Clock, Duration2> >;
64 //----------------------------------------------------------------------------//
65 // 20.9.2.3 Specializations of common_type [time.traits.specializations] //
66 //----------------------------------------------------------------------------//
69 template <class Clock, class Duration1, class Duration2>
70 struct common_type<chrono::time_point<Clock, Duration1>,
71 chrono::time_point<Clock, Duration2> >
73 typedef chrono::time_point<Clock,
74 typename common_type<Duration1, Duration2>::type> type;
81 // time_point arithmetic
82 template <class Clock, class Duration1, class Rep2, class Period2>
83 inline BOOST_CONSTEXPR
85 typename common_type<Duration1, duration<Rep2, Period2> >::type>
87 const time_point<Clock, Duration1>& lhs,
88 const duration<Rep2, Period2>& rhs);
89 template <class Rep1, class Period1, class Clock, class Duration2>
90 inline BOOST_CONSTEXPR
92 typename common_type<duration<Rep1, Period1>, Duration2>::type>
94 const duration<Rep1, Period1>& lhs,
95 const time_point<Clock, Duration2>& rhs);
96 template <class Clock, class Duration1, class Rep2, class Period2>
97 inline BOOST_CONSTEXPR
99 typename common_type<Duration1, duration<Rep2, Period2> >::type>
101 const time_point<Clock, Duration1>& lhs,
102 const duration<Rep2, Period2>& rhs);
103 template <class Clock, class Duration1, class Duration2>
104 inline BOOST_CONSTEXPR
105 typename common_type<Duration1, Duration2>::type
107 const time_point<Clock, Duration1>& lhs,
108 const time_point<Clock,
111 // time_point comparisons
112 template <class Clock, class Duration1, class Duration2>
113 inline BOOST_CONSTEXPR
115 const time_point<Clock, Duration1>& lhs,
116 const time_point<Clock, Duration2>& rhs);
117 template <class Clock, class Duration1, class Duration2>
118 inline BOOST_CONSTEXPR
120 const time_point<Clock, Duration1>& lhs,
121 const time_point<Clock, Duration2>& rhs);
122 template <class Clock, class Duration1, class Duration2>
123 inline BOOST_CONSTEXPR
125 const time_point<Clock, Duration1>& lhs,
126 const time_point<Clock, Duration2>& rhs);
127 template <class Clock, class Duration1, class Duration2>
128 inline BOOST_CONSTEXPR
130 const time_point<Clock, Duration1>& lhs,
131 const time_point<Clock, Duration2>& rhs);
132 template <class Clock, class Duration1, class Duration2>
133 inline BOOST_CONSTEXPR
135 const time_point<Clock, Duration1>& lhs,
136 const time_point<Clock, Duration2>& rhs);
137 template <class Clock, class Duration1, class Duration2>
138 inline BOOST_CONSTEXPR
140 const time_point<Clock, Duration1>& lhs,
141 const time_point<Clock, Duration2>& rhs);
144 template <class ToDuration, class Clock, class Duration>
145 inline BOOST_CONSTEXPR
146 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
148 //----------------------------------------------------------------------------//
150 // 20.9.4 Class template time_point [time.point] //
152 //----------------------------------------------------------------------------//
154 template <class Clock, class Duration>
157 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::detail::is_duration<Duration>::value,
158 BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION, (Duration));
161 typedef Duration duration;
162 typedef typename duration::rep rep;
163 typedef typename duration::period period;
164 typedef Duration difference_type;
170 BOOST_FORCEINLINE BOOST_CONSTEXPR
171 time_point() : d_(duration::zero())
173 BOOST_FORCEINLINE BOOST_CONSTEXPR
174 explicit time_point(const duration& d)
179 template <class Duration2>
180 BOOST_FORCEINLINE BOOST_CONSTEXPR
181 time_point(const time_point<clock, Duration2>& t
182 , typename boost::enable_if
184 boost::is_convertible<Duration2, duration>
187 : d_(t.time_since_epoch())
193 duration time_since_epoch() const
200 #ifdef BOOST_CHRONO_EXTENSIONS
202 time_point operator+() const {return *this;}
204 time_point operator-() const {return time_point(-d_);}
205 time_point& operator++() {++d_; return *this;}
206 time_point operator++(int) {return time_point(d_++);}
207 time_point& operator--() {--d_; return *this;}
208 time_point operator--(int) {return time_point(d_--);}
210 time_point& operator+=(const rep& r) {d_ += duration(r); return *this;}
211 time_point& operator-=(const rep& r) {d_ -= duration(r); return *this;}
215 time_point& operator+=(const duration& d) {d_ += d; return *this;}
216 time_point& operator-=(const duration& d) {d_ -= d; return *this;}
220 static BOOST_CHRONO_LIB_CONSTEXPR time_point
221 min BOOST_PREVENT_MACRO_SUBSTITUTION ()
223 return time_point((duration::min)());
225 static BOOST_CHRONO_LIB_CONSTEXPR time_point
226 max BOOST_PREVENT_MACRO_SUBSTITUTION ()
228 return time_point((duration::max)());
232 //----------------------------------------------------------------------------//
233 // 20.9.4.5 time_point non-member arithmetic [time.point.nonmember] //
234 //----------------------------------------------------------------------------//
236 // time_point operator+(time_point x, duration y);
238 template <class Clock, class Duration1, class Rep2, class Period2>
239 inline BOOST_CONSTEXPR
241 typename common_type<Duration1, duration<Rep2, Period2> >::type>
242 operator+(const time_point<Clock, Duration1>& lhs,
243 const duration<Rep2, Period2>& rhs)
245 typedef typename common_type<Duration1, duration<Rep2, Period2> >::type CDuration;
250 return TimeResult(lhs.time_since_epoch() + CDuration(rhs));
253 // time_point operator+(duration x, time_point y);
255 template <class Rep1, class Period1, class Clock, class Duration2>
256 inline BOOST_CONSTEXPR
258 typename common_type<duration<Rep1, Period1>, Duration2>::type>
259 operator+(const duration<Rep1, Period1>& lhs,
260 const time_point<Clock, Duration2>& rhs)
265 // time_point operator-(time_point x, duration y);
267 template <class Clock, class Duration1, class Rep2, class Period2>
268 inline BOOST_CONSTEXPR
270 typename common_type<Duration1, duration<Rep2, Period2> >::type>
271 operator-(const time_point<Clock, Duration1>& lhs,
272 const duration<Rep2, Period2>& rhs)
277 // duration operator-(time_point x, time_point y);
279 template <class Clock, class Duration1, class Duration2>
280 inline BOOST_CONSTEXPR
281 typename common_type<Duration1, Duration2>::type
282 operator-(const time_point<Clock, Duration1>& lhs,
283 const time_point<Clock, Duration2>& rhs)
285 return lhs.time_since_epoch() - rhs.time_since_epoch();
288 //----------------------------------------------------------------------------//
289 // 20.9.4.6 time_point comparisons [time.point.comparisons] //
290 //----------------------------------------------------------------------------//
294 template <class Clock, class Duration1, class Duration2>
295 inline BOOST_CONSTEXPR
297 operator==(const time_point<Clock, Duration1>& lhs,
298 const time_point<Clock, Duration2>& rhs)
300 return lhs.time_since_epoch() == rhs.time_since_epoch();
305 template <class Clock, class Duration1, class Duration2>
306 inline BOOST_CONSTEXPR
308 operator!=(const time_point<Clock, Duration1>& lhs,
309 const time_point<Clock, Duration2>& rhs)
311 return !(lhs == rhs);
316 template <class Clock, class Duration1, class Duration2>
317 inline BOOST_CONSTEXPR
319 operator<(const time_point<Clock, Duration1>& lhs,
320 const time_point<Clock, Duration2>& rhs)
322 return lhs.time_since_epoch() < rhs.time_since_epoch();
327 template <class Clock, class Duration1, class Duration2>
328 inline BOOST_CONSTEXPR
330 operator>(const time_point<Clock, Duration1>& lhs,
331 const time_point<Clock, Duration2>& rhs)
338 template <class Clock, class Duration1, class Duration2>
339 inline BOOST_CONSTEXPR
341 operator<=(const time_point<Clock, Duration1>& lhs,
342 const time_point<Clock, Duration2>& rhs)
349 template <class Clock, class Duration1, class Duration2>
350 inline BOOST_CONSTEXPR
352 operator>=(const time_point<Clock, Duration1>& lhs,
353 const time_point<Clock, Duration2>& rhs)
358 //----------------------------------------------------------------------------//
359 // 20.9.4.7 time_point_cast [time.point.cast] //
360 //----------------------------------------------------------------------------//
362 template <class ToDuration, class Clock, class Duration>
363 inline BOOST_CONSTEXPR
364 time_point<Clock, ToDuration>
365 time_point_cast(const time_point<Clock, Duration>& t)
367 return time_point<Clock, ToDuration>(
368 duration_cast<ToDuration>(t.time_since_epoch()));
371 } // namespace chrono
374 #ifndef BOOST_CHRONO_HEADER_ONLY
375 // the suffix header occurs after all of our code:
376 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
379 #endif // BOOST_CHRONO_TIME_POINT_HPP