Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/convert_to_timespec.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H10#define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H1112#include <__chrono/duration.h>13#include <__config>14#include <limits>1516#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif1920_LIBCPP_PUSH_MACROS21#include <__undef_macros>2223_LIBCPP_BEGIN_NAMESPACE_STD2425// Convert a nanoseconds duration to the given TimeSpec type, which must have26// the same properties as std::timespec.27template <class _TimeSpec>28_LIBCPP_HIDE_FROM_ABI inline _TimeSpec __convert_to_timespec(const chrono::nanoseconds& __ns) {29using namespace chrono;30seconds __s = duration_cast<seconds>(__ns);31_TimeSpec __ts;32typedef decltype(__ts.tv_sec) __ts_sec;33const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();3435if (__s.count() < __ts_sec_max) {36__ts.tv_sec = static_cast<__ts_sec>(__s.count());37__ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());38} else {39__ts.tv_sec = __ts_sec_max;40__ts.tv_nsec = 999999999; // (10^9 - 1)41}4243return __ts;44}4546_LIBCPP_END_NAMESPACE_STD4748_LIBCPP_POP_MACROS4950#endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H515253