Skip to content

Commit d6c1c4c

Browse files
committed
[libc] make clock_conversion.h common and document it
clock_conversion.h implements convert_clock which shifts a timestamp from one clock domain to another. It naturally does not depend on any OS specific interface. Making it generic will allow common use.
1 parent ee3d5aa commit d6c1c4c

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

libc/src/__support/time/linux/clock_conversion.h renamed to libc/src/__support/time/clock_conversion.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//===--- clock conversion linux implementation ------------------*- C++ -*-===//
1+
//===--- clock conversion implementation ------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===----------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------===//
88

99
#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
1010
#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
@@ -16,6 +16,28 @@
1616
namespace LIBC_NAMESPACE_DECL {
1717
namespace internal {
1818

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+
*/
1941
LIBC_INLINE timespec convert_clock(timespec input, clockid_t from,
2042
clockid_t to) {
2143
using namespace time_units;

libc/src/__support/time/linux/monotonicity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "hdr/time_macros.h"
1313
#include "src/__support/libc_assert.h"
1414
#include "src/__support/macros/config.h"
15+
#include "src/__support/time/clock_conversion.h"
1516
#include "src/__support/time/linux/abs_timeout.h"
16-
#include "src/__support/time/linux/clock_conversion.h"
1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace internal {
1919
// This function is separated from abs_timeout.

0 commit comments

Comments
 (0)