[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / libcxx / include / __chrono / convert_to_timespec.h
blob0106e6dec3e1c2c641f84e85b685bf99a5d02209
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-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
10 #define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
12 #include <__chrono/duration.h>
13 #include <__config>
14 #include <limits>
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
20 _LIBCPP_PUSH_MACROS
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);
33 _TimeSpec __ts;
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());
42 else
44 __ts.tv_sec = __ts_sec_max;
45 __ts.tv_nsec = 999999999; // (10^9 - 1)
48 return __ts;
51 _LIBCPP_END_NAMESPACE_STD
53 _LIBCPP_POP_MACROS
55 #endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H