libstdc++
chrono
Go to the documentation of this file.
00001 // <chrono> -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2017 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 #if __cplusplus > 201402L
00213     template <typename _Rep>
00214       inline constexpr bool treat_as_floating_point_v =
00215         treat_as_floating_point<_Rep>::value;
00216 #endif // C++17
00217 
00218 #if __cplusplus > 201402L
00219 # define __cpp_lib_chrono 201510
00220 
00221     template<typename _ToDur, typename _Rep, typename _Period>
00222       constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
00223       floor(const duration<_Rep, _Period>& __d)
00224       {
00225         auto __to = chrono::duration_cast<_ToDur>(__d);
00226         if (__to > __d)
00227           return __to - _ToDur{1};
00228         return __to;
00229       }
00230 
00231     template<typename _ToDur, typename _Rep, typename _Period>
00232       constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
00233       ceil(const duration<_Rep, _Period>& __d)
00234       {
00235         auto __to = chrono::duration_cast<_ToDur>(__d);
00236         if (__to < __d)
00237           return __to + _ToDur{1};
00238         return __to;
00239       }
00240 
00241     template <typename _ToDur, typename _Rep, typename _Period>
00242       constexpr enable_if_t<
00243         __and_<__is_duration<_ToDur>,
00244                __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
00245         _ToDur>
00246       round(const duration<_Rep, _Period>& __d)
00247       {
00248         _ToDur __t0 = chrono::floor<_ToDur>(__d);
00249         _ToDur __t1 = __t0 + _ToDur{1};
00250         auto __diff0 = __d - __t0;
00251         auto __diff1 = __t1 - __d;
00252         if (__diff0 == __diff1)
00253         {
00254             if (__t0.count() & 1)
00255                 return __t1;
00256             return __t0;
00257         }
00258         else if (__diff0 < __diff1)
00259             return __t0;
00260         return __t1;
00261       }
00262 
00263     template<typename _Rep, typename _Period>
00264       constexpr
00265       enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
00266       abs(duration<_Rep, _Period> __d)
00267       {
00268         if (__d >= __d.zero())
00269           return __d;
00270         return -__d;
00271       }
00272 #endif // C++17
00273 
00274     /// duration_values
00275     template<typename _Rep>
00276       struct duration_values
00277       {
00278         static constexpr _Rep
00279         zero()
00280         { return _Rep(0); }
00281 
00282         static constexpr _Rep
00283         max()
00284         { return numeric_limits<_Rep>::max(); }
00285 
00286         static constexpr _Rep
00287         min()
00288         { return numeric_limits<_Rep>::lowest(); }
00289       };
00290 
00291     template<typename _Tp>
00292       struct __is_ratio
00293       : std::false_type
00294       { };
00295 
00296     template<intmax_t _Num, intmax_t _Den>
00297       struct __is_ratio<ratio<_Num, _Den>>
00298       : std::true_type
00299       { };
00300 
00301     /// duration
00302     template<typename _Rep, typename _Period>
00303       struct duration
00304       {
00305         typedef _Rep                                            rep;
00306         typedef _Period                                         period;
00307 
00308         static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
00309         static_assert(__is_ratio<_Period>::value,
00310                       "period must be a specialization of ratio");
00311         static_assert(_Period::num > 0, "period must be positive");
00312 
00313         // 20.11.5.1 construction / copy / destroy
00314         constexpr duration() = default;
00315 
00316         // NB: Make constexpr implicit. This cannot be explicitly
00317         // constexpr, as any UDT that is not a literal type with a
00318         // constexpr copy constructor will be ill-formed.
00319         duration(const duration&) = default;
00320 
00321         template<typename _Rep2, typename = typename
00322                enable_if<is_convertible<_Rep2, rep>::value
00323                          && (treat_as_floating_point<rep>::value
00324                              || !treat_as_floating_point<_Rep2>::value)>::type>
00325           constexpr explicit duration(const _Rep2& __rep)
00326           : __r(static_cast<rep>(__rep)) { }
00327 
00328         template<typename _Rep2, typename _Period2, typename = typename
00329                enable_if<treat_as_floating_point<rep>::value
00330                          || (ratio_divide<_Period2, period>::den == 1
00331                              && !treat_as_floating_point<_Rep2>::value)>::type>
00332           constexpr duration(const duration<_Rep2, _Period2>& __d)
00333           : __r(duration_cast<duration>(__d).count()) { }
00334 
00335         ~duration() = default;
00336         duration& operator=(const duration&) = default;
00337 
00338         // 20.11.5.2 observer
00339         constexpr rep
00340         count() const
00341         { return __r; }
00342 
00343         // 20.11.5.3 arithmetic
00344         constexpr duration
00345         operator+() const
00346         { return *this; }
00347 
00348         constexpr duration
00349         operator-() const
00350         { return duration(-__r); }
00351 
00352         _GLIBCXX17_CONSTEXPR duration&
00353         operator++()
00354         {
00355           ++__r;
00356           return *this;
00357         }
00358 
00359         _GLIBCXX17_CONSTEXPR duration
00360         operator++(int)
00361         { return duration(__r++); }
00362 
00363         _GLIBCXX17_CONSTEXPR duration&
00364         operator--()
00365         {
00366           --__r;
00367           return *this;
00368         }
00369 
00370         _GLIBCXX17_CONSTEXPR duration
00371         operator--(int)
00372         { return duration(__r--); }
00373 
00374         _GLIBCXX17_CONSTEXPR duration&
00375         operator+=(const duration& __d)
00376         {
00377           __r += __d.count();
00378           return *this;
00379         }
00380 
00381         _GLIBCXX17_CONSTEXPR duration&
00382         operator-=(const duration& __d)
00383         {
00384           __r -= __d.count();
00385           return *this;
00386         }
00387 
00388         _GLIBCXX17_CONSTEXPR duration&
00389         operator*=(const rep& __rhs)
00390         {
00391           __r *= __rhs;
00392           return *this;
00393         }
00394 
00395         _GLIBCXX17_CONSTEXPR duration&
00396         operator/=(const rep& __rhs)
00397         {
00398           __r /= __rhs;
00399           return *this;
00400         }
00401 
00402         // DR 934.
00403         template<typename _Rep2 = rep>
00404           _GLIBCXX17_CONSTEXPR
00405           typename enable_if<!treat_as_floating_point<_Rep2>::value,
00406                              duration&>::type
00407           operator%=(const rep& __rhs)
00408           {
00409             __r %= __rhs;
00410             return *this;
00411           }
00412 
00413         template<typename _Rep2 = rep>
00414           _GLIBCXX17_CONSTEXPR
00415           typename enable_if<!treat_as_floating_point<_Rep2>::value,
00416                              duration&>::type
00417           operator%=(const duration& __d)
00418           {
00419             __r %= __d.count();
00420             return *this;
00421           }
00422 
00423         // 20.11.5.4 special values
00424         static constexpr duration
00425         zero()
00426         { return duration(duration_values<rep>::zero()); }
00427 
00428         static constexpr duration
00429         min()
00430         { return duration(duration_values<rep>::min()); }
00431 
00432         static constexpr duration
00433         max()
00434         { return duration(duration_values<rep>::max()); }
00435 
00436       private:
00437         rep __r;
00438       };
00439 
00440     template<typename _Rep1, typename _Period1,
00441              typename _Rep2, typename _Period2>
00442       constexpr typename common_type<duration<_Rep1, _Period1>,
00443                                      duration<_Rep2, _Period2>>::type
00444       operator+(const duration<_Rep1, _Period1>& __lhs,
00445                 const duration<_Rep2, _Period2>& __rhs)
00446       {
00447         typedef duration<_Rep1, _Period1>                       __dur1;
00448         typedef duration<_Rep2, _Period2>                       __dur2;
00449         typedef typename common_type<__dur1,__dur2>::type       __cd;
00450         return __cd(__cd(__lhs).count() + __cd(__rhs).count());
00451       }
00452 
00453     template<typename _Rep1, typename _Period1,
00454              typename _Rep2, typename _Period2>
00455       constexpr typename common_type<duration<_Rep1, _Period1>,
00456                                      duration<_Rep2, _Period2>>::type
00457       operator-(const duration<_Rep1, _Period1>& __lhs,
00458                 const duration<_Rep2, _Period2>& __rhs)
00459       {
00460         typedef duration<_Rep1, _Period1>                       __dur1;
00461         typedef duration<_Rep2, _Period2>                       __dur2;
00462         typedef typename common_type<__dur1,__dur2>::type       __cd;
00463         return __cd(__cd(__lhs).count() - __cd(__rhs).count());
00464       }
00465 
00466     template<typename _Rep1, typename _Rep2, bool =
00467              is_convertible<_Rep2,
00468                             typename common_type<_Rep1, _Rep2>::type>::value>
00469       struct __common_rep_type { };
00470 
00471     template<typename _Rep1, typename _Rep2>
00472       struct __common_rep_type<_Rep1, _Rep2, true>
00473       { typedef typename common_type<_Rep1, _Rep2>::type type; };
00474 
00475     template<typename _Rep1, typename _Period, typename _Rep2>
00476       constexpr
00477       duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
00478       operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00479       {
00480         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00481           __cd;
00482         return __cd(__cd(__d).count() * __s);
00483       }
00484 
00485     template<typename _Rep1, typename _Rep2, typename _Period>
00486       constexpr
00487       duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
00488       operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
00489       { return __d * __s; }
00490 
00491     template<typename _Rep1, typename _Period, typename _Rep2>
00492       constexpr duration<typename __common_rep_type<_Rep1, typename
00493         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00494       operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00495       {
00496         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00497           __cd;
00498         return __cd(__cd(__d).count() / __s);
00499       }
00500 
00501     template<typename _Rep1, typename _Period1,
00502              typename _Rep2, typename _Period2>
00503       constexpr typename common_type<_Rep1, _Rep2>::type
00504       operator/(const duration<_Rep1, _Period1>& __lhs,
00505                 const duration<_Rep2, _Period2>& __rhs)
00506       {
00507         typedef duration<_Rep1, _Period1>                       __dur1;
00508         typedef duration<_Rep2, _Period2>                       __dur2;
00509         typedef typename common_type<__dur1,__dur2>::type       __cd;
00510         return __cd(__lhs).count() / __cd(__rhs).count();
00511       }
00512 
00513     // DR 934.
00514     template<typename _Rep1, typename _Period, typename _Rep2>
00515       constexpr duration<typename __common_rep_type<_Rep1, typename
00516         enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00517       operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00518       {
00519         typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
00520           __cd;
00521         return __cd(__cd(__d).count() % __s);
00522       }
00523 
00524     template<typename _Rep1, typename _Period1,
00525              typename _Rep2, typename _Period2>
00526       constexpr typename common_type<duration<_Rep1, _Period1>,
00527                                      duration<_Rep2, _Period2>>::type
00528       operator%(const duration<_Rep1, _Period1>& __lhs,
00529                 const duration<_Rep2, _Period2>& __rhs)
00530       {
00531         typedef duration<_Rep1, _Period1>                       __dur1;
00532         typedef duration<_Rep2, _Period2>                       __dur2;
00533         typedef typename common_type<__dur1,__dur2>::type       __cd;
00534         return __cd(__cd(__lhs).count() % __cd(__rhs).count());
00535       }
00536 
00537     // comparisons
00538     template<typename _Rep1, typename _Period1,
00539              typename _Rep2, typename _Period2>
00540       constexpr bool
00541       operator==(const duration<_Rep1, _Period1>& __lhs,
00542                  const duration<_Rep2, _Period2>& __rhs)
00543       {
00544         typedef duration<_Rep1, _Period1>                       __dur1;
00545         typedef duration<_Rep2, _Period2>                       __dur2;
00546         typedef typename common_type<__dur1,__dur2>::type       __ct;
00547         return __ct(__lhs).count() == __ct(__rhs).count();
00548       }
00549 
00550     template<typename _Rep1, typename _Period1,
00551              typename _Rep2, typename _Period2>
00552       constexpr bool
00553       operator<(const duration<_Rep1, _Period1>& __lhs,
00554                 const duration<_Rep2, _Period2>& __rhs)
00555       {
00556         typedef duration<_Rep1, _Period1>                       __dur1;
00557         typedef duration<_Rep2, _Period2>                       __dur2;
00558         typedef typename common_type<__dur1,__dur2>::type       __ct;
00559         return __ct(__lhs).count() < __ct(__rhs).count();
00560       }
00561 
00562     template<typename _Rep1, typename _Period1,
00563              typename _Rep2, typename _Period2>
00564       constexpr bool
00565       operator!=(const duration<_Rep1, _Period1>& __lhs,
00566                  const duration<_Rep2, _Period2>& __rhs)
00567       { return !(__lhs == __rhs); }
00568 
00569     template<typename _Rep1, typename _Period1,
00570              typename _Rep2, typename _Period2>
00571       constexpr bool
00572       operator<=(const duration<_Rep1, _Period1>& __lhs,
00573                  const duration<_Rep2, _Period2>& __rhs)
00574       { return !(__rhs < __lhs); }
00575 
00576     template<typename _Rep1, typename _Period1,
00577              typename _Rep2, typename _Period2>
00578       constexpr bool
00579       operator>(const duration<_Rep1, _Period1>& __lhs,
00580                 const duration<_Rep2, _Period2>& __rhs)
00581       { return __rhs < __lhs; }
00582 
00583     template<typename _Rep1, typename _Period1,
00584              typename _Rep2, typename _Period2>
00585       constexpr bool
00586       operator>=(const duration<_Rep1, _Period1>& __lhs,
00587                  const duration<_Rep2, _Period2>& __rhs)
00588       { return !(__lhs < __rhs); }
00589 
00590     /// nanoseconds
00591     typedef duration<int64_t, nano>         nanoseconds;
00592 
00593     /// microseconds
00594     typedef duration<int64_t, micro>        microseconds;
00595 
00596     /// milliseconds
00597     typedef duration<int64_t, milli>        milliseconds;
00598 
00599     /// seconds
00600     typedef duration<int64_t>               seconds;
00601 
00602     /// minutes
00603     typedef duration<int64_t, ratio< 60>>   minutes;
00604 
00605     /// hours
00606     typedef duration<int64_t, ratio<3600>>  hours;
00607 
00608     /// time_point
00609     template<typename _Clock, typename _Dur>
00610       struct time_point
00611       {
00612         typedef _Clock                                          clock;
00613         typedef _Dur                                            duration;
00614         typedef typename duration::rep                          rep;
00615         typedef typename duration::period                       period;
00616 
00617         constexpr time_point() : __d(duration::zero())
00618         { }
00619 
00620         constexpr explicit time_point(const duration& __dur)
00621         : __d(__dur)
00622         { }
00623 
00624         // conversions
00625         template<typename _Dur2>
00626           constexpr time_point(const time_point<clock, _Dur2>& __t)
00627           : __d(__t.time_since_epoch())
00628           { }
00629 
00630         // observer
00631         constexpr duration
00632         time_since_epoch() const
00633         { return __d; }
00634 
00635         // arithmetic
00636         _GLIBCXX17_CONSTEXPR time_point&
00637         operator+=(const duration& __dur)
00638         {
00639           __d += __dur;
00640           return *this;
00641         }
00642 
00643         _GLIBCXX17_CONSTEXPR time_point&
00644         operator-=(const duration& __dur)
00645         {
00646           __d -= __dur;
00647           return *this;
00648         }
00649 
00650         // special values
00651         static constexpr time_point
00652         min()
00653         { return time_point(duration::min()); }
00654 
00655         static constexpr time_point
00656         max()
00657         { return time_point(duration::max()); }
00658 
00659       private:
00660         duration __d;
00661       };
00662 
00663     /// time_point_cast
00664     template<typename _ToDur, typename _Clock, typename _Dur>
00665       constexpr typename enable_if<__is_duration<_ToDur>::value,
00666                                    time_point<_Clock, _ToDur>>::type
00667       time_point_cast(const time_point<_Clock, _Dur>& __t)
00668       {
00669         typedef time_point<_Clock, _ToDur>                      __time_point;
00670         return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
00671       }
00672 
00673 #if __cplusplus > 201402L
00674     template<typename _ToDur, typename _Clock, typename _Dur>
00675       constexpr
00676       enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
00677       floor(const time_point<_Clock, _Dur>& __tp)
00678       {
00679         return time_point<_Clock, _ToDur>{
00680             chrono::floor<_ToDur>(__tp.time_since_epoch())};
00681       }
00682 
00683     template<typename _ToDur, typename _Clock, typename _Dur>
00684       constexpr
00685       enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
00686       ceil(const time_point<_Clock, _Dur>& __tp)
00687       {
00688         return time_point<_Clock, _ToDur>{
00689             chrono::ceil<_ToDur>(__tp.time_since_epoch())};
00690       }
00691 
00692     template<typename _ToDur, typename _Clock, typename _Dur>
00693       constexpr enable_if_t<
00694         __and_<__is_duration<_ToDur>,
00695                __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
00696         time_point<_Clock, _ToDur>>
00697       round(const time_point<_Clock, _Dur>& __tp)
00698       {
00699         return time_point<_Clock, _ToDur>{
00700             chrono::round<_ToDur>(__tp.time_since_epoch())};
00701       }
00702 #endif // C++17
00703 
00704     template<typename _Clock, typename _Dur1,
00705              typename _Rep2, typename _Period2>
00706       constexpr time_point<_Clock,
00707         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
00708       operator+(const time_point<_Clock, _Dur1>& __lhs,
00709                 const duration<_Rep2, _Period2>& __rhs)
00710       {
00711         typedef duration<_Rep2, _Period2>                       __dur2;
00712         typedef typename common_type<_Dur1,__dur2>::type        __ct;
00713         typedef time_point<_Clock, __ct>                        __time_point;
00714         return __time_point(__lhs.time_since_epoch() + __rhs);
00715       }
00716 
00717     template<typename _Rep1, typename _Period1,
00718              typename _Clock, typename _Dur2>
00719       constexpr time_point<_Clock,
00720         typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
00721       operator+(const duration<_Rep1, _Period1>& __lhs,
00722                 const time_point<_Clock, _Dur2>& __rhs)
00723       {
00724         typedef duration<_Rep1, _Period1>                       __dur1;
00725         typedef typename common_type<__dur1,_Dur2>::type        __ct;
00726         typedef time_point<_Clock, __ct>                        __time_point;
00727         return __time_point(__rhs.time_since_epoch() + __lhs);
00728       }
00729 
00730     template<typename _Clock, typename _Dur1,
00731              typename _Rep2, typename _Period2>
00732       constexpr time_point<_Clock,
00733         typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
00734       operator-(const time_point<_Clock, _Dur1>& __lhs,
00735                 const duration<_Rep2, _Period2>& __rhs)
00736       {
00737         typedef duration<_Rep2, _Period2>                       __dur2;
00738         typedef typename common_type<_Dur1,__dur2>::type        __ct;
00739         typedef time_point<_Clock, __ct>                        __time_point;
00740         return __time_point(__lhs.time_since_epoch() -__rhs);
00741       }
00742 
00743     template<typename _Clock, typename _Dur1, typename _Dur2>
00744       constexpr typename common_type<_Dur1, _Dur2>::type
00745       operator-(const time_point<_Clock, _Dur1>& __lhs,
00746                 const time_point<_Clock, _Dur2>& __rhs)
00747       { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
00748 
00749     template<typename _Clock, typename _Dur1, typename _Dur2>
00750       constexpr bool
00751       operator==(const time_point<_Clock, _Dur1>& __lhs,
00752                  const time_point<_Clock, _Dur2>& __rhs)
00753       { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
00754 
00755     template<typename _Clock, typename _Dur1, typename _Dur2>
00756       constexpr bool
00757       operator!=(const time_point<_Clock, _Dur1>& __lhs,
00758                  const time_point<_Clock, _Dur2>& __rhs)
00759       { return !(__lhs == __rhs); }
00760 
00761     template<typename _Clock, typename _Dur1, typename _Dur2>
00762       constexpr bool
00763       operator<(const time_point<_Clock, _Dur1>& __lhs,
00764                 const time_point<_Clock, _Dur2>& __rhs)
00765       { return  __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
00766 
00767     template<typename _Clock, typename _Dur1, typename _Dur2>
00768       constexpr bool
00769       operator<=(const time_point<_Clock, _Dur1>& __lhs,
00770                  const time_point<_Clock, _Dur2>& __rhs)
00771       { return !(__rhs < __lhs); }
00772 
00773     template<typename _Clock, typename _Dur1, typename _Dur2>
00774       constexpr bool
00775       operator>(const time_point<_Clock, _Dur1>& __lhs,
00776                 const time_point<_Clock, _Dur2>& __rhs)
00777       { return __rhs < __lhs; }
00778 
00779     template<typename _Clock, typename _Dur1, typename _Dur2>
00780       constexpr bool
00781       operator>=(const time_point<_Clock, _Dur1>& __lhs,
00782                  const time_point<_Clock, _Dur2>& __rhs)
00783       { return !(__lhs < __rhs); }
00784 
00785 
00786     // Clocks.
00787 
00788     // Why nanosecond resolution as the default?
00789     // Why have std::system_clock always count in the highest
00790     // resolution (ie nanoseconds), even if on some OSes the low 3
00791     // or 9 decimal digits will be always zero? This allows later
00792     // implementations to change the system_clock::now()
00793     // implementation any time to provide better resolution without
00794     // changing function signature or units.
00795 
00796     // To support the (forward) evolution of the library's defined
00797     // clocks, wrap inside inline namespace so that the current
00798     // defintions of system_clock, steady_clock, and
00799     // high_resolution_clock types are uniquely mangled. This way, new
00800     // code can use the latests clocks, while the library can contain
00801     // compatibility definitions for previous versions.  At some
00802     // point, when these clocks settle down, the inlined namespaces
00803     // can be removed.  XXX GLIBCXX_ABI Deprecated
00804     inline namespace _V2 {
00805 
00806     /**
00807      *  @brief System clock.
00808      *
00809      *  Time returned represents wall time from the system-wide clock.
00810     */
00811     struct system_clock
00812     {
00813       typedef chrono::nanoseconds                               duration;
00814       typedef duration::rep                                     rep;
00815       typedef duration::period                                  period;
00816       typedef chrono::time_point<system_clock, duration>        time_point;
00817 
00818       static_assert(system_clock::duration::min()
00819                     < system_clock::duration::zero(),
00820                     "a clock's minimum duration cannot be less than its epoch");
00821 
00822       static constexpr bool is_steady = false;
00823 
00824       static time_point
00825       now() noexcept;
00826 
00827       // Map to C API
00828       static std::time_t
00829       to_time_t(const time_point& __t) noexcept
00830       {
00831         return std::time_t(duration_cast<chrono::seconds>
00832                            (__t.time_since_epoch()).count());
00833       }
00834 
00835       static time_point
00836       from_time_t(std::time_t __t) noexcept
00837       {
00838         typedef chrono::time_point<system_clock, seconds>       __from;
00839         return time_point_cast<system_clock::duration>
00840                (__from(chrono::seconds(__t)));
00841       }
00842     };
00843 
00844 
00845     /**
00846      *  @brief Monotonic clock
00847      *
00848      *  Time returned has the property of only increasing at a uniform rate.
00849     */
00850     struct steady_clock
00851     {
00852       typedef chrono::nanoseconds                               duration;
00853       typedef duration::rep                                     rep;
00854       typedef duration::period                                  period;
00855       typedef chrono::time_point<steady_clock, duration>        time_point;
00856 
00857       static constexpr bool is_steady = true;
00858 
00859       static time_point
00860       now() noexcept;
00861     };
00862 
00863 
00864     /**
00865      *  @brief Highest-resolution clock
00866      *
00867      *  This is the clock "with the shortest tick period." Alias to
00868      *  std::system_clock until higher-than-nanosecond definitions
00869      *  become feasible.
00870     */
00871     using high_resolution_clock = system_clock;
00872 
00873     } // end inline namespace _V2
00874 
00875   _GLIBCXX_END_NAMESPACE_VERSION
00876   } // namespace chrono
00877 
00878 #if __cplusplus > 201103L
00879 
00880 #define __cpp_lib_chrono_udls 201304
00881 
00882   inline namespace literals
00883   {
00884   inline namespace chrono_literals
00885   {
00886   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00887 
00888     template<typename _Rep, unsigned long long _Val>
00889       struct _Checked_integral_constant
00890       : integral_constant<_Rep, static_cast<_Rep>(_Val)>
00891       {
00892         static_assert(_Checked_integral_constant::value >= 0
00893                       && _Checked_integral_constant::value == _Val,
00894                       "literal value cannot be represented by duration type");
00895       };
00896 
00897     template<typename _Dur, char... _Digits>
00898       constexpr _Dur __check_overflow()
00899       {
00900         using _Val = __parse_int::_Parse_int<_Digits...>;
00901         using _Rep = typename _Dur::rep;
00902         // TODO: should be simply integral_constant<_Rep, _Val::value>
00903         // but GCC doesn't reject narrowing conversions to _Rep.
00904         using _CheckedVal = _Checked_integral_constant<_Rep, _Val::value>;
00905         return _Dur{_CheckedVal::value};
00906       }
00907 
00908     constexpr chrono::duration<long double, ratio<3600,1>>
00909     operator""h(long double __hours)
00910     { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
00911 
00912     template <char... _Digits>
00913       constexpr chrono::hours
00914       operator""h()
00915       { return __check_overflow<chrono::hours, _Digits...>(); }
00916 
00917     constexpr chrono::duration<long double, ratio<60,1>>
00918     operator""min(long double __mins)
00919     { return chrono::duration<long double, ratio<60,1>>{__mins}; }
00920 
00921     template <char... _Digits>
00922       constexpr chrono::minutes
00923       operator""min()
00924       { return __check_overflow<chrono::minutes, _Digits...>(); }
00925 
00926     constexpr chrono::duration<long double>
00927     operator""s(long double __secs)
00928     { return chrono::duration<long double>{__secs}; }
00929 
00930     template <char... _Digits>
00931       constexpr chrono::seconds
00932       operator""s()
00933       { return __check_overflow<chrono::seconds, _Digits...>(); }
00934 
00935     constexpr chrono::duration<long double, milli>
00936     operator""ms(long double __msecs)
00937     { return chrono::duration<long double, milli>{__msecs}; }
00938 
00939     template <char... _Digits>
00940       constexpr chrono::milliseconds
00941       operator""ms()
00942       { return __check_overflow<chrono::milliseconds, _Digits...>(); }
00943 
00944     constexpr chrono::duration<long double, micro>
00945     operator""us(long double __usecs)
00946     { return chrono::duration<long double, micro>{__usecs}; }
00947 
00948     template <char... _Digits>
00949       constexpr chrono::microseconds
00950       operator""us()
00951       { return __check_overflow<chrono::microseconds, _Digits...>(); }
00952 
00953     constexpr chrono::duration<long double, nano>
00954     operator""ns(long double __nsecs)
00955     { return chrono::duration<long double, nano>{__nsecs}; }
00956 
00957     template <char... _Digits>
00958       constexpr chrono::nanoseconds
00959       operator""ns()
00960       { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
00961 
00962   _GLIBCXX_END_NAMESPACE_VERSION
00963   } // inline namespace chrono_literals
00964   } // inline namespace literals
00965 
00966   namespace chrono
00967   {
00968   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00969 
00970   using namespace literals::chrono_literals;
00971 
00972   _GLIBCXX_END_NAMESPACE_VERSION
00973   } // namespace chrono
00974 
00975 #endif // __cplusplus > 201103L
00976 
00977   // @} group chrono
00978 } // namespace std
00979 
00980 #endif //_GLIBCXX_USE_C99_STDINT_TR1
00981 
00982 #endif // C++11
00983 
00984 #endif //_GLIBCXX_CHRONO