xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/std/chrono (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino// <chrono> -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino// Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4*e4b17023SJohn Marino//
5*e4b17023SJohn Marino// This file is part of the GNU ISO C++ Library.  This library is free
6*e4b17023SJohn Marino// software; you can redistribute it and/or modify it under the
7*e4b17023SJohn Marino// terms of the GNU General Public License as published by the
8*e4b17023SJohn Marino// Free Software Foundation; either version 3, or (at your option)
9*e4b17023SJohn Marino// any later version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino// This library is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*e4b17023SJohn Marino// GNU General Public License for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino// Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino// permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino// 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino// You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino// a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*e4b17023SJohn Marino// <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino/** @file include/chrono
26*e4b17023SJohn Marino *  This is a Standard C++ Library header.
27*e4b17023SJohn Marino */
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino#ifndef _GLIBCXX_CHRONO
30*e4b17023SJohn Marino#define _GLIBCXX_CHRONO 1
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino#pragma GCC system_header
33*e4b17023SJohn Marino
34*e4b17023SJohn Marino#ifndef __GXX_EXPERIMENTAL_CXX0X__
35*e4b17023SJohn Marino# include <bits/c++0x_warning.h>
36*e4b17023SJohn Marino#else
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino#include <ratio>
39*e4b17023SJohn Marino#include <type_traits>
40*e4b17023SJohn Marino#include <limits>
41*e4b17023SJohn Marino#include <ctime>
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino#ifdef _GLIBCXX_USE_C99_STDINT_TR1
44*e4b17023SJohn Marino
45*e4b17023SJohn Marinonamespace std _GLIBCXX_VISIBILITY(default)
46*e4b17023SJohn Marino{
47*e4b17023SJohn Marino  /**
48*e4b17023SJohn Marino   * @defgroup chrono Time
49*e4b17023SJohn Marino   * @ingroup utilities
50*e4b17023SJohn Marino   *
51*e4b17023SJohn Marino   * Classes and functions for time.
52*e4b17023SJohn Marino   * @{
53*e4b17023SJohn Marino   */
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino  /** @namespace std::chrono
56*e4b17023SJohn Marino   *  @brief ISO C++ 0x entities sub namespace for time and date.
57*e4b17023SJohn Marino   */
58*e4b17023SJohn Marino  namespace chrono
59*e4b17023SJohn Marino  {
60*e4b17023SJohn Marino  _GLIBCXX_BEGIN_NAMESPACE_VERSION
61*e4b17023SJohn Marino
62*e4b17023SJohn Marino    template<typename _Rep, typename _Period = ratio<1>>
63*e4b17023SJohn Marino      struct duration;
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino    template<typename _Clock, typename _Dur = typename _Clock::duration>
66*e4b17023SJohn Marino      struct time_point;
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino  _GLIBCXX_END_NAMESPACE_VERSION
69*e4b17023SJohn Marino  }
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino_GLIBCXX_BEGIN_NAMESPACE_VERSION
72*e4b17023SJohn Marino  // 20.8.2.3 specialization of common_type (for duration)
73*e4b17023SJohn Marino  template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
74*e4b17023SJohn Marino    struct common_type<chrono::duration<_Rep1, _Period1>,
75*e4b17023SJohn Marino		       chrono::duration<_Rep2, _Period2>>
76*e4b17023SJohn Marino    {
77*e4b17023SJohn Marino    private:
78*e4b17023SJohn Marino      typedef __static_gcd<_Period1::num, _Period2::num> 	__gcd_num;
79*e4b17023SJohn Marino      typedef __static_gcd<_Period1::den, _Period2::den> 	__gcd_den;
80*e4b17023SJohn Marino      typedef typename common_type<_Rep1, _Rep2>::type		__cr;
81*e4b17023SJohn Marino      typedef ratio<__gcd_num::value,
82*e4b17023SJohn Marino		    (_Period1::den / __gcd_den::value) * _Period2::den> __r;
83*e4b17023SJohn Marino
84*e4b17023SJohn Marino    public:
85*e4b17023SJohn Marino      typedef chrono::duration<__cr, __r> 			type;
86*e4b17023SJohn Marino    };
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino  // 20.8.2.3 specialization of common_type (for time_point)
89*e4b17023SJohn Marino  template<typename _Clock, typename _Dur1, typename _Dur2>
90*e4b17023SJohn Marino    struct common_type<chrono::time_point<_Clock, _Dur1>,
91*e4b17023SJohn Marino		       chrono::time_point<_Clock, _Dur2>>
92*e4b17023SJohn Marino    {
93*e4b17023SJohn Marino    private:
94*e4b17023SJohn Marino      typedef typename common_type<_Dur1, _Dur2>::type 		__ct;
95*e4b17023SJohn Marino
96*e4b17023SJohn Marino    public:
97*e4b17023SJohn Marino      typedef chrono::time_point<_Clock, __ct> 			type;
98*e4b17023SJohn Marino    };
99*e4b17023SJohn Marino_GLIBCXX_END_NAMESPACE_VERSION
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino  namespace chrono
102*e4b17023SJohn Marino  {
103*e4b17023SJohn Marino  _GLIBCXX_BEGIN_NAMESPACE_VERSION
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino    // Primary template for duration_cast impl.
106*e4b17023SJohn Marino    template<typename _ToDur, typename _CF, typename _CR,
107*e4b17023SJohn Marino	     bool _NumIsOne = false, bool _DenIsOne = false>
108*e4b17023SJohn Marino      struct __duration_cast_impl
109*e4b17023SJohn Marino      {
110*e4b17023SJohn Marino	template<typename _Rep, typename _Period>
111*e4b17023SJohn Marino	  static constexpr _ToDur
112*e4b17023SJohn Marino	  __cast(const duration<_Rep, _Period>& __d)
113*e4b17023SJohn Marino	  {
114*e4b17023SJohn Marino	    typedef typename _ToDur::rep			__to_rep;
115*e4b17023SJohn Marino	    return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
116*e4b17023SJohn Marino	      * static_cast<_CR>(_CF::num)
117*e4b17023SJohn Marino	      / static_cast<_CR>(_CF::den)));
118*e4b17023SJohn Marino	  }
119*e4b17023SJohn Marino      };
120*e4b17023SJohn Marino
121*e4b17023SJohn Marino    template<typename _ToDur, typename _CF, typename _CR>
122*e4b17023SJohn Marino      struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
123*e4b17023SJohn Marino      {
124*e4b17023SJohn Marino	template<typename _Rep, typename _Period>
125*e4b17023SJohn Marino	  static constexpr _ToDur
126*e4b17023SJohn Marino	  __cast(const duration<_Rep, _Period>& __d)
127*e4b17023SJohn Marino	  {
128*e4b17023SJohn Marino	    typedef typename _ToDur::rep			__to_rep;
129*e4b17023SJohn Marino	    return _ToDur(static_cast<__to_rep>(__d.count()));
130*e4b17023SJohn Marino	  }
131*e4b17023SJohn Marino      };
132*e4b17023SJohn Marino
133*e4b17023SJohn Marino    template<typename _ToDur, typename _CF, typename _CR>
134*e4b17023SJohn Marino      struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
135*e4b17023SJohn Marino      {
136*e4b17023SJohn Marino	template<typename _Rep, typename _Period>
137*e4b17023SJohn Marino	  static constexpr _ToDur
138*e4b17023SJohn Marino	  __cast(const duration<_Rep, _Period>& __d)
139*e4b17023SJohn Marino	  {
140*e4b17023SJohn Marino	    typedef typename _ToDur::rep			__to_rep;
141*e4b17023SJohn Marino	    return _ToDur(static_cast<__to_rep>(
142*e4b17023SJohn Marino	      static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
143*e4b17023SJohn Marino	  }
144*e4b17023SJohn Marino      };
145*e4b17023SJohn Marino
146*e4b17023SJohn Marino    template<typename _ToDur, typename _CF, typename _CR>
147*e4b17023SJohn Marino      struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
148*e4b17023SJohn Marino      {
149*e4b17023SJohn Marino	template<typename _Rep, typename _Period>
150*e4b17023SJohn Marino	  static constexpr _ToDur
151*e4b17023SJohn Marino	  __cast(const duration<_Rep, _Period>& __d)
152*e4b17023SJohn Marino	  {
153*e4b17023SJohn Marino	    typedef typename _ToDur::rep			__to_rep;
154*e4b17023SJohn Marino	    return _ToDur(static_cast<__to_rep>(
155*e4b17023SJohn Marino	      static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
156*e4b17023SJohn Marino	  }
157*e4b17023SJohn Marino      };
158*e4b17023SJohn Marino
159*e4b17023SJohn Marino    template<typename _Tp>
160*e4b17023SJohn Marino      struct __is_duration
161*e4b17023SJohn Marino      : std::false_type
162*e4b17023SJohn Marino      { };
163*e4b17023SJohn Marino
164*e4b17023SJohn Marino    template<typename _Rep, typename _Period>
165*e4b17023SJohn Marino      struct __is_duration<duration<_Rep, _Period>>
166*e4b17023SJohn Marino      : std::true_type
167*e4b17023SJohn Marino      { };
168*e4b17023SJohn Marino
169*e4b17023SJohn Marino    /// duration_cast
170*e4b17023SJohn Marino    template<typename _ToDur, typename _Rep, typename _Period>
171*e4b17023SJohn Marino      constexpr typename enable_if<__is_duration<_ToDur>::value,
172*e4b17023SJohn Marino				   _ToDur>::type
173*e4b17023SJohn Marino      duration_cast(const duration<_Rep, _Period>& __d)
174*e4b17023SJohn Marino      {
175*e4b17023SJohn Marino	typedef typename _ToDur::period				__to_period;
176*e4b17023SJohn Marino	typedef typename _ToDur::rep				__to_rep;
177*e4b17023SJohn Marino	typedef ratio_divide<_Period, __to_period> 		__r_div;
178*e4b17023SJohn Marino	typedef typename __r_div::type 				__cf;
179*e4b17023SJohn Marino	typedef typename common_type<__to_rep, _Rep, intmax_t>::type
180*e4b17023SJohn Marino	  							__cr;
181*e4b17023SJohn Marino	typedef  __duration_cast_impl<_ToDur, __cf, __cr,
182*e4b17023SJohn Marino				      __cf::num == 1, __cf::den == 1> __dc;
183*e4b17023SJohn Marino	return __dc::__cast(__d);
184*e4b17023SJohn Marino      }
185*e4b17023SJohn Marino
186*e4b17023SJohn Marino    /// treat_as_floating_point
187*e4b17023SJohn Marino    template<typename _Rep>
188*e4b17023SJohn Marino      struct treat_as_floating_point
189*e4b17023SJohn Marino      : is_floating_point<_Rep>
190*e4b17023SJohn Marino      { };
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino    /// duration_values
193*e4b17023SJohn Marino    template<typename _Rep>
194*e4b17023SJohn Marino      struct duration_values
195*e4b17023SJohn Marino      {
196*e4b17023SJohn Marino	static constexpr _Rep
197*e4b17023SJohn Marino	zero()
198*e4b17023SJohn Marino	{ return _Rep(0); }
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino	static constexpr _Rep
201*e4b17023SJohn Marino	max()
202*e4b17023SJohn Marino	{ return numeric_limits<_Rep>::max(); }
203*e4b17023SJohn Marino
204*e4b17023SJohn Marino	static constexpr _Rep
205*e4b17023SJohn Marino	min()
206*e4b17023SJohn Marino	{ return numeric_limits<_Rep>::lowest(); }
207*e4b17023SJohn Marino      };
208*e4b17023SJohn Marino
209*e4b17023SJohn Marino    template<typename T>
210*e4b17023SJohn Marino      struct __is_ratio
211*e4b17023SJohn Marino      : std::false_type
212*e4b17023SJohn Marino      { };
213*e4b17023SJohn Marino
214*e4b17023SJohn Marino    template<intmax_t _Num, intmax_t _Den>
215*e4b17023SJohn Marino      struct __is_ratio<ratio<_Num, _Den>>
216*e4b17023SJohn Marino      : std::true_type
217*e4b17023SJohn Marino      { };
218*e4b17023SJohn Marino
219*e4b17023SJohn Marino    /// duration
220*e4b17023SJohn Marino    template<typename _Rep, typename _Period>
221*e4b17023SJohn Marino      struct duration
222*e4b17023SJohn Marino      {
223*e4b17023SJohn Marino	typedef _Rep						rep;
224*e4b17023SJohn Marino	typedef _Period 					period;
225*e4b17023SJohn Marino
226*e4b17023SJohn Marino	static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
227*e4b17023SJohn Marino	static_assert(__is_ratio<_Period>::value,
228*e4b17023SJohn Marino		      "period must be a specialization of ratio");
229*e4b17023SJohn Marino	static_assert(_Period::num > 0, "period must be positive");
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino	// 20.8.3.1 construction / copy / destroy
232*e4b17023SJohn Marino	constexpr duration() : __r() { }
233*e4b17023SJohn Marino
234*e4b17023SJohn Marino	constexpr duration(const duration&) = default;
235*e4b17023SJohn Marino
236*e4b17023SJohn Marino	template<typename _Rep2, typename = typename
237*e4b17023SJohn Marino	       enable_if<is_convertible<_Rep2, rep>::value
238*e4b17023SJohn Marino			 && (treat_as_floating_point<rep>::value
239*e4b17023SJohn Marino			     || !treat_as_floating_point<_Rep2>::value)>::type>
240*e4b17023SJohn Marino	  constexpr explicit duration(const _Rep2& __rep)
241*e4b17023SJohn Marino	  : __r(static_cast<rep>(__rep)) { }
242*e4b17023SJohn Marino
243*e4b17023SJohn Marino	template<typename _Rep2, typename _Period2, typename = typename
244*e4b17023SJohn Marino	       enable_if<treat_as_floating_point<rep>::value
245*e4b17023SJohn Marino			 || (ratio_divide<_Period2, period>::type::den == 1
246*e4b17023SJohn Marino			     && !treat_as_floating_point<_Rep2>::value)>::type>
247*e4b17023SJohn Marino	  constexpr duration(const duration<_Rep2, _Period2>& __d)
248*e4b17023SJohn Marino	  : __r(duration_cast<duration>(__d).count()) { }
249*e4b17023SJohn Marino
250*e4b17023SJohn Marino	~duration() = default;
251*e4b17023SJohn Marino	duration& operator=(const duration&) = default;
252*e4b17023SJohn Marino
253*e4b17023SJohn Marino	// 20.8.3.2 observer
254*e4b17023SJohn Marino	constexpr rep
255*e4b17023SJohn Marino	count() const
256*e4b17023SJohn Marino	{ return __r; }
257*e4b17023SJohn Marino
258*e4b17023SJohn Marino	// 20.8.3.3 arithmetic
259*e4b17023SJohn Marino	constexpr duration
260*e4b17023SJohn Marino	operator+() const
261*e4b17023SJohn Marino	{ return *this; }
262*e4b17023SJohn Marino
263*e4b17023SJohn Marino	constexpr duration
264*e4b17023SJohn Marino	operator-() const
265*e4b17023SJohn Marino	{ return duration(-__r); }
266*e4b17023SJohn Marino
267*e4b17023SJohn Marino	duration&
268*e4b17023SJohn Marino	operator++()
269*e4b17023SJohn Marino	{
270*e4b17023SJohn Marino	  ++__r;
271*e4b17023SJohn Marino	  return *this;
272*e4b17023SJohn Marino	}
273*e4b17023SJohn Marino
274*e4b17023SJohn Marino	duration
275*e4b17023SJohn Marino	operator++(int)
276*e4b17023SJohn Marino	{ return duration(__r++); }
277*e4b17023SJohn Marino
278*e4b17023SJohn Marino	duration&
279*e4b17023SJohn Marino	operator--()
280*e4b17023SJohn Marino	{
281*e4b17023SJohn Marino	  --__r;
282*e4b17023SJohn Marino	  return *this;
283*e4b17023SJohn Marino	}
284*e4b17023SJohn Marino
285*e4b17023SJohn Marino	duration
286*e4b17023SJohn Marino	operator--(int)
287*e4b17023SJohn Marino	{ return duration(__r--); }
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino	duration&
290*e4b17023SJohn Marino	operator+=(const duration& __d)
291*e4b17023SJohn Marino	{
292*e4b17023SJohn Marino	  __r += __d.count();
293*e4b17023SJohn Marino	  return *this;
294*e4b17023SJohn Marino	}
295*e4b17023SJohn Marino
296*e4b17023SJohn Marino	duration&
297*e4b17023SJohn Marino	operator-=(const duration& __d)
298*e4b17023SJohn Marino	{
299*e4b17023SJohn Marino	  __r -= __d.count();
300*e4b17023SJohn Marino	  return *this;
301*e4b17023SJohn Marino	}
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino	duration&
304*e4b17023SJohn Marino	operator*=(const rep& __rhs)
305*e4b17023SJohn Marino	{
306*e4b17023SJohn Marino	  __r *= __rhs;
307*e4b17023SJohn Marino	  return *this;
308*e4b17023SJohn Marino	}
309*e4b17023SJohn Marino
310*e4b17023SJohn Marino	duration&
311*e4b17023SJohn Marino	operator/=(const rep& __rhs)
312*e4b17023SJohn Marino	{
313*e4b17023SJohn Marino	  __r /= __rhs;
314*e4b17023SJohn Marino	  return *this;
315*e4b17023SJohn Marino	}
316*e4b17023SJohn Marino
317*e4b17023SJohn Marino	// DR 934.
318*e4b17023SJohn Marino	template<typename _Rep2 = rep>
319*e4b17023SJohn Marino	  typename enable_if<!treat_as_floating_point<_Rep2>::value,
320*e4b17023SJohn Marino			     duration&>::type
321*e4b17023SJohn Marino	  operator%=(const rep& __rhs)
322*e4b17023SJohn Marino	  {
323*e4b17023SJohn Marino	    __r %= __rhs;
324*e4b17023SJohn Marino	    return *this;
325*e4b17023SJohn Marino	  }
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino	template<typename _Rep2 = rep>
328*e4b17023SJohn Marino	  typename enable_if<!treat_as_floating_point<_Rep2>::value,
329*e4b17023SJohn Marino			     duration&>::type
330*e4b17023SJohn Marino	  operator%=(const duration& __d)
331*e4b17023SJohn Marino	  {
332*e4b17023SJohn Marino	    __r %= __d.count();
333*e4b17023SJohn Marino	    return *this;
334*e4b17023SJohn Marino	  }
335*e4b17023SJohn Marino
336*e4b17023SJohn Marino	// 20.8.3.4 special values
337*e4b17023SJohn Marino	static constexpr duration
338*e4b17023SJohn Marino	zero()
339*e4b17023SJohn Marino	{ return duration(duration_values<rep>::zero()); }
340*e4b17023SJohn Marino
341*e4b17023SJohn Marino	static constexpr duration
342*e4b17023SJohn Marino	min()
343*e4b17023SJohn Marino	{ return duration(duration_values<rep>::min()); }
344*e4b17023SJohn Marino
345*e4b17023SJohn Marino	static constexpr duration
346*e4b17023SJohn Marino	max()
347*e4b17023SJohn Marino	{ return duration(duration_values<rep>::max()); }
348*e4b17023SJohn Marino
349*e4b17023SJohn Marino      private:
350*e4b17023SJohn Marino	rep __r;
351*e4b17023SJohn Marino      };
352*e4b17023SJohn Marino
353*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
354*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
355*e4b17023SJohn Marino      constexpr typename common_type<duration<_Rep1, _Period1>,
356*e4b17023SJohn Marino				     duration<_Rep2, _Period2>>::type
357*e4b17023SJohn Marino      operator+(const duration<_Rep1, _Period1>& __lhs,
358*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
359*e4b17023SJohn Marino      {
360*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
361*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
362*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__cd;
363*e4b17023SJohn Marino	return __cd(__cd(__lhs).count() + __cd(__rhs).count());
364*e4b17023SJohn Marino      }
365*e4b17023SJohn Marino
366*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
367*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
368*e4b17023SJohn Marino      constexpr typename common_type<duration<_Rep1, _Period1>,
369*e4b17023SJohn Marino				     duration<_Rep2, _Period2>>::type
370*e4b17023SJohn Marino      operator-(const duration<_Rep1, _Period1>& __lhs,
371*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
372*e4b17023SJohn Marino      {
373*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
374*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
375*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__cd;
376*e4b17023SJohn Marino	return __cd(__cd(__lhs).count() - __cd(__rhs).count());
377*e4b17023SJohn Marino      }
378*e4b17023SJohn Marino
379*e4b17023SJohn Marino    template<typename _Rep1, typename _Rep2, bool =
380*e4b17023SJohn Marino	     is_convertible<_Rep2,
381*e4b17023SJohn Marino			    typename common_type<_Rep1, _Rep2>::type>::value>
382*e4b17023SJohn Marino      struct __common_rep_type { };
383*e4b17023SJohn Marino
384*e4b17023SJohn Marino    template<typename _Rep1, typename _Rep2>
385*e4b17023SJohn Marino      struct __common_rep_type<_Rep1, _Rep2, true>
386*e4b17023SJohn Marino      { typedef typename common_type<_Rep1, _Rep2>::type type; };
387*e4b17023SJohn Marino
388*e4b17023SJohn Marino    template<typename _Rep1, typename _Period, typename _Rep2>
389*e4b17023SJohn Marino      constexpr
390*e4b17023SJohn Marino      duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
391*e4b17023SJohn Marino      operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
392*e4b17023SJohn Marino      {
393*e4b17023SJohn Marino	typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
394*e4b17023SJohn Marino	  __cd;
395*e4b17023SJohn Marino	return __cd(__cd(__d).count() * __s);
396*e4b17023SJohn Marino      }
397*e4b17023SJohn Marino
398*e4b17023SJohn Marino    template<typename _Rep1, typename _Rep2, typename _Period>
399*e4b17023SJohn Marino      constexpr
400*e4b17023SJohn Marino      duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
401*e4b17023SJohn Marino      operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
402*e4b17023SJohn Marino      { return __d * __s; }
403*e4b17023SJohn Marino
404*e4b17023SJohn Marino    template<typename _Rep1, typename _Period, typename _Rep2>
405*e4b17023SJohn Marino      constexpr duration<typename __common_rep_type<_Rep1, typename
406*e4b17023SJohn Marino	enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
407*e4b17023SJohn Marino      operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
408*e4b17023SJohn Marino      {
409*e4b17023SJohn Marino	typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
410*e4b17023SJohn Marino	  __cd;
411*e4b17023SJohn Marino	return __cd(__cd(__d).count() / __s);
412*e4b17023SJohn Marino      }
413*e4b17023SJohn Marino
414*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
415*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
416*e4b17023SJohn Marino      constexpr typename common_type<_Rep1, _Rep2>::type
417*e4b17023SJohn Marino      operator/(const duration<_Rep1, _Period1>& __lhs,
418*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
419*e4b17023SJohn Marino      {
420*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
421*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
422*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__cd;
423*e4b17023SJohn Marino	return __cd(__lhs).count() / __cd(__rhs).count();
424*e4b17023SJohn Marino      }
425*e4b17023SJohn Marino
426*e4b17023SJohn Marino    // DR 934.
427*e4b17023SJohn Marino    template<typename _Rep1, typename _Period, typename _Rep2>
428*e4b17023SJohn Marino      constexpr duration<typename __common_rep_type<_Rep1, typename
429*e4b17023SJohn Marino	enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
430*e4b17023SJohn Marino      operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
431*e4b17023SJohn Marino      {
432*e4b17023SJohn Marino	typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
433*e4b17023SJohn Marino	  __cd;
434*e4b17023SJohn Marino	return __cd(__cd(__d).count() % __s);
435*e4b17023SJohn Marino      }
436*e4b17023SJohn Marino
437*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
438*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
439*e4b17023SJohn Marino      constexpr typename common_type<duration<_Rep1, _Period1>,
440*e4b17023SJohn Marino				     duration<_Rep2, _Period2>>::type
441*e4b17023SJohn Marino      operator%(const duration<_Rep1, _Period1>& __lhs,
442*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
443*e4b17023SJohn Marino      {
444*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
445*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
446*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__cd;
447*e4b17023SJohn Marino	return __cd(__cd(__lhs).count() % __cd(__rhs).count());
448*e4b17023SJohn Marino      }
449*e4b17023SJohn Marino
450*e4b17023SJohn Marino    // comparisons
451*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
452*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
453*e4b17023SJohn Marino      constexpr bool
454*e4b17023SJohn Marino      operator==(const duration<_Rep1, _Period1>& __lhs,
455*e4b17023SJohn Marino		 const duration<_Rep2, _Period2>& __rhs)
456*e4b17023SJohn Marino      {
457*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
458*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
459*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__ct;
460*e4b17023SJohn Marino	return __ct(__lhs).count() == __ct(__rhs).count();
461*e4b17023SJohn Marino      }
462*e4b17023SJohn Marino
463*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
464*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
465*e4b17023SJohn Marino      constexpr bool
466*e4b17023SJohn Marino      operator<(const duration<_Rep1, _Period1>& __lhs,
467*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
468*e4b17023SJohn Marino      {
469*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
470*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
471*e4b17023SJohn Marino	typedef typename common_type<__dur1,__dur2>::type	__ct;
472*e4b17023SJohn Marino	return __ct(__lhs).count() < __ct(__rhs).count();
473*e4b17023SJohn Marino      }
474*e4b17023SJohn Marino
475*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
476*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
477*e4b17023SJohn Marino      constexpr bool
478*e4b17023SJohn Marino      operator!=(const duration<_Rep1, _Period1>& __lhs,
479*e4b17023SJohn Marino		 const duration<_Rep2, _Period2>& __rhs)
480*e4b17023SJohn Marino      { return !(__lhs == __rhs); }
481*e4b17023SJohn Marino
482*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
483*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
484*e4b17023SJohn Marino      constexpr bool
485*e4b17023SJohn Marino      operator<=(const duration<_Rep1, _Period1>& __lhs,
486*e4b17023SJohn Marino		 const duration<_Rep2, _Period2>& __rhs)
487*e4b17023SJohn Marino      { return !(__rhs < __lhs); }
488*e4b17023SJohn Marino
489*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
490*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
491*e4b17023SJohn Marino      constexpr bool
492*e4b17023SJohn Marino      operator>(const duration<_Rep1, _Period1>& __lhs,
493*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
494*e4b17023SJohn Marino      { return __rhs < __lhs; }
495*e4b17023SJohn Marino
496*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
497*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
498*e4b17023SJohn Marino      constexpr bool
499*e4b17023SJohn Marino      operator>=(const duration<_Rep1, _Period1>& __lhs,
500*e4b17023SJohn Marino		 const duration<_Rep2, _Period2>& __rhs)
501*e4b17023SJohn Marino      { return !(__lhs < __rhs); }
502*e4b17023SJohn Marino
503*e4b17023SJohn Marino    /// nanoseconds
504*e4b17023SJohn Marino    typedef duration<int64_t, nano> 	nanoseconds;
505*e4b17023SJohn Marino
506*e4b17023SJohn Marino    /// microseconds
507*e4b17023SJohn Marino    typedef duration<int64_t, micro> 	microseconds;
508*e4b17023SJohn Marino
509*e4b17023SJohn Marino    /// milliseconds
510*e4b17023SJohn Marino    typedef duration<int64_t, milli> 	milliseconds;
511*e4b17023SJohn Marino
512*e4b17023SJohn Marino    /// seconds
513*e4b17023SJohn Marino    typedef duration<int64_t> 		seconds;
514*e4b17023SJohn Marino
515*e4b17023SJohn Marino    /// minutes
516*e4b17023SJohn Marino    typedef duration<int, ratio< 60>> 	minutes;
517*e4b17023SJohn Marino
518*e4b17023SJohn Marino    /// hours
519*e4b17023SJohn Marino    typedef duration<int, ratio<3600>> 	hours;
520*e4b17023SJohn Marino
521*e4b17023SJohn Marino    /// time_point
522*e4b17023SJohn Marino    template<typename _Clock, typename _Dur>
523*e4b17023SJohn Marino      struct time_point
524*e4b17023SJohn Marino      {
525*e4b17023SJohn Marino	typedef _Clock			  			clock;
526*e4b17023SJohn Marino	typedef _Dur		  				duration;
527*e4b17023SJohn Marino	typedef typename duration::rep	  			rep;
528*e4b17023SJohn Marino	typedef typename duration::period			period;
529*e4b17023SJohn Marino
530*e4b17023SJohn Marino	constexpr time_point() : __d(duration::zero())
531*e4b17023SJohn Marino	{ }
532*e4b17023SJohn Marino
533*e4b17023SJohn Marino	constexpr explicit time_point(const duration& __dur)
534*e4b17023SJohn Marino	: __d(__dur)
535*e4b17023SJohn Marino	{ }
536*e4b17023SJohn Marino
537*e4b17023SJohn Marino	// conversions
538*e4b17023SJohn Marino	template<typename _Dur2>
539*e4b17023SJohn Marino	  constexpr time_point(const time_point<clock, _Dur2>& __t)
540*e4b17023SJohn Marino	  : __d(__t.time_since_epoch())
541*e4b17023SJohn Marino	  { }
542*e4b17023SJohn Marino
543*e4b17023SJohn Marino	// observer
544*e4b17023SJohn Marino	constexpr duration
545*e4b17023SJohn Marino	time_since_epoch() const
546*e4b17023SJohn Marino	{ return __d; }
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino	// arithmetic
549*e4b17023SJohn Marino	time_point&
550*e4b17023SJohn Marino	operator+=(const duration& __dur)
551*e4b17023SJohn Marino	{
552*e4b17023SJohn Marino	  __d += __dur;
553*e4b17023SJohn Marino	  return *this;
554*e4b17023SJohn Marino	}
555*e4b17023SJohn Marino
556*e4b17023SJohn Marino	time_point&
557*e4b17023SJohn Marino	operator-=(const duration& __dur)
558*e4b17023SJohn Marino	{
559*e4b17023SJohn Marino	  __d -= __dur;
560*e4b17023SJohn Marino	  return *this;
561*e4b17023SJohn Marino	}
562*e4b17023SJohn Marino
563*e4b17023SJohn Marino	// special values
564*e4b17023SJohn Marino	static constexpr time_point
565*e4b17023SJohn Marino	min()
566*e4b17023SJohn Marino	{ return time_point(duration::min()); }
567*e4b17023SJohn Marino
568*e4b17023SJohn Marino	static constexpr time_point
569*e4b17023SJohn Marino	max()
570*e4b17023SJohn Marino	{ return time_point(duration::max()); }
571*e4b17023SJohn Marino
572*e4b17023SJohn Marino      private:
573*e4b17023SJohn Marino	duration __d;
574*e4b17023SJohn Marino      };
575*e4b17023SJohn Marino
576*e4b17023SJohn Marino    /// time_point_cast
577*e4b17023SJohn Marino    template<typename _ToDur, typename _Clock, typename _Dur>
578*e4b17023SJohn Marino      constexpr typename enable_if<__is_duration<_ToDur>::value,
579*e4b17023SJohn Marino				   time_point<_Clock, _ToDur>>::type
580*e4b17023SJohn Marino      time_point_cast(const time_point<_Clock, _Dur>& __t)
581*e4b17023SJohn Marino      {
582*e4b17023SJohn Marino	typedef time_point<_Clock, _ToDur> 			__time_point;
583*e4b17023SJohn Marino	return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
584*e4b17023SJohn Marino      }
585*e4b17023SJohn Marino
586*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1,
587*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
588*e4b17023SJohn Marino      constexpr time_point<_Clock,
589*e4b17023SJohn Marino	typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
590*e4b17023SJohn Marino      operator+(const time_point<_Clock, _Dur1>& __lhs,
591*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
592*e4b17023SJohn Marino      {
593*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
594*e4b17023SJohn Marino	typedef typename common_type<_Dur1,__dur2>::type	__ct;
595*e4b17023SJohn Marino	typedef time_point<_Clock, __ct> 			__time_point;
596*e4b17023SJohn Marino	return __time_point(__lhs.time_since_epoch() + __rhs);
597*e4b17023SJohn Marino      }
598*e4b17023SJohn Marino
599*e4b17023SJohn Marino    template<typename _Rep1, typename _Period1,
600*e4b17023SJohn Marino	     typename _Clock, typename _Dur2>
601*e4b17023SJohn Marino      constexpr time_point<_Clock,
602*e4b17023SJohn Marino	typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
603*e4b17023SJohn Marino      operator+(const duration<_Rep1, _Period1>& __lhs,
604*e4b17023SJohn Marino		const time_point<_Clock, _Dur2>& __rhs)
605*e4b17023SJohn Marino      {
606*e4b17023SJohn Marino	typedef duration<_Rep1, _Period1>			__dur1;
607*e4b17023SJohn Marino	typedef typename common_type<__dur1,_Dur2>::type	__ct;
608*e4b17023SJohn Marino	typedef time_point<_Clock, __ct> 			__time_point;
609*e4b17023SJohn Marino	return __time_point(__rhs.time_since_epoch() + __lhs);
610*e4b17023SJohn Marino      }
611*e4b17023SJohn Marino
612*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1,
613*e4b17023SJohn Marino	     typename _Rep2, typename _Period2>
614*e4b17023SJohn Marino      constexpr time_point<_Clock,
615*e4b17023SJohn Marino	typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
616*e4b17023SJohn Marino      operator-(const time_point<_Clock, _Dur1>& __lhs,
617*e4b17023SJohn Marino		const duration<_Rep2, _Period2>& __rhs)
618*e4b17023SJohn Marino      {
619*e4b17023SJohn Marino	typedef duration<_Rep2, _Period2>			__dur2;
620*e4b17023SJohn Marino	typedef typename common_type<_Dur1,__dur2>::type	__ct;
621*e4b17023SJohn Marino	typedef time_point<_Clock, __ct> 			__time_point;
622*e4b17023SJohn Marino	return __time_point(__lhs.time_since_epoch() -__rhs);
623*e4b17023SJohn Marino      }
624*e4b17023SJohn Marino
625*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
626*e4b17023SJohn Marino      constexpr typename common_type<_Dur1, _Dur2>::type
627*e4b17023SJohn Marino      operator-(const time_point<_Clock, _Dur1>& __lhs,
628*e4b17023SJohn Marino		const time_point<_Clock, _Dur2>& __rhs)
629*e4b17023SJohn Marino      { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
630*e4b17023SJohn Marino
631*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
632*e4b17023SJohn Marino      constexpr bool
633*e4b17023SJohn Marino      operator==(const time_point<_Clock, _Dur1>& __lhs,
634*e4b17023SJohn Marino		 const time_point<_Clock, _Dur2>& __rhs)
635*e4b17023SJohn Marino      { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
636*e4b17023SJohn Marino
637*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
638*e4b17023SJohn Marino      constexpr bool
639*e4b17023SJohn Marino      operator!=(const time_point<_Clock, _Dur1>& __lhs,
640*e4b17023SJohn Marino		 const time_point<_Clock, _Dur2>& __rhs)
641*e4b17023SJohn Marino      { return !(__lhs == __rhs); }
642*e4b17023SJohn Marino
643*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
644*e4b17023SJohn Marino      constexpr bool
645*e4b17023SJohn Marino      operator<(const time_point<_Clock, _Dur1>& __lhs,
646*e4b17023SJohn Marino		const time_point<_Clock, _Dur2>& __rhs)
647*e4b17023SJohn Marino      { return  __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
648*e4b17023SJohn Marino
649*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
650*e4b17023SJohn Marino      constexpr bool
651*e4b17023SJohn Marino      operator<=(const time_point<_Clock, _Dur1>& __lhs,
652*e4b17023SJohn Marino		 const time_point<_Clock, _Dur2>& __rhs)
653*e4b17023SJohn Marino      { return !(__rhs < __lhs); }
654*e4b17023SJohn Marino
655*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
656*e4b17023SJohn Marino      constexpr bool
657*e4b17023SJohn Marino      operator>(const time_point<_Clock, _Dur1>& __lhs,
658*e4b17023SJohn Marino		const time_point<_Clock, _Dur2>& __rhs)
659*e4b17023SJohn Marino      { return __rhs < __lhs; }
660*e4b17023SJohn Marino
661*e4b17023SJohn Marino    template<typename _Clock, typename _Dur1, typename _Dur2>
662*e4b17023SJohn Marino      constexpr bool
663*e4b17023SJohn Marino      operator>=(const time_point<_Clock, _Dur1>& __lhs,
664*e4b17023SJohn Marino		 const time_point<_Clock, _Dur2>& __rhs)
665*e4b17023SJohn Marino      { return !(__lhs < __rhs); }
666*e4b17023SJohn Marino
667*e4b17023SJohn Marino    /// system_clock
668*e4b17023SJohn Marino    struct system_clock
669*e4b17023SJohn Marino    {
670*e4b17023SJohn Marino#ifdef _GLIBCXX_USE_CLOCK_REALTIME
671*e4b17023SJohn Marino      typedef chrono::nanoseconds     				duration;
672*e4b17023SJohn Marino#elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
673*e4b17023SJohn Marino      typedef chrono::microseconds    				duration;
674*e4b17023SJohn Marino#else
675*e4b17023SJohn Marino      typedef chrono::seconds	      				duration;
676*e4b17023SJohn Marino#endif
677*e4b17023SJohn Marino
678*e4b17023SJohn Marino      typedef duration::rep    					rep;
679*e4b17023SJohn Marino      typedef duration::period 					period;
680*e4b17023SJohn Marino      typedef chrono::time_point<system_clock, duration> 	time_point;
681*e4b17023SJohn Marino
682*e4b17023SJohn Marino      static_assert(system_clock::duration::min()
683*e4b17023SJohn Marino		    < system_clock::duration::zero(),
684*e4b17023SJohn Marino		    "a clock's minimum duration cannot be less than its epoch");
685*e4b17023SJohn Marino
686*e4b17023SJohn Marino      static constexpr bool is_steady = false;
687*e4b17023SJohn Marino
688*e4b17023SJohn Marino      static time_point
689*e4b17023SJohn Marino      now() noexcept;
690*e4b17023SJohn Marino
691*e4b17023SJohn Marino      // Map to C API
692*e4b17023SJohn Marino      static std::time_t
693*e4b17023SJohn Marino      to_time_t(const time_point& __t) noexcept
694*e4b17023SJohn Marino      {
695*e4b17023SJohn Marino	return std::time_t(duration_cast<chrono::seconds>
696*e4b17023SJohn Marino			   (__t.time_since_epoch()).count());
697*e4b17023SJohn Marino      }
698*e4b17023SJohn Marino
699*e4b17023SJohn Marino      static time_point
700*e4b17023SJohn Marino      from_time_t(std::time_t __t) noexcept
701*e4b17023SJohn Marino      {
702*e4b17023SJohn Marino	typedef chrono::time_point<system_clock, seconds>	__from;
703*e4b17023SJohn Marino	return time_point_cast<system_clock::duration>
704*e4b17023SJohn Marino	       (__from(chrono::seconds(__t)));
705*e4b17023SJohn Marino      }
706*e4b17023SJohn Marino    };
707*e4b17023SJohn Marino
708*e4b17023SJohn Marino#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
709*e4b17023SJohn Marino    /// steady_clock
710*e4b17023SJohn Marino    struct steady_clock
711*e4b17023SJohn Marino    {
712*e4b17023SJohn Marino      typedef chrono::nanoseconds 				duration;
713*e4b17023SJohn Marino      typedef duration::rep	  				rep;
714*e4b17023SJohn Marino      typedef duration::period	  				period;
715*e4b17023SJohn Marino      typedef chrono::time_point<steady_clock, duration> 	time_point;
716*e4b17023SJohn Marino
717*e4b17023SJohn Marino      static constexpr bool is_steady = true;
718*e4b17023SJohn Marino
719*e4b17023SJohn Marino      static time_point
720*e4b17023SJohn Marino      now() noexcept;
721*e4b17023SJohn Marino    };
722*e4b17023SJohn Marino#else
723*e4b17023SJohn Marino    typedef system_clock steady_clock;
724*e4b17023SJohn Marino#endif
725*e4b17023SJohn Marino
726*e4b17023SJohn Marino    typedef system_clock high_resolution_clock;
727*e4b17023SJohn Marino
728*e4b17023SJohn Marino  _GLIBCXX_END_NAMESPACE_VERSION
729*e4b17023SJohn Marino  } // namespace chrono
730*e4b17023SJohn Marino
731*e4b17023SJohn Marino  // @} group chrono
732*e4b17023SJohn Marino} // namespace
733*e4b17023SJohn Marino
734*e4b17023SJohn Marino#endif //_GLIBCXX_USE_C99_STDINT_TR1
735*e4b17023SJohn Marino
736*e4b17023SJohn Marino#endif //__GXX_EXPERIMENTAL_CXX0X__
737*e4b17023SJohn Marino
738*e4b17023SJohn Marino#endif //_GLIBCXX_CHRONO
739