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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
11 #define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
13 #include <__chrono/duration.h>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
22 #include <__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 // Convert a nanoseconds duration to the given TimeSpec type, which must have
27 // the same properties as std::timespec.
28 template <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
);
33 typedef decltype(__ts
.tv_sec
) __ts_sec
;
34 const __ts_sec __ts_sec_max
= numeric_limits
<__ts_sec
>::max();
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());
40 __ts
.tv_sec
= __ts_sec_max
;
41 __ts
.tv_nsec
= 999999999; // (10^9 - 1)
47 _LIBCPP_END_NAMESPACE_STD
51 #endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H