1 //===--- clock conversion linux implementation ------------------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
10 #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
12 #include "src/__support/macros/config.h"
13 #include "src/__support/time/linux/clock_gettime.h"
14 #include "src/__support/time/units.h"
16 namespace LIBC_NAMESPACE_DECL
{
19 LIBC_INLINE timespec
convert_clock(timespec input
, clockid_t from
,
21 using namespace time_units
;
25 internal::clock_gettime(from
, &from_time
);
26 internal::clock_gettime(to
, &to_time
);
27 output
.tv_sec
= input
.tv_sec
- from_time
.tv_sec
+ to_time
.tv_sec
;
28 output
.tv_nsec
= input
.tv_nsec
- from_time
.tv_nsec
+ to_time
.tv_nsec
;
30 if (output
.tv_nsec
> 1_s_ns
) {
32 output
.tv_nsec
-= 1_s_ns
;
33 } else if (output
.tv_nsec
< 0) {
35 output
.tv_nsec
+= 1_s_ns
;
40 } // namespace internal
41 } // namespace LIBC_NAMESPACE_DECL
43 #endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H