-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
13 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,56 @@ | ||
/* | ||
* Copyright (c) 2024 Meta Platforms | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#include <zephyr/sys/util.h> | ||
|
||
#define DATE_STRING_BUF_SZ 26U | ||
#define DATE_WDAY_STRING_SZ 7U | ||
#define DATE_MON_STRING_SZ 12U | ||
#define DATE_TM_YEAR_BASE 1900 | ||
|
||
static char *asctime_impl(const struct tm *tp, char *buf) | ||
{ | ||
static const char wday_str[DATE_WDAY_STRING_SZ][3] = { | ||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", | ||
}; | ||
static const char mon_str[DATE_MON_STRING_SZ][3] = { | ||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | ||
}; | ||
|
||
if ((buf == NULL) || (tp == NULL) || ((unsigned int)tp->tm_wday >= DATE_WDAY_STRING_SZ) || | ||
((unsigned int)tp->tm_mon >= DATE_MON_STRING_SZ)) { | ||
return NULL; | ||
} | ||
|
||
unsigned int n = (unsigned int)snprintf( | ||
buf, DATE_STRING_BUF_SZ, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", wday_str[tp->tm_wday], | ||
mon_str[tp->tm_mon], tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, | ||
DATE_TM_YEAR_BASE + tp->tm_year); | ||
|
||
if (n >= DATE_STRING_BUF_SZ) { | ||
return NULL; | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
char *asctime(const struct tm *tp) | ||
{ | ||
static char buf[DATE_STRING_BUF_SZ]; | ||
|
||
return asctime_impl(tp, buf); | ||
} | ||
|
||
#if defined(CONFIG_COMMON_LIBC_ASCTIME_R) | ||
char *asctime_r(const struct tm *tp, char *buf) | ||
{ | ||
return asctime_impl(tp, buf); | ||
} | ||
#endif /* CONFIG_COMMON_LIBC_ASCTIME_R */ |
This file contains 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,26 @@ | ||
/* | ||
* Copyright (c) 2024 Meta Platforms | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <time.h> | ||
|
||
/** | ||
* `ctime()` is equivalent to `asctime(localtime(clock))` | ||
* See: https://pubs.opengroup.org/onlinepubs/009695399/functions/ctime.html | ||
*/ | ||
|
||
char *ctime(const time_t *clock) | ||
{ | ||
return asctime(localtime(clock)); | ||
} | ||
|
||
#if defined(CONFIG_COMMON_LIBC_CTIME_R) | ||
char *ctime_r(const time_t *clock, char *buf) | ||
{ | ||
struct tm tmp; | ||
|
||
return asctime_r(localtime_r(clock, &tmp), buf); | ||
} | ||
#endif /* CONFIG_COMMON_LIBC_CTIME_R */ |
This file contains 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,17 @@ | ||
/* | ||
* Copyright (c) 2024 Meta Platforms | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <time.h> | ||
|
||
struct tm *localtime_r(const time_t *timer, struct tm *result) | ||
{ | ||
return gmtime_r(timer, result); | ||
} | ||
|
||
struct tm *localtime(const time_t *timer) | ||
{ | ||
return gmtime(timer); | ||
} |
This file contains 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 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 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 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,18 @@ | ||
# Copyright (c) 2024 Tenstorrent AI ULC | ||
# Copyright (c) 2024 Meta Platforms | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config POSIX_C_LANG_SUPPORT_R | ||
bool "Thread-Safe General ISO C Library" | ||
select COMMON_LIBC_ASCTIME_R | ||
select COMMON_LIBC_CTIME_R | ||
select COMMON_LIBC_GMTIME_R | ||
select COMMON_LIBC_LOCALTIME_R_UTC | ||
help | ||
Select 'y' here and Zephyr will provide an implementation of the POSIX_C_LANG_SUPPORT_R | ||
Option Group, consisting of asctime_r(), ctime_r(), gmtime_r(), localtime_r(), rand_r(), | ||
strerror_r(), and strtok_r() | ||
|
||
For more informnation, please see | ||
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html |
This file contains 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 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 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