-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
posix: implement _POSIX_THREAD_SAFE_FUNCTIONS
's time functions
#74180
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 */ |
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 */ |
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); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+9
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||
|
||||||||||||||||||||||
struct tm *localtime(const time_t *timer) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
return gmtime(timer); | ||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,6 +39,9 @@ config MINIMAL_LIBC_TIME | |||||
bool "Time functions" | ||||||
select COMMON_LIBC_TIME if POSIX_TIMERS | ||||||
select COMMON_LIBC_GMTIME_R | ||||||
select COMMON_LIBC_ASCTIME | ||||||
select COMMON_LIBC_LOCALTIME_R_UTC | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
select COMMON_LIBC_CTIME | ||||||
default y | ||||||
help | ||||||
Enable time() and gmtime_r() for the minimal libc. | ||||||
|
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one can be abbreviated without the UTC.
From here
So if our implementation of
localtime_r()
never sets the timezone (i.e. is UTC-only), it's still compliant. No special treatment necessary. My assumption is that most would probably like to avoid bringing timezone details into Zephyr.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
COMMON_LIBC_LOCALTIME_R_UTC
is shared by both thelocaltime()
&localtime_r()
, I guess that's why I added that suffix to make it obvious that both functions only work for UTC, should I still remove the_UTC
suffix? Or maybe I should create another Kconfig forlocaltime()
?