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