[libc-commits] [libc] [libc] Implement C23 timespec_getres in <time.h> (PR #242528)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Mon Nov 16 13:47:58 PST 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/242528
>From 3f23d8e53aa73bb650d6641dc715acac3cfa2319 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 14 Nov 2026 05:18:22 +0530
Subject: [PATCH 1/2] [libc] Implement C23 timespec_getres in <time.h>
C23 (ISO/IEC 9899:2024) section 7.29.2.6 standardizes timespec_getres
to query the resolution of supported time bases (such as TIME_UTC).
Implement timespec_getres for Linux targets using clock_getres,
register the entrypoint across supported architectures, and add unittests.
Assisted-by: Antigravity (Gemini 3.8 Flash)
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/time.yaml | 7 +++
libc/src/time/CMakeLists.txt | 7 +++
libc/src/time/linux/CMakeLists.txt | 13 ++++++
libc/src/time/linux/timespec_getres.cpp | 51 +++++++++++++++++++++
libc/src/time/timespec_getres.h | 27 +++++++++++
libc/test/src/time/CMakeLists.txt | 12 +++++
libc/test/src/time/timespec_getres_test.cpp | 48 +++++++++++++++++++
10 files changed, 168 insertions(+)
create mode 100644 libc/src/time/linux/timespec_getres.cpp
create mode 100644 libc/src/time/timespec_getres.h
create mode 100644 libc/test/src/time/timespec_getres_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index aed6852ff9b06d..881ff2bc7f1bbc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1504,6 +1504,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.nanosleep
libc.src.time.time
libc.src.time.timespec_get
+ libc.src.time.timespec_getres
libc.src.time.timer_create
libc.src.time.timer_delete
libc.src.time.timer_getoverrun
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index dda4f01e369787..2dc0ec43640470 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1726,6 +1726,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.strftime_l
libc.src.time.time
libc.src.time.timespec_get
+ libc.src.time.timespec_getres
libc.src.time.timer_create
libc.src.time.timer_delete
libc.src.time.timer_getoverrun
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index eb5dcd44c8fb72..3a34ff6433ca28 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1730,6 +1730,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.strftime_l
libc.src.time.time
libc.src.time.timespec_get
+ libc.src.time.timespec_getres
libc.src.time.timer_create
libc.src.time.timer_delete
libc.src.time.timer_getoverrun
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index adacfab194f822..6ff76311363ea1 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -180,6 +180,13 @@ functions:
arguments:
- type: struct timespec *
- type: int
+ - name: timespec_getres
+ standards:
+ - stdc
+ return_type: int
+ arguments:
+ - type: struct timespec *
+ - type: int
- name: timer_create
standards:
- posix
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 10415731078965..89d3cee0a25763 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -265,6 +265,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.timespec_get
)
+add_entrypoint_object(
+ timespec_getres
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.timespec_getres
+)
+
add_entrypoint_object(
clock
ALIAS
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index 74891dce27445a..e274a614a57b28 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -17,6 +17,19 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ timespec_getres
+ SRCS
+ timespec_getres.cpp
+ HDRS
+ ../timespec_getres.h
+ DEPENDS
+ libc.hdr.time_macros
+ libc.hdr.types.struct_timespec
+ libc.src.__support.OSUtil.linux.syscall_wrappers.clock_getres
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
clock
SRCS
diff --git a/libc/src/time/linux/timespec_getres.cpp b/libc/src/time/linux/timespec_getres.cpp
new file mode 100644
index 00000000000000..b214cd00cbf768
--- /dev/null
+++ b/libc/src/time/linux/timespec_getres.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of timespec_getres.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/timespec_getres.h"
+#include "hdr/time_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/clock_getres.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, timespec_getres, (timespec * ts, int base)) {
+ clockid_t clockid;
+ switch (base) {
+ case TIME_UTC:
+ clockid = CLOCK_REALTIME;
+ break;
+ case TIME_MONOTONIC:
+ clockid = CLOCK_MONOTONIC;
+ break;
+ case TIME_ACTIVE:
+ clockid = CLOCK_PROCESS_CPUTIME_ID;
+ break;
+ case TIME_THREAD_ACTIVE:
+ clockid = CLOCK_THREAD_CPUTIME_ID;
+ break;
+ default:
+ return 0;
+ }
+
+ auto result = linux_syscalls::clock_getres(clockid, ts);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return 0;
+ }
+ return base;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
diff --git a/libc/src/time/timespec_getres.h b/libc/src/time/timespec_getres.h
new file mode 100644
index 00000000000000..bee8a531528042
--- /dev/null
+++ b/libc/src/time/timespec_getres.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of timespec_getres.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_TIMESPEC_GETRES_H
+#define LLVM_LIBC_SRC_TIME_TIMESPEC_GETRES_H
+
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int timespec_getres(struct timespec *ts, int base);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_TIMESPEC_GETRES_H
+
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index f2d71dba95516e..1784380c5919e2 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -339,6 +339,18 @@ add_libc_test(
libc.hdr.types.struct_timespec
)
+add_libc_test(
+ timespec_getres_test
+ SUITE
+ libc-time-tests
+ SRCS
+ timespec_getres_test.cpp
+ DEPENDS
+ libc.src.time.timespec_getres
+ libc.hdr.types.struct_timespec
+ libc.hdr.time_macros
+)
+
add_libc_test(
clock_test
SUITE
diff --git a/libc/test/src/time/timespec_getres_test.cpp b/libc/test/src/time/timespec_getres_test.cpp
new file mode 100644
index 00000000000000..6a765ec4631c43
--- /dev/null
+++ b/libc/test/src/time/timespec_getres_test.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for timespec_getres.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/time_macros.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/time/timespec_getres.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcTimespecGetRes, Utc) {
+ timespec ts;
+ int result = LIBC_NAMESPACE::timespec_getres(&ts, TIME_UTC);
+ ASSERT_EQ(result, TIME_UTC);
+ ASSERT_GE(ts.tv_sec, time_t(0));
+ ASSERT_GE(ts.tv_nsec, 0L);
+}
+
+TEST(LlvmLibcTimespecGetRes, Monotonic) {
+ timespec ts;
+ int result = LIBC_NAMESPACE::timespec_getres(&ts, TIME_MONOTONIC);
+ ASSERT_EQ(result, TIME_MONOTONIC);
+ ASSERT_GE(ts.tv_sec, time_t(0));
+ ASSERT_GE(ts.tv_nsec, 0L);
+}
+
+TEST(LlvmLibcTimespecGetRes, NullSpec) {
+ int result = LIBC_NAMESPACE::timespec_getres(nullptr, TIME_UTC);
+ ASSERT_EQ(result, TIME_UTC);
+}
+
+TEST(LlvmLibcTimespecGetRes, InvalidBase) {
+ timespec ts;
+ int result = LIBC_NAMESPACE::timespec_getres(&ts, -1);
+ ASSERT_EQ(result, 0);
+
+ result = LIBC_NAMESPACE::timespec_getres(&ts, 0);
+ ASSERT_EQ(result, 0);
+}
+
>From 2b9f39a5fb82a91630febca55af5097677f1baec Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Tue, 17 Nov 2026 03:17:06 +0530
Subject: [PATCH 2/2] [libc] Fix formatting in timespec_getres
Assisted-by: Antigravity (Gemini 3.8 Flash)
---
libc/src/time/linux/timespec_getres.cpp | 1 -
libc/src/time/timespec_getres.h | 1 -
libc/test/src/time/timespec_getres_test.cpp | 1 -
3 files changed, 3 deletions(-)
diff --git a/libc/src/time/linux/timespec_getres.cpp b/libc/src/time/linux/timespec_getres.cpp
index b214cd00cbf768..ba269e9a346562 100644
--- a/libc/src/time/linux/timespec_getres.cpp
+++ b/libc/src/time/linux/timespec_getres.cpp
@@ -48,4 +48,3 @@ LLVM_LIBC_FUNCTION(int, timespec_getres, (timespec * ts, int base)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
diff --git a/libc/src/time/timespec_getres.h b/libc/src/time/timespec_getres.h
index bee8a531528042..eb9bdac4fbdf73 100644
--- a/libc/src/time/timespec_getres.h
+++ b/libc/src/time/timespec_getres.h
@@ -24,4 +24,3 @@ int timespec_getres(struct timespec *ts, int base);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_TIME_TIMESPEC_GETRES_H
-
diff --git a/libc/test/src/time/timespec_getres_test.cpp b/libc/test/src/time/timespec_getres_test.cpp
index 6a765ec4631c43..66d39bb4d3e1a3 100644
--- a/libc/test/src/time/timespec_getres_test.cpp
+++ b/libc/test/src/time/timespec_getres_test.cpp
@@ -45,4 +45,3 @@ TEST(LlvmLibcTimespecGetRes, InvalidBase) {
result = LIBC_NAMESPACE::timespec_getres(&ts, 0);
ASSERT_EQ(result, 0);
}
-
More information about the libc-commits
mailing list