Skip to content

Commit 097d459

Browse files
committed
[libc] Implement strftime and strftime_l
1 parent 8ea3b4c commit 097d459

22 files changed

+824
-332
lines changed

libc/src/stdio/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ add_entrypoint_object(
239239

240240
add_subdirectory(printf_core)
241241
add_subdirectory(scanf_core)
242-
add_subdirectory(strftime_core)
243242

244243
add_entrypoint_object(
245244
remove

libc/src/stdio/strftime_core/CMakeLists.txt

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

libc/src/stdio/strftime_core/core_structs.h

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

libc/src/time/CMakeLists.txt

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3-
endif()
1+
# if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
# endif()
44

55
add_object_library(
66
time_utils
@@ -103,28 +103,6 @@ add_entrypoint_object(
103103
libc.src.errno.errno
104104
)
105105

106-
107-
add_entrypoint_object(
108-
strftime
109-
SRCS
110-
strftime.cpp
111-
HDRS
112-
strftime.h
113-
DEPENDS
114-
libc.include.time
115-
)
116-
117-
add_entrypoint_object(
118-
strftime_l
119-
SRCS
120-
strftime_l.cpp
121-
HDRS
122-
strftime_l.h
123-
DEPENDS
124-
libc.include.time
125-
libc.include.locale
126-
)
127-
128106
add_entrypoint_object(
129107
time
130108
ALIAS
@@ -159,3 +137,30 @@ add_entrypoint_object(
159137
DEPENDS
160138
.${LIBC_TARGET_OS}.gettimeofday
161139
)
140+
141+
add_subdirectory(strftime_core)
142+
143+
add_entrypoint_object(
144+
strftime
145+
SRCS
146+
strftime.cpp
147+
HDRS
148+
strftime.h
149+
DEPENDS
150+
libc.include.time
151+
libc.src.time.strftime_core.strftime_main
152+
libc.src.stdio.printf_core.writer
153+
)
154+
155+
add_entrypoint_object(
156+
strftime_l
157+
SRCS
158+
strftime_l.cpp
159+
HDRS
160+
strftime_l.h
161+
DEPENDS
162+
libc.include.time
163+
libc.include.locale
164+
libc.src.time.strftime_core.strftime_main
165+
libc.src.stdio.printf_core.writer
166+
)

libc/src/time/strftime.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
#include "src/errno/libc_errno.h"
1313
#include "src/time/time_utils.h"
1414

15+
#include "src/stdio/printf_core/writer.h"
16+
#include "src/time/strftime_core/strftime_main.h"
1517
namespace LIBC_NAMESPACE_DECL {
1618

17-
using LIBC_NAMESPACE::time_utils::TimeConstants;
19+
size_t strftime(char *__restrict buffer, size_t buffsz,
20+
const char *__restrict format, const struct tm *timeptr) {
1821

19-
LLVM_LIBC_FUNCTION(size_t, strftime,
20-
(char *__restrict, size_t, const char *__restrict,
21-
const struct tm *)) {
22-
// TODO: Implement this for the default locale.
23-
return -1;
22+
printf_core::WriteBuffer wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
23+
printf_core::Writer writer(&wb);
24+
strftime_core::strftime_main(&writer, format, timeptr);
25+
return writer.get_chars_written();
2426
}
2527

2628
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
add_header_library(
2+
core_structs
3+
HDRS
4+
core_structs.h
5+
DEPENDS
6+
libc.src.__support.CPP.string_view
7+
libc.include.time
8+
)
9+
10+
add_header_library(
11+
parser
12+
HDRS
13+
parser.h
14+
DEPENDS
15+
.core_structs
16+
libc.src.string.string_utils
17+
libc.include.time
18+
19+
)
20+
21+
add_object_library(
22+
converter
23+
SRCS
24+
converter.cpp
25+
HDRS
26+
converter.h
27+
DEPENDS
28+
.core_structs
29+
libc.src.stdio.printf_core.writer
30+
libc.src.__support.big_int
31+
libc.src.__support.CPP.string_view
32+
libc.src.__support.float_to_string
33+
libc.src.__support.integer_to_string
34+
libc.src.__support.uint128
35+
libc.src.__support.StringUtil.error_to_string
36+
libc.include.time
37+
38+
)
39+
40+
add_object_library(
41+
strftime_main
42+
SRCS
43+
strftime_main.cpp
44+
HDRS
45+
strftime_main.h
46+
DEPENDS
47+
.core_structs
48+
.parser
49+
.converter
50+
libc.src.stdio.printf_core.writer
51+
libc.include.time
52+
)

0 commit comments

Comments
 (0)