1*38fd1498Szrj // chrono -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2008-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj #include <bits/c++config.h> 26*38fd1498Szrj #include <chrono> 27*38fd1498Szrj 28*38fd1498Szrj #ifdef _GLIBCXX_USE_C99_STDINT_TR1 29*38fd1498Szrj 30*38fd1498Szrj // Conditional inclusion of sys/time.h for gettimeofday 31*38fd1498Szrj #if !defined(_GLIBCXX_USE_CLOCK_MONOTONIC) && \ 32*38fd1498Szrj !defined(_GLIBCXX_USE_CLOCK_REALTIME) && \ 33*38fd1498Szrj defined(_GLIBCXX_USE_GETTIMEOFDAY) 34*38fd1498Szrj #include <sys/time.h> 35*38fd1498Szrj #endif 36*38fd1498Szrj 37*38fd1498Szrj #ifdef _GLIBCXX_USE_CLOCK_GETTIME_SYSCALL 38*38fd1498Szrj #include <unistd.h> 39*38fd1498Szrj #include <sys/syscall.h> 40*38fd1498Szrj #endif 41*38fd1498Szrj 42*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 43*38fd1498Szrj { 44*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 45*38fd1498Szrj 46*38fd1498Szrj namespace chrono 47*38fd1498Szrj { 48*38fd1498Szrj // XXX GLIBCXX_ABI Deprecated 49*38fd1498Szrj inline namespace _V2 { 50*38fd1498Szrj 51*38fd1498Szrj constexpr bool system_clock::is_steady; 52*38fd1498Szrj 53*38fd1498Szrj system_clock::time_point now()54*38fd1498Szrj system_clock::now() noexcept 55*38fd1498Szrj { 56*38fd1498Szrj #ifdef _GLIBCXX_USE_CLOCK_REALTIME 57*38fd1498Szrj timespec tp; 58*38fd1498Szrj // -EINVAL, -EFAULT 59*38fd1498Szrj #ifdef _GLIBCXX_USE_CLOCK_GETTIME_SYSCALL 60*38fd1498Szrj syscall(SYS_clock_gettime, CLOCK_REALTIME, &tp); 61*38fd1498Szrj #else 62*38fd1498Szrj clock_gettime(CLOCK_REALTIME, &tp); 63*38fd1498Szrj #endif 64*38fd1498Szrj return time_point(duration(chrono::seconds(tp.tv_sec) 65*38fd1498Szrj + chrono::nanoseconds(tp.tv_nsec))); 66*38fd1498Szrj #elif defined(_GLIBCXX_USE_GETTIMEOFDAY) 67*38fd1498Szrj timeval tv; 68*38fd1498Szrj // EINVAL, EFAULT 69*38fd1498Szrj gettimeofday(&tv, 0); 70*38fd1498Szrj return time_point(duration(chrono::seconds(tv.tv_sec) 71*38fd1498Szrj + chrono::microseconds(tv.tv_usec))); 72*38fd1498Szrj #else 73*38fd1498Szrj std::time_t __sec = std::time(0); 74*38fd1498Szrj return system_clock::from_time_t(__sec); 75*38fd1498Szrj #endif 76*38fd1498Szrj } 77*38fd1498Szrj 78*38fd1498Szrj 79*38fd1498Szrj constexpr bool steady_clock::is_steady; 80*38fd1498Szrj 81*38fd1498Szrj steady_clock::time_point now()82*38fd1498Szrj steady_clock::now() noexcept 83*38fd1498Szrj { 84*38fd1498Szrj #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC 85*38fd1498Szrj timespec tp; 86*38fd1498Szrj // -EINVAL, -EFAULT 87*38fd1498Szrj #ifdef _GLIBCXX_USE_CLOCK_GETTIME_SYSCALL 88*38fd1498Szrj syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &tp); 89*38fd1498Szrj #else 90*38fd1498Szrj clock_gettime(CLOCK_MONOTONIC, &tp); 91*38fd1498Szrj #endif 92*38fd1498Szrj return time_point(duration(chrono::seconds(tp.tv_sec) 93*38fd1498Szrj + chrono::nanoseconds(tp.tv_nsec))); 94*38fd1498Szrj #else 95*38fd1498Szrj return time_point(system_clock::now().time_since_epoch()); 96*38fd1498Szrj #endif 97*38fd1498Szrj } 98*38fd1498Szrj 99*38fd1498Szrj } // end inline namespace _V2 100*38fd1498Szrj } // namespace chrono 101*38fd1498Szrj 102*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 103*38fd1498Szrj } // namespace std 104*38fd1498Szrj 105*38fd1498Szrj #endif // _GLIBCXX_USE_C99_STDINT_TR1 106