Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:24

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___CHRONO_CONVERT_TO_TIMESPEC_H
0011 #define _LIBCPP___CXX03___CHRONO_CONVERT_TO_TIMESPEC_H
0012 
0013 #include <__cxx03/__chrono/duration.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/limits>
0016 
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 #  pragma GCC system_header
0019 #endif
0020 
0021 _LIBCPP_PUSH_MACROS
0022 #include <__cxx03/__undef_macros>
0023 
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025 
0026 // Convert a nanoseconds duration to the given TimeSpec type, which must have
0027 // the same properties as std::timespec.
0028 template <class _TimeSpec>
0029 _LIBCPP_HIDE_FROM_ABI inline _TimeSpec __convert_to_timespec(const chrono::nanoseconds& __ns) {
0030   using namespace chrono;
0031   seconds __s = duration_cast<seconds>(__ns);
0032   _TimeSpec __ts;
0033   typedef decltype(__ts.tv_sec) __ts_sec;
0034   const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
0035 
0036   if (__s.count() < __ts_sec_max) {
0037     __ts.tv_sec  = static_cast<__ts_sec>(__s.count());
0038     __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
0039   } else {
0040     __ts.tv_sec  = __ts_sec_max;
0041     __ts.tv_nsec = 999999999; // (10^9 - 1)
0042   }
0043 
0044   return __ts;
0045 }
0046 
0047 _LIBCPP_END_NAMESPACE_STD
0048 
0049 _LIBCPP_POP_MACROS
0050 
0051 #endif // _LIBCPP___CXX03___CHRONO_CONVERT_TO_TIMESPEC_H