|
| 1 | +//===--- clock conversion 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_CLOCK_CONVERSION_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_CONVERSION_H |
| 11 | + |
| 12 | +#include "src/__support/macros/config.h" |
| 13 | +#include "src/__support/time/clock_gettime.h" |
| 14 | +#include "src/__support/time/units.h" |
| 15 | + |
| 16 | +namespace LIBC_NAMESPACE_DECL { |
| 17 | +namespace internal { |
| 18 | + |
| 19 | +// @brief Convert a timespec value from one clock domain to another. |
| 20 | +// |
| 21 | +// The function takes a timestamp that is expressed in terms of the clock |
| 22 | +// identified by param from and returns an equivalent timestamp expressed |
| 23 | +// in terms of the clock identified by param to. |
| 24 | +// |
| 25 | +// Internally it obtains the current time of both clocks with |
| 26 | +// clock_gettime, then subtracts the source clock’s value and |
| 27 | +// adds the target clock’s value. The result is normalised so that |
| 28 | +// the nanoseconds field is always in the range [0, 1 s). |
| 29 | +// |
| 30 | +// This is useful, for example, for converting a value obtained from |
| 31 | +// CLOCK_MONOTONIC to CLOCK_REALTIME (or vice‑versa) so that the |
| 32 | +// timestamp can be displayed to a user or stored in a format that |
| 33 | +// is independent of the original clock domain. |
| 34 | +// |
| 35 | +// @param input The timestamp to convert |
| 36 | +// @param from Clock ID of the original timestamp (e.g. CLOCK_MONOTONIC). |
| 37 | +// @param to Clock ID of the desired timestamp (e.g. CLOCK_REALTIME). |
| 38 | +// @return The converted timespec |
| 39 | +// |
| 40 | +LIBC_INLINE timespec convert_clock(timespec input, clockid_t from, |
| 41 | + clockid_t to) { |
| 42 | + using namespace time_units; |
| 43 | + timespec from_time; |
| 44 | + timespec to_time; |
| 45 | + timespec output; |
| 46 | + internal::clock_gettime(from, &from_time); |
| 47 | + internal::clock_gettime(to, &to_time); |
| 48 | + output.tv_sec = input.tv_sec - from_time.tv_sec + to_time.tv_sec; |
| 49 | + output.tv_nsec = input.tv_nsec - from_time.tv_nsec + to_time.tv_nsec; |
| 50 | + |
| 51 | + if (output.tv_nsec > 1_s_ns) { |
| 52 | + output.tv_sec++; |
| 53 | + output.tv_nsec -= 1_s_ns; |
| 54 | + } else if (output.tv_nsec < 0) { |
| 55 | + output.tv_sec--; |
| 56 | + output.tv_nsec += 1_s_ns; |
| 57 | + } |
| 58 | + return output; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace internal |
| 62 | +} // namespace LIBC_NAMESPACE_DECL |
| 63 | + |
| 64 | +#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_CONVERSION_H |
0 commit comments