52 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H11#define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H12 13#include <__chrono/duration.h>14#include <__config>15#include <limits>16 17#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18# pragma GCC system_header19#endif20 21_LIBCPP_PUSH_MACROS22#include <__undef_macros>23 24_LIBCPP_BEGIN_NAMESPACE_STD25 26// Convert a nanoseconds duration to the given TimeSpec type, which must have27// the same properties as std::timespec.28template <class _TimeSpec>29_LIBCPP_HIDE_FROM_ABI inline _TimeSpec __convert_to_timespec(const chrono::nanoseconds& __ns) {30 using namespace chrono;31 seconds __s = duration_cast<seconds>(__ns);32 _TimeSpec __ts;33 typedef decltype(__ts.tv_sec) __ts_sec;34 const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();35 36 if (__s.count() < __ts_sec_max) {37 __ts.tv_sec = static_cast<__ts_sec>(__s.count());38 __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());39 } else {40 __ts.tv_sec = __ts_sec_max;41 __ts.tv_nsec = 999999999; // (10^9 - 1)42 }43 44 return __ts;45}46 47_LIBCPP_END_NAMESPACE_STD48 49_LIBCPP_POP_MACROS50 51#endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H52