Skip to content

Commit bef8896

Browse files
committed
[libc] Add ctime_s
Resolves #110548 Definition: ```c++ int ctime_s(char *buffer, size_t buffer_size, const time_t *t_ptr); ```
1 parent 79ecb81 commit bef8896

File tree

13 files changed

+175
-0
lines changed

13 files changed

+175
-0
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ set(TARGET_LIBC_ENTRYPOINTS
205205
libc.src.time.asctime_r
206206
libc.src.time.ctime
207207
libc.src.time.ctime_r
208+
libc.src.time.ctime_s
208209
libc.src.time.difftime
209210
libc.src.time.gmtime
210211
libc.src.time.gmtime_r

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ set(TARGET_LIBC_ENTRYPOINTS
201201
libc.src.time.asctime_r
202202
libc.src.time.ctime
203203
libc.src.time.ctime_r
204+
libc.src.time.ctime_s
204205
libc.src.time.difftime
205206
libc.src.time.gmtime
206207
libc.src.time.gmtime_r

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ if(LLVM_LIBC_FULL_BUILD)
950950
libc.src.time.asctime_r
951951
libc.src.time.ctime
952952
libc.src.time.ctime_r
953+
libc.src.time.ctime_s
953954
libc.src.time.clock
954955
libc.src.time.clock_gettime
955956
libc.src.time.difftime

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ if(LLVM_LIBC_FULL_BUILD)
885885
libc.src.time.asctime_r
886886
libc.src.time.ctime
887887
libc.src.time.ctime_r
888+
libc.src.time.ctime_s
888889
libc.src.time.clock
889890
libc.src.time.clock_gettime
890891
libc.src.time.difftime

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ if(LLVM_LIBC_FULL_BUILD)
10051005
libc.src.time.asctime_r
10061006
libc.src.time.ctime
10071007
libc.src.time.ctime_r
1008+
libc.src.time.ctime_s
10081009
libc.src.time.clock
10091010
libc.src.time.clock_gettime
10101011
libc.src.time.difftime

libc/docs/date_and_time.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Implementation Status
5959
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
6060
| ctime_r | |check| | |check| | | |check| | | | | | | | | |
6161
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
62+
| ctime_s | |check| | |check| | | |check| | | | | | | | | |
63+
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
6264
| clock | |check| | |check| | | |check| | | | | | | | | |
6365
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
6466
| clock_getcpuclockid | | | | | | | | | | | | |

libc/newhdrgen/yaml/time.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ functions:
3737
arguments:
3838
- type: const time_t *
3939
- type: char *
40+
- name: ctime_s
41+
standard:
42+
- stdc
43+
return_type: int
44+
arguments:
45+
- type: char *
46+
- type: size_t
47+
- type: const time_t *
4048
- name: clock
4149
standard:
4250
- stdc

libc/spec/stdc.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,15 @@ def StdC : StandardSpec<"stdc"> {
16151615
ArgSpec<CharPtr>,
16161616
]
16171617
>,
1618+
FunctionSpec<
1619+
"ctime_s",
1620+
RetValSpec<IntType>,
1621+
[
1622+
ArgSpec<CharPtr>,
1623+
ArgSpec<SizeTType>,
1624+
ArgSpec<TimeTTypePtr>,
1625+
]
1626+
>,
16181627
FunctionSpec<
16191628
"clock",
16201629
RetValSpec<ClockT>,

libc/src/time/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ add_entrypoint_object(
6060
libc.include.time
6161
)
6262

63+
add_entrypoint_object(
64+
ctime_s
65+
SRCS
66+
ctime_s.cpp
67+
HDRS
68+
ctime_s.h
69+
DEPENDS
70+
.time_utils
71+
libc.hdr.types.time_t
72+
libc.include.time
73+
)
74+
6375
add_entrypoint_object(
6476
difftime
6577
SRCS

libc/src/time/ctime_s.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Implementation of ctime_s function --------------------------------===//
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+
#include "ctime_s.h"
10+
#include "hdr/errno_macros.h"
11+
#include "src/__support/CPP/limits.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "time_utils.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
using LIBC_NAMESPACE::time_utils::TimeConstants;
19+
20+
LLVM_LIBC_FUNCTION(int, ctime_s,
21+
(char *buffer, size_t buffer_size, const time_t *t_ptr)) {
22+
if (t_ptr == nullptr || buffer == nullptr ||
23+
*time > cpp::numeric_limits<int32_t>::max()) {
24+
return EINVAL;
25+
}
26+
27+
if (buffer_size < TimeConstants::ASCTIME_MAX_BYTES) {
28+
return ERANGE;
29+
}
30+
31+
if (time_utils::asctime(time_utils::localtime(t_ptr), buffer, buffer_size) ==
32+
nullptr) {
33+
return EINVAL;
34+
}
35+
36+
return 0;
37+
}
38+
39+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)