2 //===----------------------------------------------------------------------===//
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-exception
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
10 #define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
12 #include <__chrono/duration.h>
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
21 #include <__undef_macros>
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 // Convert a nanoseconds duration to the given TimeSpec type, which must have
26 // the same properties as std::timespec.
27 template <class _TimeSpec
>
28 _LIBCPP_HIDE_FROM_ABI
inline
29 _TimeSpec
__convert_to_timespec(const chrono::nanoseconds
& __ns
)
31 using namespace chrono
;
32 seconds __s
= duration_cast
<seconds
>(__ns
);
34 typedef decltype(__ts
.tv_sec
) __ts_sec
;
35 const __ts_sec __ts_sec_max
= numeric_limits
<__ts_sec
>::max();
37 if (__s
.count() < __ts_sec_max
)
39 __ts
.tv_sec
= static_cast<__ts_sec
>(__s
.count());
40 __ts
.tv_nsec
= static_cast<decltype(__ts
.tv_nsec
)>((__ns
- __s
).count());
44 __ts
.tv_sec
= __ts_sec_max
;
45 __ts
.tv_nsec
= 999999999; // (10^9 - 1)
51 _LIBCPP_END_NAMESPACE_STD
55 #endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H