[libc-commits] [libc] [libc] Add `ctime_s` (PR #110676)
Rajveer Singh Bharadwaj via libc-commits
libc-commits at lists.llvm.org
Wed Oct 2 11:31:43 PDT 2024
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/110676
>From c7692cce223464963218ea59a6eb2a6de852aff9 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Tue, 1 Oct 2024 19:09:11 +0530
Subject: [PATCH] [libc] Add `ctime_s`
Resolves #110548
Definition:
```c++
int ctime_s(char *buffer, size_t buffer_size, const time_t *t_ptr);
```
---
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/date_and_time.rst | 2 +
libc/newhdrgen/yaml/time.yaml | 8 +++
libc/spec/stdc.td | 9 +++
libc/src/time/CMakeLists.txt | 12 ++++
libc/src/time/ctime_s.cpp | 39 +++++++++++++
libc/src/time/ctime_s.h | 21 +++++++
libc/test/src/time/CMakeLists.txt | 18 ++++++
libc/test/src/time/ctime_s_test.cpp | 61 +++++++++++++++++++++
13 files changed, 175 insertions(+)
create mode 100644 libc/src/time/ctime_s.cpp
create mode 100644 libc/src/time/ctime_s.h
create mode 100644 libc/test/src/time/ctime_s_test.cpp
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 68030f7f1775b5..233af0e2915f73 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -205,6 +205,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
+ libc.src.time.ctime_s
libc.src.time.difftime
libc.src.time.gmtime
libc.src.time.gmtime_r
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 5894b591072ef0..1c6f4aa04ca3f0 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -201,6 +201,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
+ libc.src.time.ctime_s
libc.src.time.difftime
libc.src.time.gmtime
libc.src.time.gmtime_r
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 64fbe1a250c0ba..cd46edcdde52d7 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -950,6 +950,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
+ libc.src.time.ctime_s
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ff3d821c664c5b..e46f534d230c64 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -885,6 +885,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
+ libc.src.time.ctime_s
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index dd658af3bfb674..b9b14c570bc074 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1005,6 +1005,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
+ libc.src.time.ctime_s
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
diff --git a/libc/docs/date_and_time.rst b/libc/docs/date_and_time.rst
index b745a3b416f802..e8a78ff9fbdb1d 100644
--- a/libc/docs/date_and_time.rst
+++ b/libc/docs/date_and_time.rst
@@ -59,6 +59,8 @@ Implementation Status
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
| ctime_r | |check| | |check| | | |check| | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
+| ctime_s | |check| | |check| | | |check| | | | | | | | | |
++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
| clock | |check| | |check| | | |check| | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
| clock_getcpuclockid | | | | | | | | | | | | |
diff --git a/libc/newhdrgen/yaml/time.yaml b/libc/newhdrgen/yaml/time.yaml
index 69b40bef3160dd..ac0ccdde5f4cf6 100644
--- a/libc/newhdrgen/yaml/time.yaml
+++ b/libc/newhdrgen/yaml/time.yaml
@@ -37,6 +37,14 @@ functions:
arguments:
- type: const time_t *
- type: char *
+ - name: ctime_s
+ standard:
+ - stdc
+ return_type: int
+ arguments:
+ - type: char *
+ - type: size_t
+ - type: const time_t *
- name: clock
standard:
- stdc
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 7caf543748151a..79faed068abb3a 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1615,6 +1615,15 @@ def StdC : StandardSpec<"stdc"> {
ArgSpec<CharPtr>,
]
>,
+ FunctionSpec<
+ "ctime_s",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<CharPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<ConstTimeTTypePtr>,
+ ]
+ >,
FunctionSpec<
"clock",
RetValSpec<ClockT>,
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index b3318e7ca87fa5..c2fc04ace137f5 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -60,6 +60,18 @@ add_entrypoint_object(
libc.include.time
)
+add_entrypoint_object(
+ ctime_s
+ SRCS
+ ctime_s.cpp
+ HDRS
+ ctime_s.h
+ DEPENDS
+ .time_utils
+ libc.hdr.types.time_t
+ libc.include.time
+)
+
add_entrypoint_object(
difftime
SRCS
diff --git a/libc/src/time/ctime_s.cpp b/libc/src/time/ctime_s.cpp
new file mode 100644
index 00000000000000..abd8c266f20b66
--- /dev/null
+++ b/libc/src/time/ctime_s.cpp
@@ -0,0 +1,39 @@
+//===-- Implementation of ctime_s function --------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "ctime_s.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+LLVM_LIBC_FUNCTION(int, ctime_s,
+ (char *buffer, size_t buffer_size, const time_t *t_ptr)) {
+ if (t_ptr == nullptr || buffer == nullptr ||
+ *time > cpp::numeric_limits<int32_t>::max()) {
+ return EINVAL;
+ }
+
+ if (buffer_size < TimeConstants::ASCTIME_MAX_BYTES) {
+ return ERANGE;
+ }
+
+ if (time_utils::asctime(time_utils::localtime(t_ptr), buffer, buffer_size) ==
+ nullptr) {
+ return EINVAL;
+ }
+
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/ctime_s.h b/libc/src/time/ctime_s.h
new file mode 100644
index 00000000000000..023c4b5b3cbc7f
--- /dev/null
+++ b/libc/src/time/ctime_s.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of ctime_s ------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_CTIME_S_H
+#define LLVM_LIBC_SRC_TIME_CTIME_S_H
+
+#include "hdr/types/time_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int ctime_s(char *buffer, size_t buffer_size, const time_t *t_ptr);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_CTIME_S_H
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index bba01f063fed27..7c27be0f23200a 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -66,6 +66,24 @@ add_libc_unittest(
libc.src.time.time_utils
)
+add_libc_unittest(
+ ctime_s_test
+ SUITE
+ libc_time_unittests
+ SRCS
+ ctime_s_test.cpp
+ HDRS
+ TmHelper.h
+ TmMatcher.h
+ CXX_STANDARD
+ 20
+ DEPENDS
+ libc.include.time
+ libc.hdr.types.time_t
+ libc.src.time.ctime_s
+ libc.src.time.time_utils
+)
+
add_libc_test(
clock_gettime_test
SUITE
diff --git a/libc/test/src/time/ctime_s_test.cpp b/libc/test/src/time/ctime_s_test.cpp
new file mode 100644
index 00000000000000..cf1f8b26e2b936
--- /dev/null
+++ b/libc/test/src/time/ctime_s_test.cpp
@@ -0,0 +1,61 @@
+//===-- Unittests for ctime_s ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/errno/libc_errno.h"
+#include "src/time/ctime_s.h"
+#include "src/time/time_utils.h"
+#include "test/UnitTest/Test.h"
+#include "test/src/time/TmHelper.h"
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+TEST(LlvmLibcCtimeS, Nullptr) {
+ int result;
+ result = LIBC_NAMESPACE::ctime_s(nullptr, 0, nullptr);
+ ASSERT_EQ(EINVAL, result);
+
+ char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+ result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), nullptr);
+ ASSERT_EQ(EINVAL, result);
+
+ time_t t;
+ result = LIBC_NAMESPACE::ctime_s(nullptr, 0, &t);
+ ASSERT_EQ(EINVAL, result);
+}
+
+TEST(LlvmLibcCtimeS, ValidUnixTimestamp0) {
+ char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+ time_t t;
+ int result;
+ // 1970-01-01 00:00:00. Test with a valid buffer size.
+ t = 0;
+ result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+ ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", buffer);
+ ASSERT_EQ(0, result);
+}
+
+TEST(LlvmLibcCtimeS, ValidUnixTimestamp32Int) {
+ char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+ time_t t;
+ int result;
+ // 2038-01-19 03:14:07. Test with a valid buffer size.
+ t = 2147483647;
+ result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+ ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", buffer);
+ ASSERT_EQ(0, result);
+}
+
+TEST(LlvmLibcCtimeS, InvalidArgument) {
+ char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+ time_t t;
+ int result;
+ t = 2147483648;
+ result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+ ASSERT_EQ(EINVAL, result);
+}
More information about the libc-commits
mailing list