* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / libstdc++-v3 / include / std / chrono
blobceae7f805c1ea16dc8df90c3d9a5b4f28a545f3b
1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/chrono
26  *  This is a Standard C++ Library header.
27  */
29 #ifndef _GLIBCXX_CHRONO
30 #define _GLIBCXX_CHRONO 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
38 #include <ratio>
39 #include <type_traits>
40 #include <limits>
41 #include <ctime>
42 #include <bits/parse_numbers.h> // for literals support.
44 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
46 namespace std _GLIBCXX_VISIBILITY(default)
48   /**
49    * @defgroup chrono Time
50    * @ingroup utilities
51    *
52    * Classes and functions for time.
53    * @{
54    */
56   /** @namespace std::chrono
57    *  @brief ISO C++ 2011 entities sub-namespace for time and date.
58    */
59   namespace chrono
60   {
61   _GLIBCXX_BEGIN_NAMESPACE_VERSION
63     template<typename _Rep, typename _Period = ratio<1>>
64       struct duration;
66     template<typename _Clock, typename _Dur = typename _Clock::duration>
67       struct time_point;
69   _GLIBCXX_END_NAMESPACE_VERSION
70   }
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74   // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
76   template<typename _CT, typename _Period1, typename _Period2>
77     struct __duration_common_type_wrapper
78     {
79     private:
80       typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
81       typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
82       typedef typename _CT::type __cr;
83       typedef ratio<__gcd_num::value,
84         (_Period1::den / __gcd_den::value) * _Period2::den> __r;
85     public:
86       typedef __success_type<chrono::duration<__cr, __r>> type;
87     };
89   template<typename _Period1, typename _Period2>
90     struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
91     { typedef __failure_type type; };
93   template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
94     struct common_type<chrono::duration<_Rep1, _Period1>,
95              chrono::duration<_Rep2, _Period2>>
96     : public __duration_common_type_wrapper<typename __member_type_wrapper<
97              common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
98     { };
100   // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
102   template<typename _CT, typename _Clock>
103     struct __timepoint_common_type_wrapper
104     {
105       typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
106         type;
107     };
109   template<typename _Clock>
110     struct __timepoint_common_type_wrapper<__failure_type, _Clock>
111     { typedef __failure_type type; };
113   template<typename _Clock, typename _Duration1, typename _Duration2>
114     struct common_type<chrono::time_point<_Clock, _Duration1>,
115              chrono::time_point<_Clock, _Duration2>>
116     : public __timepoint_common_type_wrapper<typename __member_type_wrapper<
117              common_type<_Duration1, _Duration2>>::type, _Clock>::type
118     { };
120 _GLIBCXX_END_NAMESPACE_VERSION
122   namespace chrono
123   {
124   _GLIBCXX_BEGIN_NAMESPACE_VERSION
126     // Primary template for duration_cast impl.
127     template<typename _ToDur, typename _CF, typename _CR,
128              bool _NumIsOne = false, bool _DenIsOne = false>
129       struct __duration_cast_impl
130       {
131         template<typename _Rep, typename _Period>
132           static constexpr _ToDur
133           __cast(const duration<_Rep, _Period>& __d)
134           {
135             typedef typename _ToDur::rep                        __to_rep;
136             return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
137               * static_cast<_CR>(_CF::num)
138               / static_cast<_CR>(_CF::den)));
139           }
140       };
142     template<typename _ToDur, typename _CF, typename _CR>
143       struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
144       {
145         template<typename _Rep, typename _Period>
146           static constexpr _ToDur
147           __cast(const duration<_Rep, _Period>& __d)
148           {
149             typedef typename _ToDur::rep                        __to_rep;
150             return _ToDur(static_cast<__to_rep>(__d.count()));
151           }
152       };
154     template<typename _ToDur, typename _CF, typename _CR>
155       struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
156       {
157         template<typename _Rep, typename _Period>
158           static constexpr _ToDur
159           __cast(const duration<_Rep, _Period>& __d)
160           {
161             typedef typename _ToDur::rep                        __to_rep;
162             return _ToDur(static_cast<__to_rep>(
163               static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
164           }
165       };
167     template<typename _ToDur, typename _CF, typename _CR>
168       struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
169       {
170         template<typename _Rep, typename _Period>
171           static constexpr _ToDur
172           __cast(const duration<_Rep, _Period>& __d)
173           {
174             typedef typename _ToDur::rep                        __to_rep;
175             return _ToDur(static_cast<__to_rep>(
176               static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
177           }
178       };
180     template<typename _Tp>
181       struct __is_duration
182       : std::false_type
183       { };
185     template<typename _Rep, typename _Period>
186       struct __is_duration<duration<_Rep, _Period>>
187       : std::true_type
188       { };
190     /// duration_cast
191     template<typename _ToDur, typename _Rep, typename _Period>
192       constexpr typename enable_if<__is_duration<_ToDur>::value,
193                                    _ToDur>::type
194       duration_cast(const duration<_Rep, _Period>& __d)
195       {
196         typedef typename _ToDur::period                         __to_period;
197         typedef typename _ToDur::rep                            __to_rep;
198         typedef ratio_divide<_Period, __to_period>              __cf;
199         typedef typename common_type<__to_rep, _Rep, intmax_t>::type
200                                                                 __cr;
201         typedef  __duration_cast_impl<_ToDur, __cf, __cr,
202                                       __cf::num == 1, __cf::den == 1> __dc;
203         return __dc::__cast(__d);
204       }
206     /// treat_as_floating_point
207     template<typename _Rep>
208       struct treat_as_floating_point
209       : is_floating_point<_Rep>
210       { };
212 #if __cplusplus > 201402L
213     template <typename _Rep>
214       constexpr bool treat_as_floating_point_v =
215         treat_as_floating_point<_Rep>::value;
216 #endif // C++17
218 #if __cplusplus > 201402L
219 # define __cpp_lib_chrono 201510
221     template<typename _ToDur, typename _Rep, typename _Period>
222       constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
223       floor(const duration<_Rep, _Period>& __d)
224       {
225         auto __to = chrono::duration_cast<_ToDur>(__d);
226         if (__to > __d)
227           return __to - _ToDur{1};
228         return __to;
229       }
231     template<typename _ToDur, typename _Rep, typename _Period>
232       constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
233       ceil(const duration<_Rep, _Period>& __d)
234       {
235         auto __to = chrono::duration_cast<_ToDur>(__d);
236         if (__to < __d)
237           return __to + _ToDur{1};
238         return __to;
239       }
241     template <typename _ToDur, typename _Rep, typename _Period>
242       constexpr enable_if_t<
243         __and_<__is_duration<_ToDur>,
244                __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
245         _ToDur>
246       round(const duration<_Rep, _Period>& __d)
247       {
248         _ToDur __t0 = chrono::floor<_ToDur>(__d);
249         _ToDur __t1 = __t0 + _ToDur{1};
250         auto __diff0 = __d - __t0;
251         auto __diff1 = __t1 - __d;
252         if (__diff0 == __diff1)
253         {
254             if (__t0.count() & 1)
255                 return __t1;
256             return __t0;
257         }
258         else if (__diff0 < __diff1)
259             return __t0;
260         return __t1;
261       }
263     template<typename _Rep, typename _Period>
264       constexpr
265       enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
266       abs(duration<_Rep, _Period> __d)
267       {
268         if (__d >= __d.zero())
269           return __d;
270         return -__d;
271       }
272 #endif // C++17
274     /// duration_values
275     template<typename _Rep>
276       struct duration_values
277       {
278         static constexpr _Rep
279         zero()
280         { return _Rep(0); }
282         static constexpr _Rep
283         max()
284         { return numeric_limits<_Rep>::max(); }
286         static constexpr _Rep
287         min()
288         { return numeric_limits<_Rep>::lowest(); }
289       };
291     template<typename _Tp>
292       struct __is_ratio
293       : std::false_type
294       { };
296     template<intmax_t _Num, intmax_t _Den>
297       struct __is_ratio<ratio<_Num, _Den>>
298       : std::true_type
299       { };
301     /// duration
302     template<typename _Rep, typename _Period>
303       struct duration
304       {
305         typedef _Rep                                            rep;
306         typedef _Period                                         period;
308         static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
309         static_assert(__is_ratio<_Period>::value,
310                       "period must be a specialization of ratio");
311         static_assert(_Period::num > 0, "period must be positive");
313         // 20.11.5.1 construction / copy / destroy
314         constexpr duration() = default;
316         // NB: Make constexpr implicit. This cannot be explicitly
317         // constexpr, as any UDT that is not a literal type with a
318         // constexpr copy constructor will be ill-formed.
319         duration(const duration&) = default;
321         template<typename _Rep2, typename = typename
322                enable_if<is_convertible<_Rep2, rep>::value
323                          && (treat_as_floating_point<rep>::value
324                              || !treat_as_floating_point<_Rep2>::value)>::type>
325           constexpr explicit duration(const _Rep2& __rep)
326           : __r(static_cast<rep>(__rep)) { }
328         template<typename _Rep2, typename _Period2, typename = typename
329                enable_if<treat_as_floating_point<rep>::value
330                          || (ratio_divide<_Period2, period>::den == 1
331                              && !treat_as_floating_point<_Rep2>::value)>::type>
332           constexpr duration(const duration<_Rep2, _Period2>& __d)
333           : __r(duration_cast<duration>(__d).count()) { }
335         ~duration() = default;
336         duration& operator=(const duration&) = default;
338         // 20.11.5.2 observer
339         constexpr rep
340         count() const
341         { return __r; }
343         // 20.11.5.3 arithmetic
344         constexpr duration
345         operator+() const
346         { return *this; }
348         constexpr duration
349         operator-() const
350         { return duration(-__r); }
352         duration&
353         operator++()
354         {
355           ++__r;
356           return *this;
357         }
359         duration
360         operator++(int)
361         { return duration(__r++); }
363         duration&
364         operator--()
365         {
366           --__r;
367           return *this;
368         }
370         duration
371         operator--(int)
372         { return duration(__r--); }
374         duration&
375         operator+=(const duration& __d)
376         {
377           __r += __d.count();
378           return *this;
379         }
381         duration&
382         operator-=(const duration& __d)
383         {
384           __r -= __d.count();
385           return *this;
386         }
388         duration&
389         operator*=(const rep& __rhs)
390         {
391           __r *= __rhs;
392           return *this;
393         }
395         duration&
396         operator/=(const rep& __rhs)
397         {
398           __r /= __rhs;
399           return *this;
400         }
402         // DR 934.
403         template<typename _Rep2 = rep>
404           typename enable_if<!treat_as_floating_point<_Rep2>::value,
405                              duration&>::type
406           operator%=(const rep& __rhs)
407           {
408             __r %= __rhs;
409             return *this;
410           }
412         template<typename _Rep2 = rep>
413           typename enable_if<!treat_as_floating_point<_Rep2>::value,
414                              duration&>::type
415           operator%=(const duration& __d)
416           {
417             __r %= __d.count();
418             return *this;
419           }
421         // 20.11.5.4 special values
422         static constexpr duration
423         zero()
424         { return duration(duration_values<rep>::zero()); }
426         static constexpr duration
427         min()
428         { return duration(duration_values<rep>::min()); }
430         static constexpr duration
431         max()
432         { return duration(duration_values<rep>::max()); }
434       private:
435         rep __r;
436       };
438     template<typename _Rep1, typename _Period1,
439              typename _Rep2, typename _Period2>
440       constexpr typename common_type<duration<_Rep1, _Period1>,
441                                      duration<_Rep2, _Period2>>::type
442       operator+(const duration<_Rep1, _Period1>& __lhs,
443                 const duration<_Rep2, _Period2>& __rhs)
444       {
445         typedef duration<_Rep1, _Period1>                       __dur1;
446         typedef duration<_Rep2, _Period2>                       __dur2;
447         typedef typename common_type<__dur1,__dur2>::type       __cd;
448         return __cd(__cd(__lhs).count() + __cd(__rhs).count());
449       }
451     template<typename _Rep1, typename _Period1,
452              typename _Rep2, typename _Period2>
453       constexpr typename common_type<duration<_Rep1, _Period1>,
454                                      duration<_Rep2, _Period2>>::type
455       operator-(const duration<_Rep1, _Period1>& __lhs,
456                 const duration<_Rep2, _Period2>& __rhs)
457       {
458         typedef duration<_Rep1, _Period1>                       __dur1;
459         typedef duration<_Rep2, _Period2>                       __dur2;
460         typedef typename common_type<__dur1,__dur2>::type       __cd;
461         return __cd(__cd(__lhs).count() - __cd(__rhs).count());
462       }
464     template<typename _Rep1, typename _Rep2, bool =
465              is_convertible<_Rep2,
466                             typename common_type<_Rep1, _Rep2>::type>::value>
467       struct __common_rep_type { };
469     template<typename _Rep1, typename _Rep2>
470       struct __common_rep_type<_Rep1, _Rep2, true>
471       { typedef typename common_type<_Rep1, _Rep2>::type type; };
473     template<typename _Rep1, typename _Period, typename _Rep2>
474       constexpr
475       duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
476       operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
477       {
478         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
479           __cd;
480         return __cd(__cd(__d).count() * __s);
481       }
483     template<typename _Rep1, typename _Rep2, typename _Period>
484       constexpr
485       duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
486       operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
487       { return __d * __s; }
489     template<typename _Rep1, typename _Period, typename _Rep2>
490       constexpr duration<typename __common_rep_type<_Rep1, typename
491         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
492       operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
493       {
494         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
495           __cd;
496         return __cd(__cd(__d).count() / __s);
497       }
499     template<typename _Rep1, typename _Period1,
500              typename _Rep2, typename _Period2>
501       constexpr typename common_type<_Rep1, _Rep2>::type
502       operator/(const duration<_Rep1, _Period1>& __lhs,
503                 const duration<_Rep2, _Period2>& __rhs)
504       {
505         typedef duration<_Rep1, _Period1>                       __dur1;
506         typedef duration<_Rep2, _Period2>                       __dur2;
507         typedef typename common_type<__dur1,__dur2>::type       __cd;
508         return __cd(__lhs).count() / __cd(__rhs).count();
509       }
511     // DR 934.
512     template<typename _Rep1, typename _Period, typename _Rep2>
513       constexpr duration<typename __common_rep_type<_Rep1, typename
514         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
515       operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
516       {
517         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
518           __cd;
519         return __cd(__cd(__d).count() % __s);
520       }
522     template<typename _Rep1, typename _Period1,
523              typename _Rep2, typename _Period2>
524       constexpr typename common_type<duration<_Rep1, _Period1>,
525                                      duration<_Rep2, _Period2>>::type
526       operator%(const duration<_Rep1, _Period1>& __lhs,
527                 const duration<_Rep2, _Period2>& __rhs)
528       {
529         typedef duration<_Rep1, _Period1>                       __dur1;
530         typedef duration<_Rep2, _Period2>                       __dur2;
531         typedef typename common_type<__dur1,__dur2>::type       __cd;
532         return __cd(__cd(__lhs).count() % __cd(__rhs).count());
533       }
535     // comparisons
536     template<typename _Rep1, typename _Period1,
537              typename _Rep2, typename _Period2>
538       constexpr bool
539       operator==(const duration<_Rep1, _Period1>& __lhs,
540                  const duration<_Rep2, _Period2>& __rhs)
541       {
542         typedef duration<_Rep1, _Period1>                       __dur1;
543         typedef duration<_Rep2, _Period2>                       __dur2;
544         typedef typename common_type<__dur1,__dur2>::type       __ct;
545         return __ct(__lhs).count() == __ct(__rhs).count();
546       }
548     template<typename _Rep1, typename _Period1,
549              typename _Rep2, typename _Period2>
550       constexpr bool
551       operator<(const duration<_Rep1, _Period1>& __lhs,
552                 const duration<_Rep2, _Period2>& __rhs)
553       {
554         typedef duration<_Rep1, _Period1>                       __dur1;
555         typedef duration<_Rep2, _Period2>                       __dur2;
556         typedef typename common_type<__dur1,__dur2>::type       __ct;
557         return __ct(__lhs).count() < __ct(__rhs).count();
558       }
560     template<typename _Rep1, typename _Period1,
561              typename _Rep2, typename _Period2>
562       constexpr bool
563       operator!=(const duration<_Rep1, _Period1>& __lhs,
564                  const duration<_Rep2, _Period2>& __rhs)
565       { return !(__lhs == __rhs); }
567     template<typename _Rep1, typename _Period1,
568              typename _Rep2, typename _Period2>
569       constexpr bool
570       operator<=(const duration<_Rep1, _Period1>& __lhs,
571                  const duration<_Rep2, _Period2>& __rhs)
572       { return !(__rhs < __lhs); }
574     template<typename _Rep1, typename _Period1,
575              typename _Rep2, typename _Period2>
576       constexpr bool
577       operator>(const duration<_Rep1, _Period1>& __lhs,
578                 const duration<_Rep2, _Period2>& __rhs)
579       { return __rhs < __lhs; }
581     template<typename _Rep1, typename _Period1,
582              typename _Rep2, typename _Period2>
583       constexpr bool
584       operator>=(const duration<_Rep1, _Period1>& __lhs,
585                  const duration<_Rep2, _Period2>& __rhs)
586       { return !(__lhs < __rhs); }
588     /// nanoseconds
589     typedef duration<int64_t, nano>         nanoseconds;
591     /// microseconds
592     typedef duration<int64_t, micro>        microseconds;
594     /// milliseconds
595     typedef duration<int64_t, milli>        milliseconds;
597     /// seconds
598     typedef duration<int64_t>               seconds;
600     /// minutes
601     typedef duration<int64_t, ratio< 60>>   minutes;
603     /// hours
604     typedef duration<int64_t, ratio<3600>>  hours;
606     /// time_point
607     template<typename _Clock, typename _Dur>
608       struct time_point
609       {
610         typedef _Clock                                          clock;
611         typedef _Dur                                            duration;
612         typedef typename duration::rep                          rep;
613         typedef typename duration::period                       period;
615         constexpr time_point() : __d(duration::zero())
616         { }
618         constexpr explicit time_point(const duration& __dur)
619         : __d(__dur)
620         { }
622         // conversions
623         template<typename _Dur2>
624           constexpr time_point(const time_point<clock, _Dur2>& __t)
625           : __d(__t.time_since_epoch())
626           { }
628         // observer
629         constexpr duration
630         time_since_epoch() const
631         { return __d; }
633         // arithmetic
634         time_point&
635         operator+=(const duration& __dur)
636         {
637           __d += __dur;
638           return *this;
639         }
641         time_point&
642         operator-=(const duration& __dur)
643         {
644           __d -= __dur;
645           return *this;
646         }
648         // special values
649         static constexpr time_point
650         min()
651         { return time_point(duration::min()); }
653         static constexpr time_point
654         max()
655         { return time_point(duration::max()); }
657       private:
658         duration __d;
659       };
661     /// time_point_cast
662     template<typename _ToDur, typename _Clock, typename _Dur>
663       constexpr typename enable_if<__is_duration<_ToDur>::value,
664                                    time_point<_Clock, _ToDur>>::type
665       time_point_cast(const time_point<_Clock, _Dur>& __t)
666       {
667         typedef time_point<_Clock, _ToDur>                      __time_point;
668         return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
669       }
671 #if __cplusplus > 201402L
672     template<typename _ToDur, typename _Clock, typename _Dur>
673       constexpr
674       enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
675       floor(const time_point<_Clock, _Dur>& __tp)
676       {
677         return time_point<_Clock, _ToDur>{
678             chrono::floor<_ToDur>(__tp.time_since_epoch())};
679       }
681     template<typename _ToDur, typename _Clock, typename _Dur>
682       constexpr
683       enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
684       ceil(const time_point<_Clock, _Dur>& __tp)
685       {
686         return time_point<_Clock, _ToDur>{
687             chrono::ceil<_ToDur>(__tp.time_since_epoch())};
688       }
690     template<typename _ToDur, typename _Clock, typename _Dur>
691       constexpr enable_if_t<
692         __and_<__is_duration<_ToDur>,
693                __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
694         time_point<_Clock, _ToDur>>
695       round(const time_point<_Clock, _Dur>& __tp)
696       {
697         return time_point<_Clock, _ToDur>{
698             chrono::round<_ToDur>(__tp.time_since_epoch())};
699       }
700 #endif // C++17
702     template<typename _Clock, typename _Dur1,
703              typename _Rep2, typename _Period2>
704       constexpr time_point<_Clock,
705         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
706       operator+(const time_point<_Clock, _Dur1>& __lhs,
707                 const duration<_Rep2, _Period2>& __rhs)
708       {
709         typedef duration<_Rep2, _Period2>                       __dur2;
710         typedef typename common_type<_Dur1,__dur2>::type        __ct;
711         typedef time_point<_Clock, __ct>                        __time_point;
712         return __time_point(__lhs.time_since_epoch() + __rhs);
713       }
715     template<typename _Rep1, typename _Period1,
716              typename _Clock, typename _Dur2>
717       constexpr time_point<_Clock,
718         typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
719       operator+(const duration<_Rep1, _Period1>& __lhs,
720                 const time_point<_Clock, _Dur2>& __rhs)
721       {
722         typedef duration<_Rep1, _Period1>                       __dur1;
723         typedef typename common_type<__dur1,_Dur2>::type        __ct;
724         typedef time_point<_Clock, __ct>                        __time_point;
725         return __time_point(__rhs.time_since_epoch() + __lhs);
726       }
728     template<typename _Clock, typename _Dur1,
729              typename _Rep2, typename _Period2>
730       constexpr time_point<_Clock,
731         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
732       operator-(const time_point<_Clock, _Dur1>& __lhs,
733                 const duration<_Rep2, _Period2>& __rhs)
734       {
735         typedef duration<_Rep2, _Period2>                       __dur2;
736         typedef typename common_type<_Dur1,__dur2>::type        __ct;
737         typedef time_point<_Clock, __ct>                        __time_point;
738         return __time_point(__lhs.time_since_epoch() -__rhs);
739       }
741     template<typename _Clock, typename _Dur1, typename _Dur2>
742       constexpr typename common_type<_Dur1, _Dur2>::type
743       operator-(const time_point<_Clock, _Dur1>& __lhs,
744                 const time_point<_Clock, _Dur2>& __rhs)
745       { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
747     template<typename _Clock, typename _Dur1, typename _Dur2>
748       constexpr bool
749       operator==(const time_point<_Clock, _Dur1>& __lhs,
750                  const time_point<_Clock, _Dur2>& __rhs)
751       { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
753     template<typename _Clock, typename _Dur1, typename _Dur2>
754       constexpr bool
755       operator!=(const time_point<_Clock, _Dur1>& __lhs,
756                  const time_point<_Clock, _Dur2>& __rhs)
757       { return !(__lhs == __rhs); }
759     template<typename _Clock, typename _Dur1, typename _Dur2>
760       constexpr bool
761       operator<(const time_point<_Clock, _Dur1>& __lhs,
762                 const time_point<_Clock, _Dur2>& __rhs)
763       { return  __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
765     template<typename _Clock, typename _Dur1, typename _Dur2>
766       constexpr bool
767       operator<=(const time_point<_Clock, _Dur1>& __lhs,
768                  const time_point<_Clock, _Dur2>& __rhs)
769       { return !(__rhs < __lhs); }
771     template<typename _Clock, typename _Dur1, typename _Dur2>
772       constexpr bool
773       operator>(const time_point<_Clock, _Dur1>& __lhs,
774                 const time_point<_Clock, _Dur2>& __rhs)
775       { return __rhs < __lhs; }
777     template<typename _Clock, typename _Dur1, typename _Dur2>
778       constexpr bool
779       operator>=(const time_point<_Clock, _Dur1>& __lhs,
780                  const time_point<_Clock, _Dur2>& __rhs)
781       { return !(__lhs < __rhs); }
784     // Clocks.
786     // Why nanosecond resolution as the default?
787     // Why have std::system_clock always count in the highest
788     // resolution (ie nanoseconds), even if on some OSes the low 3
789     // or 9 decimal digits will be always zero? This allows later
790     // implementations to change the system_clock::now()
791     // implementation any time to provide better resolution without
792     // changing function signature or units.
794     // To support the (forward) evolution of the library's defined
795     // clocks, wrap inside inline namespace so that the current
796     // defintions of system_clock, steady_clock, and
797     // high_resolution_clock types are uniquely mangled. This way, new
798     // code can use the latests clocks, while the library can contain
799     // compatibility definitions for previous versions.  At some
800     // point, when these clocks settle down, the inlined namespaces
801     // can be removed.  XXX GLIBCXX_ABI Deprecated
802     inline namespace _V2 {
804     /**
805      *  @brief System clock.
806      *
807      *  Time returned represents wall time from the system-wide clock.
808     */
809     struct system_clock
810     {
811       typedef chrono::nanoseconds                               duration;
812       typedef duration::rep                                     rep;
813       typedef duration::period                                  period;
814       typedef chrono::time_point<system_clock, duration>        time_point;
816       static_assert(system_clock::duration::min()
817                     < system_clock::duration::zero(),
818                     "a clock's minimum duration cannot be less than its epoch");
820       static constexpr bool is_steady = false;
822       static time_point
823       now() noexcept;
825       // Map to C API
826       static std::time_t
827       to_time_t(const time_point& __t) noexcept
828       {
829         return std::time_t(duration_cast<chrono::seconds>
830                            (__t.time_since_epoch()).count());
831       }
833       static time_point
834       from_time_t(std::time_t __t) noexcept
835       {
836         typedef chrono::time_point<system_clock, seconds>       __from;
837         return time_point_cast<system_clock::duration>
838                (__from(chrono::seconds(__t)));
839       }
840     };
843     /**
844      *  @brief Monotonic clock
845      *
846      *  Time returned has the property of only increasing at a uniform rate.
847     */
848     struct steady_clock
849     {
850       typedef chrono::nanoseconds                               duration;
851       typedef duration::rep                                     rep;
852       typedef duration::period                                  period;
853       typedef chrono::time_point<steady_clock, duration>        time_point;
855       static constexpr bool is_steady = true;
857       static time_point
858       now() noexcept;
859     };
862     /**
863      *  @brief Highest-resolution clock
864      *
865      *  This is the clock "with the shortest tick period." Alias to
866      *  std::system_clock until higher-than-nanosecond definitions
867      *  become feasible.
868     */
869     using high_resolution_clock = system_clock;
871     } // end inline namespace _V2
873   _GLIBCXX_END_NAMESPACE_VERSION
874   } // namespace chrono
876 #if __cplusplus > 201103L
878 #define __cpp_lib_chrono_udls 201304
880   inline namespace literals
881   {
882   inline namespace chrono_literals
883   {
884   _GLIBCXX_BEGIN_NAMESPACE_VERSION
886     template<typename _Rep, unsigned long long _Val>
887       struct _Checked_integral_constant
888       : integral_constant<_Rep, static_cast<_Rep>(_Val)>
889       {
890         static_assert(_Checked_integral_constant::value >= 0
891                       && _Checked_integral_constant::value == _Val,
892                       "literal value cannot be represented by duration type");
893       };
895     template<typename _Dur, char... _Digits>
896       constexpr _Dur __check_overflow()
897       {
898         using _Val = __parse_int::_Parse_int<_Digits...>;
899         using _Rep = typename _Dur::rep;
900         // TODO: should be simply integral_constant<_Rep, _Val::value>
901         // but GCC doesn't reject narrowing conversions to _Rep.
902         using _CheckedVal = _Checked_integral_constant<_Rep, _Val::value>;
903         return _Dur{_CheckedVal::value};
904       }
906     constexpr chrono::duration<long double, ratio<3600,1>>
907     operator""h(long double __hours)
908     { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
910     template <char... _Digits>
911       constexpr chrono::hours
912       operator""h()
913       { return __check_overflow<chrono::hours, _Digits...>(); }
915     constexpr chrono::duration<long double, ratio<60,1>>
916     operator""min(long double __mins)
917     { return chrono::duration<long double, ratio<60,1>>{__mins}; }
919     template <char... _Digits>
920       constexpr chrono::minutes
921       operator""min()
922       { return __check_overflow<chrono::minutes, _Digits...>(); }
924     constexpr chrono::duration<long double>
925     operator""s(long double __secs)
926     { return chrono::duration<long double>{__secs}; }
928     template <char... _Digits>
929       constexpr chrono::seconds
930       operator""s()
931       { return __check_overflow<chrono::seconds, _Digits...>(); }
933     constexpr chrono::duration<long double, milli>
934     operator""ms(long double __msecs)
935     { return chrono::duration<long double, milli>{__msecs}; }
937     template <char... _Digits>
938       constexpr chrono::milliseconds
939       operator""ms()
940       { return __check_overflow<chrono::milliseconds, _Digits...>(); }
942     constexpr chrono::duration<long double, micro>
943     operator""us(long double __usecs)
944     { return chrono::duration<long double, micro>{__usecs}; }
946     template <char... _Digits>
947       constexpr chrono::microseconds
948       operator""us()
949       { return __check_overflow<chrono::microseconds, _Digits...>(); }
951     constexpr chrono::duration<long double, nano>
952     operator""ns(long double __nsecs)
953     { return chrono::duration<long double, nano>{__nsecs}; }
955     template <char... _Digits>
956       constexpr chrono::nanoseconds
957       operator""ns()
958       { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
960   _GLIBCXX_END_NAMESPACE_VERSION
961   } // inline namespace chrono_literals
962   } // inline namespace literals
964   namespace chrono
965   {
966   _GLIBCXX_BEGIN_NAMESPACE_VERSION
968   using namespace literals::chrono_literals;
970   _GLIBCXX_END_NAMESPACE_VERSION
971   } // namespace chrono
973 #endif // __cplusplus > 201103L
975   // @} group chrono
976 } // namespace std
978 #endif //_GLIBCXX_USE_C99_STDINT_TR1
980 #endif // C++11
982 #endif //_GLIBCXX_CHRONO