|
1 | | -//===--- clock conversion linux implementation ------------------*- C++ -*-===// |
| 1 | +//===--- clock conversion implementation ------------------*- C++ -*-===// |
2 | 2 | // |
3 | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | 4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | 6 | // |
7 | | -//===----------------------------------------------------------------------===// |
| 7 | +//===----------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H |
10 | 10 | #define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H |
|
16 | 16 | namespace LIBC_NAMESPACE_DECL { |
17 | 17 | namespace internal { |
18 | 18 |
|
| 19 | +/** |
| 20 | + * @brief Convert a timespec value from one clock domain to another. |
| 21 | + * |
| 22 | + * The function takes a timestamp that is expressed in terms of the clock |
| 23 | + * identified by param from and returns an equivalent timestamp expressed |
| 24 | + * in terms of the clock identified by param to. |
| 25 | + * |
| 26 | + * Internally it obtains the current time of both clocks with |
| 27 | + * clock_gettime, then subtracts the source clock’s value and |
| 28 | + * adds the target clock’s value. The result is normalised so that |
| 29 | + * the nanoseconds field is always in the range [0, 1 s). |
| 30 | + * |
| 31 | + * This is useful, for example, for converting a value obtained from |
| 32 | + * CLOCK_MONOTONIC to CLOCK_REALTIME (or vice‑versa) so that the |
| 33 | + * timestamp can be displayed to a user or stored in a format that |
| 34 | + * is independent of the original clock domain. |
| 35 | + * |
| 36 | + * @param input The timestamp to convert |
| 37 | + * @param from Clock ID of the original timestamp (e.g. CLOCK_MONOTONIC). |
| 38 | + * @param to Clock ID of the desired timestamp (e.g. CLOCK_REALTIME). |
| 39 | + * @return The converted timespec |
| 40 | + */ |
19 | 41 | LIBC_INLINE timespec convert_clock(timespec input, clockid_t from, |
20 | 42 | clockid_t to) { |
21 | 43 | using namespace time_units; |
|
0 commit comments