Skip to content

[libc][c11] implement ctime #107285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 63 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
b982621
[libc][c11] implement ctime (#86567)
zimirza Sep 3, 2024
bbbd2a9
[libc][c11] implement ctime
zimirza Sep 4, 2024
e146ac1
[libc][c11] implement ctime
zimirza Sep 4, 2024
eb02ef7
[libc][c11] implement ctime
zimirza Sep 4, 2024
12adb50
[libc][c11] implement ctime
zimirza Sep 4, 2024
b10a470
[libc][c11] implement ctime
zimirza Sep 4, 2024
bd482ba
[libc][ctime] ctime implementation
zimirza Sep 4, 2024
a1d86d2
[libc][c11] ctime implementation
zimirza Sep 4, 2024
de83e31
[libc][c11] implement ctime
zimirza Sep 4, 2024
b7d6c73
[libc][c11] implement ctime
zimirza Sep 4, 2024
948786a
[libc][c11] implement ctime
zimirza Sep 4, 2024
574583a
[libc][c11] implement ctime
zimirza Sep 5, 2024
7743993
[libc][c11] implement ctime
zimirza Sep 5, 2024
2db41fc
[libc][c11] implement ctime
zimirza Sep 5, 2024
1fd0113
[libc][c11] implement ctime
zimirza Sep 5, 2024
2f1a873
[libc][c11] implement ctime
zimirza Sep 5, 2024
cd65c21
[libc][c11] implement ctime
zimirza Sep 5, 2024
207e1e0
[libc][c11] implement ctime
zimirza Sep 5, 2024
925a6a6
[libc][c11] implement ctime
zimirza Sep 5, 2024
82ab554
[libc][c11] implement ctime
zimirza Sep 5, 2024
6fa3bc0
[libc][c11] implement ctime
zimirza Sep 5, 2024
a16d1a9
[libc][c11] implement ctime
zimirza Sep 5, 2024
f41a8ed
[libc][c11] implement ctime
zimirza Sep 5, 2024
cf3afc2
[libc][c11] implement ctime
zimirza Sep 5, 2024
2ff2fa1
[libc][c11] implement ctime
zimirza Sep 5, 2024
677775d
[libc][c11] implement ctime
zimirza Sep 5, 2024
f6b3037
[libc][c11] implement ctime
zimirza Sep 5, 2024
50b8710
[libc][c11] implement ctime
zimirza Sep 5, 2024
6225790
undo clang-tidy
zimirza Sep 5, 2024
cf870c0
add header file for `time.h`
zimirza Sep 5, 2024
eac511e
remove `time.h`
zimirza Sep 6, 2024
2f2a9a0
add function for `localtime` that temporarily use `gmtime`, since
zimirza Sep 6, 2024
602486b
added github issue for implementing `localtime`
zimirza Sep 6, 2024
eadea59
remove `struct tm` from `localtime`
zimirza Sep 6, 2024
325de68
use relative path
zimirza Sep 6, 2024
42c0508
add newline at end of files
zimirza Sep 6, 2024
2e51762
add comment for tests
zimirza Sep 6, 2024
9137851
add newline to end of files
zimirza Sep 6, 2024
485e7fb
return `nullptr` if `time_t` is greater than 32bit value
zimirza Sep 6, 2024
3a44b7a
return `nullptr` if `time_t` is greater than 32bit value
zimirza Sep 6, 2024
cd47d24
add 32bit maximum value to `TimeConstants` struct
zimirza Sep 6, 2024
a28d5cb
use `time_t.h` and include it in dependencies for `ctime` and `ctime_t`
zimirza Sep 6, 2024
3647f40
update cmake for tests
zimirza Sep 6, 2024
592e21c
remove symbols, since these will be implemented in
zimirza Sep 6, 2024
73e65a0
update cmake for tests
zimirza Sep 6, 2024
b9c6ceb
use `time_utils::localtime`
zimirza Sep 8, 2024
c130c1f
use `cpp::numeric_limits<int32_t>::max()`
zimirza Sep 8, 2024
4919279
pointer should be dereferenced
zimirza Sep 8, 2024
4cda79a
update comment for tests
zimirza Sep 8, 2024
8528172
remove helper functions and update comments
zimirza Sep 9, 2024
4543afa
`result` should be `static`
zimirza Sep 9, 2024
ac16aa6
remove comments
zimirza Sep 9, 2024
badde0d
removed `ctime` and `ctime_r` from clang fmt
zimirza Sep 11, 2024
30ba340
format code with clang-format
zimirza Sep 11, 2024
553c777
fix: function argument
zimirza Sep 12, 2024
1beea56
fix: pointers
zimirza Sep 12, 2024
34475ba
fix: header files
zimirza Sep 12, 2024
11821b0
fix: pointers
zimirza Sep 12, 2024
374d672
fix: pointers
zimirza Sep 12, 2024
2f4c296
fix: pointers for tests
zimirza Sep 12, 2024
02c780d
fix: pointers for tests
zimirza Sep 13, 2024
25ba008
fix: tests
zimirza Sep 13, 2024
77a0ba2
refactor `ctime` and `ctime_r`
zimirza Sep 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
libc.src.time.difftime
libc.src.time.gmtime
libc.src.time.gmtime_r
Expand Down
2 changes: 2 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
libc.src.time.difftime
libc.src.time.gmtime
libc.src.time.gmtime_r
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ if(LLVM_LIBC_FULL_BUILD)
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ if(LLVM_LIBC_FULL_BUILD)
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ if(LLVM_LIBC_FULL_BUILD)
# time.h entrypoints
libc.src.time.asctime
libc.src.time.asctime_r
libc.src.time.ctime
libc.src.time.ctime_r
libc.src.time.clock
libc.src.time.clock_gettime
libc.src.time.difftime
Expand Down
13 changes: 13 additions & 0 deletions libc/newhdrgen/yaml/time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ functions:
arguments:
- type: struct tm *
- type: char *
- name: ctime
standard:
- stdc
return_type: char *
arguments:
- type: const time_t *
- name: ctime_r
standard:
- stdc
return_type: char *
arguments:
- type: const time_t *
- type: char *
- name: clock
standard:
- stdc
Expand Down
13 changes: 13 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,19 @@ def StdC : StandardSpec<"stdc"> {
ArgSpec<CharPtr>,
]
>,
FunctionSpec<
"ctime",
RetValSpec<CharPtr>,
[ArgSpec<TimeTTypePtr>]
>,
FunctionSpec<
"ctime_r",
RetValSpec<CharPtr>,
[
ArgSpec<TimeTTypePtr>,
ArgSpec<CharPtr>,
]
>,
FunctionSpec<
"clock",
RetValSpec<ClockT>,
Expand Down
24 changes: 24 additions & 0 deletions libc/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ add_entrypoint_object(
libc.include.time
)

add_entrypoint_object(
ctime
SRCS
ctime.cpp
HDRS
ctime.h
DEPENDS
.time_utils
libc.hdr.types.time_t
libc.include.time
)

add_entrypoint_object(
ctime_r
SRCS
ctime_r.cpp
HDRS
ctime_r.h
DEPENDS
.time_utils
libc.hdr.types.time_t
libc.include.time
)

add_entrypoint_object(
difftime
SRCS
Expand Down
28 changes: 28 additions & 0 deletions libc/src/time/ctime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Implementation of ctime function ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ctime.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
return nullptr;
}
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
TimeConstants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/time/ctime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header of ctime --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_TIME_CTIME_H
#define LLVM_LIBC_SRC_TIME_CTIME_H

#include "hdr/types/time_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

char *ctime(const time_t *t_ptr);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_TIME_CTIME_H
29 changes: 29 additions & 0 deletions libc/src/time/ctime_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Implementation of ctime_r function --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ctime_r.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) {
if (t_ptr == nullptr || buffer == nullptr ||
*t_ptr > cpp::numeric_limits<int32_t>::max()) {
return nullptr;
}

return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
TimeConstants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/time/ctime_r.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header of ctime_r ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_TIME_CTIME_R_H
#define LLVM_LIBC_SRC_TIME_CTIME_R_H

#include "hdr/types/time_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

char *ctime_r(const time_t *t_ptr, char *buffer);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_TIME_CTIME_R_H
7 changes: 7 additions & 0 deletions libc/src/time/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ LIBC_INLINE struct tm *gmtime_internal(const time_t *timer, struct tm *result) {
return result;
}

// TODO: localtime is not yet implemented and a temporary solution is to
// use gmtime, https://github.com/llvm/llvm-project/issues/107597
LIBC_INLINE struct tm *localtime(const time_t *t_ptr) {
static struct tm result;
return time_utils::gmtime_internal(t_ptr, &result);
}

} // namespace time_utils
} // namespace LIBC_NAMESPACE_DECL

