Skip to content

Commit 00637b7

Browse files
authored
[libc] Add strftime_l (#127767)
This is a (no-op) locale version of strftime. Fixes: #106630
1 parent 7e4fef6 commit 00637b7

File tree

9 files changed

+89
-1
lines changed

9 files changed

+89
-1
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ set(TARGET_LIBC_ENTRYPOINTS
248248
libc.src.time.gmtime
249249
libc.src.time.gmtime_r
250250
libc.src.time.mktime
251+
libc.src.time.strftime
252+
libc.src.time.strftime_l
251253
libc.src.time.timespec_get
252254

253255
# internal entrypoints

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ set(TARGET_LIBC_ENTRYPOINTS
248248
libc.src.time.gmtime
249249
libc.src.time.gmtime_r
250250
libc.src.time.mktime
251+
libc.src.time.strftime
252+
libc.src.time.strftime_l
251253
libc.src.time.timespec_get
252254

253255
# internal entrypoints

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ set(TARGET_LIBC_ENTRYPOINTS
244244
libc.src.time.gmtime
245245
libc.src.time.gmtime_r
246246
libc.src.time.mktime
247+
libc.src.time.strftime
248+
libc.src.time.strftime_l
247249
libc.src.time.timespec_get
248250

249251
# internal entrypoints

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ if(LLVM_LIBC_FULL_BUILD)
11291129
libc.src.time.mktime
11301130
libc.src.time.nanosleep
11311131
libc.src.time.strftime
1132+
libc.src.time.strftime_l
11321133
libc.src.time.time
11331134
libc.src.time.timespec_get
11341135

libc/include/time.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ types:
99
- type_name: time_t
1010
- type_name: clock_t
1111
- type_name: size_t
12+
- type_name: locale_t
1213
enums: []
1314
objects: []
1415
functions:
@@ -100,6 +101,16 @@ functions:
100101
- type: size_t
101102
- type: const char *__restrict
102103
- type: const struct tm *__restrict
104+
- name: strftime_l
105+
standard:
106+
- stdc
107+
return_type: size_t
108+
arguments:
109+
- type: char *__restrict
110+
- type: size_t
111+
- type: const char *__restrict
112+
- type: const struct tm *__restrict
113+
- type: locale_t
103114
- name: time
104115
standard:
105116
- stdc

libc/src/time/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ add_entrypoint_object(
150150
libc.src.time.strftime_core.strftime_main
151151
)
152152

153+
add_entrypoint_object(
154+
strftime_l
155+
SRCS
156+
strftime_l.cpp
157+
HDRS
158+
strftime_l.h
159+
DEPENDS
160+
libc.hdr.types.locale_t
161+
libc.hdr.types.size_t
162+
libc.hdr.types.struct_tm
163+
libc.src.stdio.printf_core.writer
164+
libc.src.time.strftime_core.strftime_main
165+
)
166+
153167
add_entrypoint_object(
154168
time
155169
SRCS

libc/src/time/strftime.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace LIBC_NAMESPACE_DECL {
1919
LLVM_LIBC_FUNCTION(size_t, strftime,
2020
(char *__restrict buffer, size_t buffsz,
2121
const char *__restrict format, const tm *timeptr)) {
22-
2322
printf_core::WriteBuffer wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
2423
printf_core::Writer writer(&wb);
2524
int ret = strftime_core::strftime_main(&writer, format, timeptr);

libc/src/time/strftime_l.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Implementation of strftime_l 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 "src/time/strftime_l.h"
10+
#include "hdr/types/locale_t.h"
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/struct_tm.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/macros/config.h"
15+
#include "src/stdio/printf_core/writer.h"
16+
#include "src/time/strftime_core/strftime_main.h"
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
// TODO: Add support for locales.
21+
LLVM_LIBC_FUNCTION(size_t, strftime_l,
22+
(char *__restrict buffer, size_t buffsz,
23+
const char *__restrict format, const tm *timeptr,
24+
locale_t)) {
25+
printf_core::WriteBuffer wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
26+
printf_core::Writer writer(&wb);
27+
int ret = strftime_core::strftime_main(&writer, format, timeptr);
28+
if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
29+
wb.buff[wb.buff_cur] = '\0';
30+
return (ret < 0 || static_cast<size_t>(ret) > buffsz) ? 0 : ret;
31+
}
32+
33+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/strftime_l.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for strftime_l --------------------*- 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_STRFTIME_L_H
10+
#define LLVM_LIBC_SRC_TIME_STRFTIME_L_H
11+
12+
#include "hdr/types/locale_t.h"
13+
#include "hdr/types/size_t.h"
14+
#include "hdr/types/struct_tm.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
size_t strftime_l(char *__restrict buffer, size_t count,
20+
const char *__restrict format, const tm *timeptr, locale_t);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_TIME_STRFTIME_L_H

0 commit comments

Comments
 (0)