xref: /freebsd-src/contrib/llvm-project/libcxx/include/__chrono/convert_to_timespec.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
104eeddc0SDimitry Andric // -*- C++ -*-
204eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
304eeddc0SDimitry Andric //
404eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
504eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
604eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
704eeddc0SDimitry Andric //
804eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
1004eeddc0SDimitry Andric #ifndef _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
1104eeddc0SDimitry Andric #define _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
1204eeddc0SDimitry Andric 
1304eeddc0SDimitry Andric #include <__chrono/duration.h>
1404eeddc0SDimitry Andric #include <__config>
1504eeddc0SDimitry Andric #include <limits>
1604eeddc0SDimitry Andric 
1704eeddc0SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1804eeddc0SDimitry Andric #  pragma GCC system_header
1904eeddc0SDimitry Andric #endif
2004eeddc0SDimitry Andric 
2104eeddc0SDimitry Andric _LIBCPP_PUSH_MACROS
2204eeddc0SDimitry Andric #include <__undef_macros>
2304eeddc0SDimitry Andric 
2404eeddc0SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2504eeddc0SDimitry Andric 
2604eeddc0SDimitry Andric // Convert a nanoseconds duration to the given TimeSpec type, which must have
2704eeddc0SDimitry Andric // the same properties as std::timespec.
2804eeddc0SDimitry Andric template <class _TimeSpec>
__convert_to_timespec(const chrono::nanoseconds & __ns)29*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline _TimeSpec __convert_to_timespec(const chrono::nanoseconds& __ns) {
3004eeddc0SDimitry Andric   using namespace chrono;
3104eeddc0SDimitry Andric   seconds __s = duration_cast<seconds>(__ns);
3204eeddc0SDimitry Andric   _TimeSpec __ts;
3304eeddc0SDimitry Andric   typedef decltype(__ts.tv_sec) __ts_sec;
3404eeddc0SDimitry Andric   const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
3504eeddc0SDimitry Andric 
36*cb14a3feSDimitry Andric   if (__s.count() < __ts_sec_max) {
3704eeddc0SDimitry Andric     __ts.tv_sec  = static_cast<__ts_sec>(__s.count());
3804eeddc0SDimitry Andric     __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
39*cb14a3feSDimitry Andric   } else {
4004eeddc0SDimitry Andric     __ts.tv_sec  = __ts_sec_max;
4104eeddc0SDimitry Andric     __ts.tv_nsec = 999999999; // (10^9 - 1)
4204eeddc0SDimitry Andric   }
4304eeddc0SDimitry Andric 
4404eeddc0SDimitry Andric   return __ts;
4504eeddc0SDimitry Andric }
4604eeddc0SDimitry Andric 
4704eeddc0SDimitry Andric _LIBCPP_END_NAMESPACE_STD
4804eeddc0SDimitry Andric 
4904eeddc0SDimitry Andric _LIBCPP_POP_MACROS
5004eeddc0SDimitry Andric 
5104eeddc0SDimitry Andric #endif // _LIBCPP___CHRONO_CONVERT_TO_TIMESPEC_H
52