Expand Down
36 changes: 36 additions & 0 deletions libc/test/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ add_libc_unittest(
libc.src.time.asctime_r
)

add_libc_unittest(
ctime_test
SUITE
libc_time_unittests
SRCS
ctime_test.cpp
HDRS
TmHelper.h
TmMatcher.h
CXX_STANDARD
20
DEPENDS
libc.include.time
libc.hdr.types.time_t
libc.src.time.ctime
libc.src.time.time_utils
)

add_libc_unittest(
ctime_r_test
SUITE
libc_time_unittests
SRCS
ctime_r_test.cpp
HDRS
TmHelper.h
TmMatcher.h
CXX_STANDARD
20
DEPENDS
libc.include.time
libc.hdr.types.time_t
libc.src.time.ctime_r
libc.src.time.time_utils
)

add_libc_test(
clock_gettime_test
SUITE
Expand Down
58 changes: 58 additions & 0 deletions libc/test/src/time/ctime_r_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===-- Unittests for ctime_r ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/ctime_r.h"
#include "src/time/time_utils.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmHelper.h"

using LIBC_NAMESPACE::time_utils::TimeConstants;

TEST(LlvmLibcCtimeR, Nullptr) {
char *result;
result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr);
ASSERT_STREQ(nullptr, result);

char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
result = LIBC_NAMESPACE::ctime_r(nullptr, buffer);
ASSERT_STREQ(nullptr, result);

time_t t;
result = LIBC_NAMESPACE::ctime_r(&t, nullptr);
ASSERT_STREQ(nullptr, result);
}

TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) {
char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
time_t t;
char *result;
// 1970-01-01 00:00:00. Test with a valid buffer size.
t = 0;
result = LIBC_NAMESPACE::ctime_r(&t, buffer);
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
}

TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
time_t t;
char *result;
// 2038-01-19 03:14:07. Test with a valid buffer size.
t = 2147483647;
result = LIBC_NAMESPACE::ctime_r(&t, buffer);
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
}

TEST(LlvmLibcCtimeR, InvalidArgument) {
char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
time_t t;
char *result;
t = 2147483648;
result = LIBC_NAMESPACE::ctime_r(&t, buffer);
ASSERT_STREQ(nullptr, result);
}
42 changes: 42 additions & 0 deletions libc/test/src/time/ctime_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===-- Unittests for ctime -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/ctime.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmHelper.h"

TEST(LlvmLibcCtime, NULL) {
char *result;
result = LIBC_NAMESPACE::ctime(NULL);
ASSERT_STREQ(NULL, result);
}

TEST(LlvmLibcCtime, ValidUnixTimestamp0) {
time_t t;
char *result;
t = 0;
result = LIBC_NAMESPACE::ctime(&t);
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
}

TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
time_t t;
char *result;
t = 2147483647;
result = LIBC_NAMESPACE::ctime(&t);
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
}

TEST(LlvmLibcCtime, InvalidArgument) {
time_t t;
char *result;
t = 2147483648;
result = LIBC_NAMESPACE::ctime(&t);
ASSERT_STREQ(NULL, result);
}