• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- clock conversion linux implementation ------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
10 #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
11 
12 #include "src/__support/time/linux/clock_gettime.h"
13 #include "src/__support/time/units.h"
14 
15 namespace LIBC_NAMESPACE {
16 namespace internal {
17 
convert_clock(timespec input,clockid_t from,clockid_t to)18 LIBC_INLINE timespec convert_clock(timespec input, clockid_t from,
19                                    clockid_t to) {
20   using namespace time_units;
21   timespec from_time;
22   timespec to_time;
23   timespec output;
24   internal::clock_gettime(from, &from_time);
25   internal::clock_gettime(to, &to_time);
26   output.tv_sec = input.tv_sec - from_time.tv_sec + to_time.tv_sec;
27   output.tv_nsec = input.tv_nsec - from_time.tv_nsec + to_time.tv_nsec;
28 
29   if (output.tv_nsec > 1_s_ns) {
30     output.tv_sec++;
31     output.tv_nsec -= 1_s_ns;
32   } else if (output.tv_nsec < 0) {
33     output.tv_sec--;
34     output.tv_nsec += 1_s_ns;
35   }
36   return output;
37 }
38 
39 } // namespace internal
40 } // namespace LIBC_NAMESPACE
41 
42 #endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
43