libstdc++
chrono
Go to the documentation of this file.
00001 // <chrono> -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/chrono
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_CHRONO
00030 #define _GLIBCXX_CHRONO 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus < 201103L
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <ratio>
00039 #include <type_traits>
00040 #include <limits>
00041 #include <ctime>
00042 #include <bits/parse_numbers.h> // for literals support.
00043 
00044 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
00045 
00046 namespace std _GLIBCXX_VISIBILITY(default)
00047 {
00048   /**
00049    * @defgroup chrono Time
00050    * @ingroup utilities
00051    *
00052    * Classes and functions for time.
00053    * @{
00054    */
00055 
00056   /** @namespace std::chrono
00057    *  @brief ISO C++ 2011 entities sub-namespace for time and date.
00058    */
00059   namespace chrono
00060   {
00061   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00062 
00063     template<typename _Rep, typename _Period = ratio<1>>
00064       struct duration;
00065 
00066     template<typename _Clock, typename _Dur = typename _Clock::duration>
00067       struct time_point;
00068 
00069   _GLIBCXX_END_NAMESPACE_VERSION
00070   }
00071 
00072 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00073 
00074   // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
00075   
00076   template<typename _CT, typename _Period1, typename _Period2>
00077     struct __duration_common_type_wrapper
00078     {
00079     private:
00080       typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
00081       typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
00082       typedef typename _CT::type __cr;
00083       typedef ratio<__gcd_num::value,
00084         (_Period1::den / __gcd_den::value) * _Period2::den> __r;
00085     public:
00086       typedef __success_type<chrono::duration<__cr, __r>> type;
00087     };
00088 
00089   template<typename _Period1, typename _Period2>
00090     struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
00091     { typedef __failure_type type; };
00092 
00093   template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
00094     struct common_type<chrono::duration<_Rep1, _Period1>,
00095              chrono::duration<_Rep2, _Period2>>
00096     : public __duration_common_type_wrapper<typename __member_type_wrapper<
00097              common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
00098     { };
00099 
00100   // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
00101   
00102   template<typename _CT, typename _Clock>
00103     struct __timepoint_common_type_wrapper
00104     {
00105       typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
00106         type;
00107     };
00108 
00109   template<typename _Clock>
00110     struct __timepoint_common_type_wrapper<__failure_type, _Clock>
00111     { typedef __failure_type type; };
00112 
00113   template<typename _Clock, typename _Duration1, typename _Duration2>
00114     struct common_type<chrono::time_point<_Clock, _Duration1>,
00115              chrono::time_point<_Clock, _Duration2>>
00116     : public __timepoint_common_type_wrapper<typename __member_type_wrapper<
00117              common_type<_Duration1, _Duration2>>::type, _Clock>::type
00118     { };
00119 
00120 _GLIBCXX_END_NAMESPACE_VERSION
00121 
00122   namespace chrono
00123   {
00124   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00125 
00126     // Primary template for duration_cast impl.
00127     template<typename _ToDur, typename _CF, typename _CR,
00128              bool _NumIsOne = false, bool _DenIsOne = false>
00129       struct __duration_cast_impl
00130       {
00131         template<typename _Rep, typename _Period>
00132           static constexpr _ToDur
00133           __cast(const duration<_Rep, _Period>& __d)
00134           {
00135             typedef typename _ToDur::rep                        __to_rep;
00136             return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
00137               * static_cast<_CR>(_CF::num)
00138               / static_cast<_CR>(_CF::den)));
00139           }
00140       };
00141 
00142     template<typename _ToDur, typename _CF, typename _CR>
00143       struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
00144       {
00145         template<typename _Rep, typename _Period>
00146           static constexpr _ToDur
00147           __cast(const duration<_Rep, _Period>& __d)
00148           {
00149             typedef typename _ToDur::rep                        __to_rep;
00150             return _ToDur(static_cast<__to_rep>(__d.count()));
00151           }
00152       };
00153 
00154     template<typename _ToDur, typename _CF, typename _CR>
00155       struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
00156       {
00157         template<typename _Rep, typename _Period>
00158           static constexpr _ToDur
00159           __cast(const duration<_Rep, _Period>& __d)
00160           {
00161             typedef typename _ToDur::rep                        __to_rep;
00162             return _ToDur(static_cast<__to_rep>(
00163               static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
00164           }
00165       };
00166 
00167     template<typename _ToDur, typename _CF, typename _CR>
00168       struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
00169       {
00170         template<typename _Rep, typename _Period>
00171           static constexpr _ToDur
00172           __cast(const duration<_Rep, _Period>& __d)
00173           {
00174             typedef typename _ToDur::rep                        __to_rep;
00175             return _ToDur(static_cast<__to_rep>(
00176               static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
00177           }
00178       };
00179 
00180     template<typename _Tp>
00181       struct __is_duration
00182       : std::false_type
00183       { };
00184 
00185     template<typename _Rep, typename _Period>
00186       struct __is_duration<duration<_Rep, _Period>>
00187       : std::true_type
00188       { };
00189 
00190     /// duration_cast
00191     template<typename _ToDur, typename _Rep, typename _Period>
00192       constexpr typename enable_if<__is_duration<_ToDur>::value,
00193                                    _ToDur>::type
00194       duration_cast(const duration<_Rep, _Period>& __d)
00195       {
00196         typedef typename _ToDur::period                         __to_period;
00197         typedef typename _ToDur::rep                            __to_rep;
00198         typedef ratio_divide<_Period, __to_period>              __cf;
00199         typedef typename common_type<__to_rep, _Rep, intmax_t>::type
00200                                                                 __cr;
00201         typedef  __duration_cast_impl<_ToDur, __cf, __cr,
00202                                       __cf::num == 1, __cf::den == 1> __dc;
00203         return __dc::__cast(__d);
00204       }
00205 
00206     /// treat_as_floating_point
00207     template<typename _Rep>
00208       struct treat_as_floating_point
00209       : is_floating_point<_Rep>
00210       { };
00211 
00212     /// duration_values
00213     template<typename _Rep>
00214       struct duration_values
00215       {
00216         static constexpr _Rep
00217         zero()
00218         { return _Rep(0); }
00219 
00220         static constexpr _Rep
00221         max()
00222         { return numeric_limits<_Rep>::max(); }
00223 
00224         static constexpr _Rep
00225         min()
00226         { return numeric_limits<_Rep>::lowest(); }
00227       };
00228 
00229     template<typename _Tp>
00230       struct __is_ratio
00231       : std::false_type
00232       { };
00233 
00234     template<intmax_t _Num, intmax_t _Den>
00235       struct __is_ratio<ratio<_Num, _Den>>
00236       : std::true_type
00237       { };
00238 
00239     /// duration
00240     template<typename _Rep, typename _Period>
00241       struct duration
00242       {
00243         typedef _Rep                                            rep;
00244         typedef _Period                                         period;
00245 
00246         static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
00247         static_assert(__is_ratio<_Period>::value,
00248                       "period must be a specialization of ratio");
00249         static_assert(_Period::num > 0, "period must be positive");
00250 
00251         // 20.11.5.1 construction / copy / destroy
00252         constexpr duration() = default;
00253 
00254         // NB: Make constexpr implicit. This cannot be explicitly
00255         // constexpr, as any UDT that is not a literal type with a
00256         // constexpr copy constructor will be ill-formed.
00257         duration(const duration&) = default;
00258 
00259         template<typename _Rep2, typename = typename
00260                enable_if<is_convertible<_Rep2, rep>::value
00261                          && (treat_as_floating_point<rep>::value
00262                              || !treat_as_floating_point<_Rep2>::value)>::type>
00263           constexpr explicit duration(const _Rep2& __rep)
00264           : __r(static_cast<rep>(__rep)) { }
00265 
00266         template<typename _Rep2, typename _Period2, typename = typename
00267                enable_if<treat_as_floating_point<rep>::value
00268                          || (ratio_divide<_Period2, period>::den == 1
00269                              && !treat_as_floating_point<_Rep2>::value)>::type>
00270           constexpr duration(const duration<_Rep2, _Period2>& __d)
00271           : __r(duration_cast<duration>(__d).count()) { }
00272 
00273         ~duration() = default;
00274         duration& operator=(const duration&) = default;
00275 
00276         // 20.11.5.2 observer
00277         constexpr rep
00278         count() const
00279         { return __r; }
00280 
00281         // 20.11.5.3 arithmetic
00282         constexpr duration
00283         operator+() const
00284         { return *this; }
00285 
00286         constexpr duration
00287         operator-() const
00288         { return duration(-__r); }
00289 
00290         duration&
00291         operator++()
00292         {
00293           ++__r;
00294           return *this;
00295         }
00296 
00297         duration
00298         operator++(int)
00299         { return duration(__r++); }
00300 
00301         duration&
00302         operator--()
00303         {
00304           --__r;
00305           return *this;
00306         }
00307 
00308         duration
00309         operator--(int)
00310         { return duration(__r--); }
00311 
00312         duration&
00313         operator+=(const duration& __d)
00314         {
00315           __r += __d.count();
00316           return *this;
00317         }
00318 
00319         duration&
00320         operator-=(const duration& __d)
00321         {
00322           __r -= __d.count();
00323           return *this;
00324         }
00325 
00326         duration&
00327         operator*=(const rep& __rhs)
00328         {
00329           __r *= __rhs;
00330           return *this;
00331         }
00332 
00333         duration&
00334         operator/=(const rep& __rhs)
00335         {
00336           __r /= __rhs;
00337           return *this;
00338         }
00339 
00340         // DR 934.
00341         template<typename _Rep2 = rep>
00342           typename enable_if<!treat_as_floating_point<_Rep2>::value,
00343                              duration&>::type
00344           operator%=(const rep& __rhs)
00345           {
00346             __r %= __rhs;
00347             return *this;
00348           }
00349 
00350         template<typename _Rep2 = rep>
00351           typename enable_if<!treat_as_floating_point<_Rep2>::value,
00352                              duration&>::type
00353           operator%=(const duration& __d)
00354           {
00355             __r %= __d.count();
00356             return *this;
00357           }
00358 
00359         // 20.11.5.4 special values
00360         static constexpr duration
00361         zero()
00362         { return duration(duration_values<rep>::zero()); }
00363 
00364         static constexpr duration
00365         min()
00366         { return duration(duration_values<rep>::min()); }
00367 
00368         static constexpr duration
00369         max()
00370         { return duration(duration_values<rep>::max()); }
00371 
00372       private:
00373         rep __r;
00374       };
00375 
00376     template<typename _Rep1, typename _Period1,
00377              typename _Rep2, typename _Period2>
00378       constexpr typename common_type<duration<_Rep1, _Period1>,
00379                                      duration<_Rep2, _Period2>>::type
00380       operator+(const duration<_Rep1, _Period1>& __lhs,
00381                 const duration<_Rep2, _Period2>& __rhs)
00382       {
00383         typedef duration<_Rep1, _Period1>                       __dur1;
00384         typedef duration<_Rep2, _Period2>                       __dur2;
00385         typedef typename common_type<__dur1,__dur2>::type       __cd;
00386         return __cd(__cd(__lhs).count() + __cd(__rhs).count());
00387       }
00388 
00389     template<typename _Rep1, typename _Period1,
00390              typename _Rep2, typename _Period2>
00391       constexpr typename common_type<duration<_Rep1, _Period1>,
00392                                      duration<_Rep2, _Period2>>::type
00393       operator-(const duration<_Rep1, _Period1>& __lhs,
00394                 const duration<_Rep2, _Period2>& __rhs)
00395       {
00396         typedef duration<_Rep1, _Period1>                       __dur1;
00397         typedef duration<_Rep2, _Period2>                       __dur2;
00398         typedef typename common_type<__dur1,__dur2>::type       __cd;
00399         return __cd(__cd(__lhs).count() - __cd(__rhs).count());
00400       }
00401 
00402     template<typename _Rep1, typename _Rep2, bool =
00403              is_convertible<_Rep2,
00404                             typename common_type<_Rep1, _Rep2>::type>::value>
00405       struct __common_rep_type { };
00406 
00407     template<typename _Rep1, typename _Rep2>
00408       struct __common_rep_type<_Rep1, _Rep2, true>
00409       { typedef typename common_type<_Rep1, _Rep2>::type type; };
00410 
00411     template<typename _Rep1, typename _Period, typename _Rep2>
00412       constexpr
00413       duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
00414       operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00415       {
00416         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00417           __cd;
00418         return __cd(__cd(__d).count() * __s);
00419       }
00420 
00421     template<typename _Rep1, typename _Rep2, typename _Period>
00422       constexpr
00423       duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
00424       operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
00425       { return __d * __s; }
00426 
00427     template<typename _Rep1, typename _Period, typename _Rep2>
00428       constexpr duration<typename __common_rep_type<_Rep1, typename
00429         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00430       operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00431       {
00432         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00433           __cd;
00434         return __cd(__cd(__d).count() / __s);
00435       }
00436 
00437     template<typename _Rep1, typename _Period1,
00438              typename _Rep2, typename _Period2>
00439       constexpr typename common_type<_Rep1, _Rep2>::type
00440       operator/(const duration<_Rep1, _Period1>& __lhs,
00441                 const duration<_Rep2, _Period2>& __rhs)
00442       {
00443         typedef duration<_Rep1, _Period1>                       __dur1;
00444         typedef duration<_Rep2, _Period2>                       __dur2;
00445         typedef typename common_type<__dur1,__dur2>::type       __cd;
00446         return __cd(__lhs).count() / __cd(__rhs).count();
00447       }
00448 
00449     // DR 934.
00450     template<typename _Rep1, typename _Period, typename _Rep2>
00451       constexpr duration<typename __common_rep_type<_Rep1, typename
00452         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00453       operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00454       {
00455         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00456           __cd;
00457         return __cd(__cd(__d).count() % __s);
00458       }
00459 
00460     template<typename _Rep1, typename _Period1,
00461              typename _Rep2, typename _Period2>
00462       constexpr typename common_type<duration<_Rep1, _Period1>,
00463                                      duration<_Rep2, _Period2>>::type
00464       operator%(const duration<_Rep1, _Period1>& __lhs,
00465                 const duration<_Rep2, _Period2>& __rhs)
00466       {
00467         typedef duration<_Rep1, _Period1>                       __dur1;
00468         typedef duration<_Rep2, _Period2>                       __dur2;
00469         typedef typename common_type<__dur1,__dur2>::type       __cd;
00470         return __cd(__cd(__lhs).count() % __cd(__rhs).count());
00471       }
00472 
00473     // comparisons
00474     template<typename _Rep1, typename _Period1,
00475              typename _Rep2, typename _Period2>
00476       constexpr bool
00477       operator==(const duration<_Rep1, _Period1>& __lhs,
00478                  const duration<_Rep2, _Period2>& __rhs)
00479       {
00480         typedef duration<_Rep1, _Period1>                       __dur1;
00481         typedef duration<_Rep2, _Period2>                       __dur2;
00482         typedef typename common_type<__dur1,__dur2>::type       __ct;
00483         return __ct(__lhs).count() == __ct(__rhs).count();
00484       }
00485 
00486     template<typename _Rep1, typename _Period1,
00487              typename _Rep2, typename _Period2>
00488       constexpr bool
00489       operator<(const duration<_Rep1, _Period1>& __lhs,
00490                 const duration<_Rep2, _Period2>& __rhs)
00491       {
00492         typedef duration<_Rep1, _Period1>                       __dur1;
00493         typedef duration<_Rep2, _Period2>                       __dur2;
00494         typedef typename common_type<__dur1,__dur2>::type       __ct;
00495         return __ct(__lhs).count() < __ct(__rhs).count();
00496       }
00497 
00498     template<typename _Rep1, typename _Period1,
00499              typename _Rep2, typename _Period2>
00500       constexpr bool
00501       operator!=(const duration<_Rep1, _Period1>& __lhs,
00502                  const duration<_Rep2, _Period2>& __rhs)
00503       { return !(__lhs == __rhs); }
00504 
00505     template<typename _Rep1, typename _Period1,
00506              typename _Rep2, typename _Period2>
00507       constexpr bool
00508       operator<=(const duration<_Rep1, _Period1>& __lhs,
00509                  const duration<_Rep2, _Period2>& __rhs)
00510       { return !(__rhs < __lhs); }
00511 
00512     template<typename _Rep1, typename _Period1,
00513              typename _Rep2, typename _Period2>
00514       constexpr bool
00515       operator>(const duration<_Rep1, _Period1>& __lhs,
00516                 const duration<_Rep2, _Period2>& __rhs)
00517       { return __rhs < __lhs; }
00518 
00519     template<typename _Rep1, typename _Period1,
00520              typename _Rep2, typename _Period2>
00521       constexpr bool
00522       operator>=(const duration<_Rep1, _Period1>& __lhs,
00523                  const duration<_Rep2, _Period2>& __rhs)
00524       { return !(__lhs < __rhs); }
00525 
00526     /// nanoseconds
00527     typedef duration<int64_t, nano>         nanoseconds;
00528 
00529     /// microseconds
00530     typedef duration<int64_t, micro>        microseconds;
00531 
00532     /// milliseconds
00533     typedef duration<int64_t, milli>        milliseconds;
00534 
00535     /// seconds
00536     typedef duration<int64_t>               seconds;
00537 
00538     /// minutes
00539     typedef duration<int64_t, ratio< 60>>   minutes;
00540 
00541     /// hours
00542     typedef duration<int64_t, ratio<3600>>  hours;
00543 
00544     /// time_point
00545     template<typename _Clock, typename _Dur>
00546       struct time_point
00547       {
00548         typedef _Clock                                          clock;
00549         typedef _Dur                                            duration;
00550         typedef typename duration::rep                          rep;
00551         typedef typename duration::period                       period;
00552 
00553         constexpr time_point() : __d(duration::zero())
00554         { }
00555 
00556         constexpr explicit time_point(const duration& __dur)
00557         : __d(__dur)
00558         { }
00559 
00560         // conversions
00561         template<typename _Dur2>
00562           constexpr time_point(const time_point<clock, _Dur2>& __t)
00563           : __d(__t.time_since_epoch())
00564           { }
00565 
00566         // observer
00567         constexpr duration
00568         time_since_epoch() const
00569         { return __d; }
00570 
00571         // arithmetic
00572         time_point&
00573         operator+=(const duration& __dur)
00574         {
00575           __d += __dur;
00576           return *this;
00577         }
00578 
00579         time_point&
00580         operator-=(const duration& __dur)
00581         {
00582           __d -= __dur;
00583           return *this;
00584         }
00585 
00586         // special values
00587         static constexpr time_point
00588         min()
00589         { return time_point(duration::min()); }
00590 
00591         static constexpr time_point
00592         max()
00593         { return time_point(duration::max()); }
00594 
00595       private:
00596         duration __d;
00597       };
00598 
00599     /// time_point_cast
00600     template<typename _ToDur, typename _Clock, typename _Dur>
00601       constexpr typename enable_if<__is_duration<_ToDur>::value,
00602                                    time_point<_Clock, _ToDur>>::type
00603       time_point_cast(const time_point<_Clock, _Dur>& __t)
00604       {
00605         typedef time_point<_Clock, _ToDur>                      __time_point;
00606         return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
00607       }
00608 
00609     template<typename _Clock, typename _Dur1,
00610              typename _Rep2, typename _Period2>
00611       constexpr time_point<_Clock,
00612         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
00613       operator+(const time_point<_Clock, _Dur1>& __lhs,
00614                 const duration<_Rep2, _Period2>& __rhs)
00615       {
00616         typedef duration<_Rep2, _Period2>                       __dur2;
00617         typedef typename common_type<_Dur1,__dur2>::type        __ct;
00618         typedef time_point<_Clock, __ct>                        __time_point;
00619         return __time_point(__lhs.time_since_epoch() + __rhs);
00620       }
00621 
00622     template<typename _Rep1, typename _Period1,
00623              typename _Clock, typename _Dur2>
00624       constexpr time_point<_Clock,
00625         typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
00626       operator+(const duration<_Rep1, _Period1>& __lhs,
00627                 const time_point<_Clock, _Dur2>& __rhs)
00628       { 
00629         typedef duration<_Rep1, _Period1>                       __dur1;
00630         typedef typename common_type<__dur1,_Dur2>::type        __ct;
00631         typedef time_point<_Clock, __ct>                        __time_point;
00632         return __time_point(__rhs.time_since_epoch() + __lhs); 
00633       }
00634 
00635     template<typename _Clock, typename _Dur1,
00636              typename _Rep2, typename _Period2>
00637       constexpr time_point<_Clock,
00638         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
00639       operator-(const time_point<_Clock, _Dur1>& __lhs,
00640                 const duration<_Rep2, _Period2>& __rhs)
00641       { 
00642         typedef duration<_Rep2, _Period2>                       __dur2;
00643         typedef typename common_type<_Dur1,__dur2>::type        __ct;
00644         typedef time_point<_Clock, __ct>                        __time_point;
00645         return __time_point(__lhs.time_since_epoch() -__rhs); 
00646       }
00647 
00648     template<typename _Clock, typename _Dur1, typename _Dur2>
00649       constexpr typename common_type<_Dur1, _Dur2>::type
00650       operator-(const time_point<_Clock, _Dur1>& __lhs,
00651                 const time_point<_Clock, _Dur2>& __rhs)
00652       { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
00653 
00654     template<typename _Clock, typename _Dur1, typename _Dur2>
00655       constexpr bool
00656       operator==(const time_point<_Clock, _Dur1>& __lhs,
00657                  const time_point<_Clock, _Dur2>& __rhs)
00658       { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
00659 
00660     template<typename _Clock, typename _Dur1, typename _Dur2>
00661       constexpr bool
00662       operator!=(const time_point<_Clock, _Dur1>& __lhs,
00663                  const time_point<_Clock, _Dur2>& __rhs)
00664       { return !(__lhs == __rhs); }
00665 
00666     template<typename _Clock, typename _Dur1, typename _Dur2>
00667       constexpr bool
00668       operator<(const time_point<_Clock, _Dur1>& __lhs,
00669                 const time_point<_Clock, _Dur2>& __rhs)
00670       { return  __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
00671 
00672     template<typename _Clock, typename _Dur1, typename _Dur2>
00673       constexpr bool
00674       operator<=(const time_point<_Clock, _Dur1>& __lhs,
00675                  const time_point<_Clock, _Dur2>& __rhs)
00676       { return !(__rhs < __lhs); }
00677 
00678     template<typename _Clock, typename _Dur1, typename _Dur2>
00679       constexpr bool
00680       operator>(const time_point<_Clock, _Dur1>& __lhs,
00681                 const time_point<_Clock, _Dur2>& __rhs)
00682       { return __rhs < __lhs; }
00683 
00684     template<typename _Clock, typename _Dur1, typename _Dur2>
00685       constexpr bool
00686       operator>=(const time_point<_Clock, _Dur1>& __lhs,
00687                  const time_point<_Clock, _Dur2>& __rhs)
00688       { return !(__lhs < __rhs); }
00689 
00690 
00691     // Clocks. 
00692 
00693     // Why nanosecond resolution as the default?  
00694     // Why have std::system_clock always count in the higest
00695     // resolution (ie nanoseconds), even if on some OSes the low 3
00696     // or 9 decimal digits will be always zero? This allows later
00697     // implementations to change the system_clock::now()
00698     // implementation any time to provide better resolution without
00699     // changing function signature or units.
00700 
00701     // To support the (forward) evolution of the library's defined
00702     // clocks, wrap inside inline namespace so that the current
00703     // defintions of system_clock, steady_clock, and
00704     // high_resolution_clock types are uniquely mangled. This way, new
00705     // code can use the latests clocks, while the library can contain
00706     // compatibility definitions for previous versions.  At some
00707     // point, when these clocks settle down, the inlined namespaces
00708     // can be removed.  XXX GLIBCXX_ABI Deprecated
00709     inline namespace _V2 {
00710 
00711     /**
00712      *  @brief System clock.
00713      *
00714      *  Time returned represents wall time from the system-wide clock.
00715     */
00716     struct system_clock
00717     {
00718       typedef chrono::nanoseconds                               duration;
00719       typedef duration::rep                                     rep;
00720       typedef duration::period                                  period;
00721       typedef chrono::time_point<system_clock, duration>        time_point;
00722 
00723       static_assert(system_clock::duration::min()
00724                     < system_clock::duration::zero(),
00725                     "a clock's minimum duration cannot be less than its epoch");
00726 
00727       static constexpr bool is_steady = false;
00728 
00729       static time_point
00730       now() noexcept;
00731 
00732       // Map to C API
00733       static std::time_t
00734       to_time_t(const time_point& __t) noexcept
00735       {
00736         return std::time_t(duration_cast<chrono::seconds>
00737                            (__t.time_since_epoch()).count());
00738       }
00739 
00740       static time_point
00741       from_time_t(std::time_t __t) noexcept
00742       {
00743         typedef chrono::time_point<system_clock, seconds>       __from;
00744         return time_point_cast<system_clock::duration>
00745                (__from(chrono::seconds(__t)));
00746       }
00747     };
00748 
00749 
00750     /**
00751      *  @brief Monotonic clock
00752      *
00753      *  Time returned has the property of only increasing at a uniform rate.
00754     */
00755     struct steady_clock
00756     {
00757       typedef chrono::nanoseconds                               duration;
00758       typedef duration::rep                                     rep;
00759       typedef duration::period                                  period;
00760       typedef chrono::time_point<steady_clock, duration>        time_point;
00761 
00762       static constexpr bool is_steady = true;
00763 
00764       static time_point
00765       now() noexcept;
00766     };
00767 
00768 
00769     /**
00770      *  @brief Highest-resolution clock
00771      *
00772      *  This is the clock "with the shortest tick period." Alias to
00773      *  std::system_clock until higher-than-nanosecond definitions
00774      *  become feasible.
00775     */
00776     using high_resolution_clock = system_clock;
00777 
00778     } // end inline namespace _V2
00779 
00780   _GLIBCXX_END_NAMESPACE_VERSION
00781   } // namespace chrono
00782 
00783 #if __cplusplus > 201103L
00784 
00785 #define __cpp_lib_chrono_udls 201304
00786 
00787   inline namespace literals
00788   {
00789   inline namespace chrono_literals
00790   {
00791 
00792     template<typename _Rep, unsigned long long _Val>
00793       struct _Checked_integral_constant
00794       : integral_constant<_Rep, static_cast<_Rep>(_Val)>
00795       {
00796         static_assert(_Checked_integral_constant::value >= 0
00797                       && _Checked_integral_constant::value == _Val,
00798                       "literal value cannot be represented by duration type");
00799       };
00800 
00801     template<typename _Dur, char... _Digits>
00802       constexpr _Dur __check_overflow()
00803       {
00804         using _Val = __parse_int::_Parse_int<_Digits...>;
00805         using _Rep = typename _Dur::rep;
00806         // TODO: should be simply integral_constant<_Rep, _Val::value>
00807         // but GCC doesn't reject narrowing conversions to _Rep.
00808         using _CheckedVal = _Checked_integral_constant<_Rep, _Val::value>;
00809         return _Dur{_CheckedVal::value};
00810       }
00811 
00812     constexpr chrono::duration<long double, ratio<3600,1>>
00813     operator""h(long double __hours)
00814     { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
00815 
00816     template <char... _Digits>
00817       constexpr chrono::hours
00818       operator""h()
00819       { return __check_overflow<chrono::hours, _Digits...>(); }
00820 
00821     constexpr chrono::duration<long double, ratio<60,1>>
00822     operator""min(long double __mins)
00823     { return chrono::duration<long double, ratio<60,1>>{__mins}; }
00824 
00825     template <char... _Digits>
00826       constexpr chrono::minutes
00827       operator""min()
00828       { return __check_overflow<chrono::minutes, _Digits...>(); }
00829 
00830     constexpr chrono::duration<long double>
00831     operator""s(long double __secs)
00832     { return chrono::duration<long double>{__secs}; }
00833 
00834     template <char... _Digits>
00835       constexpr chrono::seconds
00836       operator""s()
00837       { return __check_overflow<chrono::seconds, _Digits...>(); }
00838 
00839     constexpr chrono::duration<long double, milli>
00840     operator""ms(long double __msecs)
00841     { return chrono::duration<long double, milli>{__msecs}; }
00842 
00843     template <char... _Digits>
00844       constexpr chrono::milliseconds
00845       operator""ms()
00846       { return __check_overflow<chrono::milliseconds, _Digits...>(); }
00847 
00848     constexpr chrono::duration<long double, micro>
00849     operator""us(long double __usecs)
00850     { return chrono::duration<long double, micro>{__usecs}; }
00851 
00852     template <char... _Digits>
00853       constexpr chrono::microseconds
00854       operator""us()
00855       { return __check_overflow<chrono::microseconds, _Digits...>(); }
00856 
00857     constexpr chrono::duration<long double, nano>
00858     operator""ns(long double __nsecs)
00859     { return chrono::duration<long double, nano>{__nsecs}; }
00860 
00861     template <char... _Digits>
00862       constexpr chrono::nanoseconds
00863       operator""ns()
00864       { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
00865 
00866   } // inline namespace chrono_literals
00867   } // inline namespace literals
00868 
00869   namespace chrono
00870   {
00871   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00872 
00873   using namespace literals::chrono_literals;
00874 
00875   _GLIBCXX_END_NAMESPACE_VERSION
00876   } // namespace chrono
00877 
00878 #endif // __cplusplus > 201103L
00879 
00880   // @} group chrono
00881 } // namespace std
00882 
00883 #endif //_GLIBCXX_USE_C99_STDINT_TR1
00884 
00885 #endif // C++11
00886 
00887 #endif //_GLIBCXX_CHRONO