-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
[libc][c11] implement ctime #107285
Changes from 22 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
b982621
[libc][c11] implement ctime (#86567)
zimirza bbbd2a9
[libc][c11] implement ctime
zimirza e146ac1
[libc][c11] implement ctime
zimirza eb02ef7
[libc][c11] implement ctime
zimirza 12adb50
[libc][c11] implement ctime
zimirza b10a470
[libc][c11] implement ctime
zimirza bd482ba
[libc][ctime] ctime implementation
zimirza a1d86d2
[libc][c11] ctime implementation
zimirza de83e31
[libc][c11] implement ctime
zimirza b7d6c73
[libc][c11] implement ctime
zimirza 948786a
[libc][c11] implement ctime
zimirza 574583a
[libc][c11] implement ctime
zimirza 7743993
[libc][c11] implement ctime
zimirza 2db41fc
[libc][c11] implement ctime
zimirza 1fd0113
[libc][c11] implement ctime
zimirza 2f1a873
[libc][c11] implement ctime
zimirza cd65c21
[libc][c11] implement ctime
zimirza 207e1e0
[libc][c11] implement ctime
zimirza 925a6a6
[libc][c11] implement ctime
zimirza 82ab554
[libc][c11] implement ctime
zimirza 6fa3bc0
[libc][c11] implement ctime
zimirza a16d1a9
[libc][c11] implement ctime
zimirza f41a8ed
[libc][c11] implement ctime
zimirza cf3afc2
[libc][c11] implement ctime
zimirza 2ff2fa1
[libc][c11] implement ctime
zimirza 677775d
[libc][c11] implement ctime
zimirza f6b3037
[libc][c11] implement ctime
zimirza 50b8710
[libc][c11] implement ctime
zimirza 6225790
undo clang-tidy
zimirza cf870c0
add header file for `time.h`
zimirza eac511e
remove `time.h`
zimirza 2f2a9a0
add function for `localtime` that temporarily use `gmtime`, since
zimirza 602486b
added github issue for implementing `localtime`
zimirza eadea59
remove `struct tm` from `localtime`
zimirza 325de68
use relative path
zimirza 42c0508
add newline at end of files
zimirza 2e51762
add comment for tests
zimirza 9137851
add newline to end of files
zimirza 485e7fb
return `nullptr` if `time_t` is greater than 32bit value
zimirza 3a44b7a
return `nullptr` if `time_t` is greater than 32bit value
zimirza cd47d24
add 32bit maximum value to `TimeConstants` struct
zimirza a28d5cb
use `time_t.h` and include it in dependencies for `ctime` and `ctime_t`
zimirza 3647f40
update cmake for tests
zimirza 592e21c
remove symbols, since these will be implemented in
zimirza 73e65a0
update cmake for tests
zimirza b9c6ceb
use `time_utils::localtime`
zimirza c130c1f
use `cpp::numeric_limits<int32_t>::max()`
zimirza 4919279
pointer should be dereferenced
zimirza 4cda79a
update comment for tests
zimirza 8528172
remove helper functions and update comments
zimirza 4543afa
`result` should be `static`
zimirza ac16aa6
remove comments
zimirza badde0d
removed `ctime` and `ctime_r` from clang fmt
zimirza 30ba340
format code with clang-format
zimirza 553c777
fix: function argument
zimirza 1beea56
fix: pointers
zimirza 34475ba
fix: header files
zimirza 11821b0
fix: pointers
zimirza 374d672
fix: pointers
zimirza 2f4c296
fix: pointers for tests
zimirza 02c780d
fix: pointers for tests
zimirza 25ba008
fix: tests
zimirza 77a0ba2
refactor `ctime` and `ctime_r`
zimirza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===-- 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 "src/time/ctime.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/time/time_utils.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
using LIBC_NAMESPACE::time_utils::TimeConstants; | ||
|
||
LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) { | ||
static char buffer[TimeConstants::CTIME_BUFFER_SIZE]; | ||
return time_utils::asctime(localtime(t_ptr), buffer, TimeConstants::CTIME_MAX_BYTES); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "src/__support/macros/config.h" | ||
zimirza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <time.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
char *ctime(const time_t *t_ptr); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_TIME_CTIME_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- 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 "src/time/ctime_r.h" | ||
zimirza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/time/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)) { | ||
return time_utils::asctime(localtime(t_ptr), buffer, TimeConstants::CTIME_MAX_BYTES); | ||
zimirza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "src/__support/macros/config.h" | ||
#include <time.h> | ||
zimirza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
char *ctime_r(const time_t *t, char *buffer); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_TIME_CTIME_R_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//===-- 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; | ||
|
||
static inline char *call_ctime_r(time_t *t, int year, int month, int mday, | ||
int hour, int min, int sec, int wday, int yday, | ||
char *buffer) { | ||
LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data( | ||
localtime(t), year, month, mday, hour, min, sec, wday, yday); | ||
return LIBC_NAMESPACE::ctime_r(t, buffer); | ||
} | ||
|
||
// ctime and ctime_r share the same code and thus didn't repeat all the | ||
// tests from ctime. Added couple of validation tests. | ||
TEST(LlvmLibcCtimeR, Nullptr) { | ||
char *result; | ||
result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr); | ||
ASSERT_ERRNO_EQ(EINVAL); | ||
ASSERT_STREQ(nullptr, result); | ||
|
||
char buffer[TimeConstants::CTIME_BUFFER_SIZE]; | ||
result = LIBC_NAMESPACE::ctime_r(nullptr, buffer); | ||
ASSERT_ERRNO_EQ(EINVAL); | ||
ASSERT_STREQ(nullptr, result); | ||
|
||
time_t t; | ||
result = LIBC_NAMESPACE::ctime_r(&tm_data, nullptr); | ||
zimirza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ASSERT_ERRNO_EQ(EINVAL); | ||
ASSERT_STREQ(nullptr, result); | ||
} | ||
|
||
TEST(LlvmLibcCtimeR, ValidDate) { | ||
char buffer[TimeConstants::CTIME_BUFFER_SIZE]; | ||
struct time_t t; | ||
char *result; | ||
// 1970-01-01 00:00:00. Test with a valid buffer size. | ||
result = call_ctime_r(&t, | ||
1970, // year | ||
1, // month | ||
1, // day | ||
0, // hr | ||
0, // min | ||
0, // sec | ||
4, // wday | ||
0, // yday | ||
buffer); | ||
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.