libc: common: implement multiple time functions
Implemented the following: - `asctime_r()` - `asctime()` - `localtime()` - `localtime_r()` - `ctime()` - `ctime_r()` Specifically: - the implementation of `localtime()` & `localtime_r()` simply wraps around the gmtime() & gmtime_r() functions, the results are always expressed as UTC. - `ctime()` is equivalent to `asctime(localtime(clock))`, it inherits the limitation of `localtime()` as well, which only supports UTC results currently. Added tests for these newly implemented functions. Signed-off-by: Yong Cong Sin <ycsin@meta.com> Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
This commit is contained in:
parent
5978e5231e
commit
fe94d4354a
13 changed files with 256 additions and 5 deletions
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/__assert.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
#include <limits.h>
|
||||
|
@ -1075,7 +1076,7 @@ ZTEST(libc_common, test_strtok_r)
|
|||
*
|
||||
* @see gmtime(),gmtime_r().
|
||||
*/
|
||||
ZTEST(libc_common, test_time)
|
||||
ZTEST(libc_common, test_time_gmtime)
|
||||
{
|
||||
time_t tests1 = 0;
|
||||
time_t tests2 = -5;
|
||||
|
@ -1092,6 +1093,86 @@ ZTEST(libc_common, test_time)
|
|||
zassert_not_null(gmtime_r(&tests4, &tp), "gmtime_r failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test time function
|
||||
*
|
||||
* @see asctime(), asctime_r().
|
||||
*/
|
||||
ZTEST(libc_common, test_time_asctime)
|
||||
{
|
||||
char buf[26] = {0};
|
||||
struct tm tp = {
|
||||
.tm_sec = 10, /* Seconds */
|
||||
.tm_min = 30, /* Minutes */
|
||||
.tm_hour = 14, /* Hour (24-hour format) */
|
||||
.tm_wday = 5, /* Day of the week (0-6, 0 = Sun) */
|
||||
.tm_mday = 1, /* Day of the month */
|
||||
.tm_mon = 5, /* Month (0-11, January = 0) */
|
||||
.tm_year = 124, /* Year (current year - 1900) */
|
||||
};
|
||||
|
||||
zassert_not_null(asctime_r(&tp, buf));
|
||||
zassert_equal(strncmp("Fri Jun 1 14:30:10 2024\n", buf, sizeof(buf)), 0);
|
||||
|
||||
zassert_not_null(asctime(&tp));
|
||||
zassert_equal(strncmp("Fri Jun 1 14:30:10 2024\n", asctime(&tp), sizeof(buf)), 0);
|
||||
|
||||
if (IS_ENABLED(CONFIG_COMMON_LIBC_ASCTIME_R)) {
|
||||
zassert_is_null(asctime_r(NULL, buf));
|
||||
zassert_is_null(asctime(NULL));
|
||||
|
||||
zassert_is_null(asctime_r(&tp, NULL));
|
||||
|
||||
tp.tm_wday = 8;
|
||||
zassert_is_null(asctime_r(&tp, buf));
|
||||
zassert_is_null(asctime(&tp));
|
||||
|
||||
tp.tm_wday = 5;
|
||||
tp.tm_mon = 12;
|
||||
zassert_is_null(asctime_r(&tp, buf));
|
||||
zassert_is_null(asctime(&tp));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test time function
|
||||
*
|
||||
* @see localtime(), localtime_r().
|
||||
*/
|
||||
ZTEST(libc_common, test_time_localtime)
|
||||
{
|
||||
time_t tests1 = 0;
|
||||
time_t tests2 = -5;
|
||||
time_t tests3 = (time_t) -214748364800;
|
||||
time_t tests4 = 951868800;
|
||||
|
||||
struct tm tp;
|
||||
|
||||
zassert_not_null(localtime(&tests1), "localtime failed");
|
||||
zassert_not_null(localtime(&tests2), "localtime failed");
|
||||
|
||||
tp.tm_wday = -5;
|
||||
zassert_not_null(localtime_r(&tests3, &tp), "localtime_r failed");
|
||||
zassert_not_null(localtime_r(&tests4, &tp), "localtime_r failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test time function
|
||||
*
|
||||
* @see ctime(), ctime_r().
|
||||
*/
|
||||
ZTEST(libc_common, test_time_ctime)
|
||||
{
|
||||
char buf[26] = {0};
|
||||
time_t test1 = 1718260000;
|
||||
|
||||
zassert_not_null(ctime_r(&test1, buf));
|
||||
zassert_equal(strncmp("Thu Jun 13 06:26:40 2024\n", buf, sizeof(buf)), 0);
|
||||
|
||||
zassert_not_null(ctime(&test1));
|
||||
zassert_equal(strncmp("Thu Jun 13 06:26:40 2024\n", ctime(&test1), sizeof(buf)), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Test rand function
|
||||
|
|
|
@ -13,6 +13,8 @@ tests:
|
|||
- CONFIG_MINIMAL_LIBC=y
|
||||
- CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS=y
|
||||
- CONFIG_MINIMAL_LIBC_RAND=y
|
||||
- CONFIG_COMMON_LIBC_ASCTIME_R=y
|
||||
- CONFIG_COMMON_LIBC_CTIME_R=y
|
||||
libraries.libc.common.newlib:
|
||||
filter: CONFIG_NEWLIB_LIBC_SUPPORTED
|
||||
min_ram: 32
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue