Skip to content

Commit 6f8e17c

Browse files
authored
[libc] make clock_conversion.h common and document it (#167723)
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 65b2793 commit 6f8e17c

File tree

5 files changed

+75
-53
lines changed

5 files changed

+75
-53
lines changed

libc/src/__support/time/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ add_object_library(
2020
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
2121
)
2222

23+
add_header_library(
24+
clock_conversion
25+
HDRS
26+
clock_conversion.h
27+
DEPENDS
28+
.clock_gettime
29+
libc.src.__support.time.units
30+
)
31+
2332
if(TARGET libc.src.__support.time.${LIBC_TARGET_OS}.clock_settime)
2433
add_object_library(
2534
clock_settime
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

libc/src/__support/time/linux/CMakeLists.txt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ add_object_library(
2929
libc.src.__support.OSUtil.osutil
3030
)
3131

32-
add_header_library(
33-
clock_conversion
34-
HDRS
35-
clock_conversion.h
36-
DEPENDS
37-
.clock_gettime
38-
libc.src.__support.time.units
39-
)
4032

4133
add_header_library(
4234
abs_timeout
@@ -53,7 +45,7 @@ add_header_library(
5345
HDRS
5446
monotonicity.h
5547
DEPENDS
56-
.clock_conversion
5748
.abs_timeout
5849
libc.hdr.time_macros
50+
libc.src.__support.time.clock_conversion
5951
)

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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)