Skip to content

Commit 269dd2c

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 269dd2c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

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 "src/__support/CPP/limits.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "time_utils.h"
14+
#include <cerrno>
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

libc/src/time/ctime_s.h

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

0 commit comments

Comments
 (0)