Skip to content

Commit 000a3f0

Browse files
authored
[libc][c11] implement ctime (#107285)
This is an implementation of `ctime` and includes `ctime_r`. According to documentation, `ctime` and `ctime_r` are defined as the following: ```c char *ctime(const time_t *timep); char *ctime_r(const time_t *restrict timep, char buf[restrict 26]); ``` closes #86567
1 parent 0ea0e3a commit 000a3f0

File tree

16 files changed

+302
-0
lines changed

16 files changed

+302
-0
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ set(TARGET_LIBC_ENTRYPOINTS
203203
# time.h entrypoints
204204
libc.src.time.asctime
205205
libc.src.time.asctime_r
206+
libc.src.time.ctime
207+
libc.src.time.ctime_r
206208
libc.src.time.difftime
207209
libc.src.time.gmtime
208210
libc.src.time.gmtime_r

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ set(TARGET_LIBC_ENTRYPOINTS
199199
# time.h entrypoints
200200
libc.src.time.asctime
201201
libc.src.time.asctime_r
202+
libc.src.time.ctime
203+
libc.src.time.ctime_r
202204
libc.src.time.difftime
203205
libc.src.time.gmtime
204206
libc.src.time.gmtime_r

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ if(LLVM_LIBC_FULL_BUILD)
948948
# time.h entrypoints
949949
libc.src.time.asctime
950950
libc.src.time.asctime_r
951+
libc.src.time.ctime
952+
libc.src.time.ctime_r
951953
libc.src.time.clock
952954
libc.src.time.clock_gettime
953955
libc.src.time.difftime

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ if(LLVM_LIBC_FULL_BUILD)
883883
# time.h entrypoints
884884
libc.src.time.asctime
885885
libc.src.time.asctime_r
886+
libc.src.time.ctime
887+
libc.src.time.ctime_r
886888
libc.src.time.clock
887889
libc.src.time.clock_gettime
888890
libc.src.time.difftime

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ if(LLVM_LIBC_FULL_BUILD)
10031003
# time.h entrypoints
10041004
libc.src.time.asctime
10051005
libc.src.time.asctime_r
1006+
libc.src.time.ctime
1007+
libc.src.time.ctime_r
10061008
libc.src.time.clock
10071009
libc.src.time.clock_gettime
10081010
libc.src.time.difftime

libc/newhdrgen/yaml/time.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ functions:
2424
arguments:
2525
- type: struct tm *
2626
- type: char *
27+
- name: ctime
28+
standard:
29+
- stdc
30+
return_type: char *
31+
arguments:
32+
- type: const time_t *
33+
- name: ctime_r
34+
standard:
35+
- stdc
36+
return_type: char *
37+
arguments:
38+
- type: const time_t *
39+
- type: char *
2740
- name: clock
2841
standard:
2942
- stdc

libc/spec/stdc.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,19 @@ def StdC : StandardSpec<"stdc"> {
16001600
ArgSpec<CharPtr>,
16011601
]
16021602
>,
1603+
FunctionSpec<
1604+
"ctime",
1605+
RetValSpec<CharPtr>,
1606+
[ArgSpec<TimeTTypePtr>]
1607+
>,
1608+
FunctionSpec<
1609+
"ctime_r",
1610+
RetValSpec<CharPtr>,
1611+
[
1612+
ArgSpec<TimeTTypePtr>,
1613+
ArgSpec<CharPtr>,
1614+
]
1615+
>,
16031616
FunctionSpec<
16041617
"clock",
16051618
RetValSpec<ClockT>,

libc/src/time/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ add_entrypoint_object(
3636
libc.include.time
3737
)
3838

39+
add_entrypoint_object(
40+
ctime
41+
SRCS
42+
ctime.cpp
43+
HDRS
44+
ctime.h
45+
DEPENDS
46+
.time_utils
47+
libc.hdr.types.time_t
48+
libc.include.time
49+
)
50+
51+
add_entrypoint_object(
52+
ctime_r
53+
SRCS
54+
ctime_r.cpp
55+
HDRS
56+
ctime_r.h
57+
DEPENDS
58+
.time_utils
59+
libc.hdr.types.time_t
60+
libc.include.time
61+
)
62+
3963
add_entrypoint_object(
4064
difftime
4165
SRCS

libc/src/time/ctime.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of ctime 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.h"
10+
#include "src/__support/CPP/limits.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "time_utils.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
using LIBC_NAMESPACE::time_utils::TimeConstants;
18+
19+
LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
20+
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
21+
return nullptr;
22+
}
23+
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
24+
return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
25+
TimeConstants::ASCTIME_MAX_BYTES);
26+
}
27+
28+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/ctime.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header of ctime --------------------------*- 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_TIME_CTIME_H
10+
#define LLVM_LIBC_SRC_TIME_CTIME_H
11+
12+
#include "hdr/types/time_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
char *ctime(const time_t *t_ptr);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_TIME_CTIME_H

0 commit comments

Comments
 (0)