Skip to content

Commit efd71d9

Browse files
authored
[libc] Allow time conversions to compile on bare metal (#102014)
The `<time.h>` functions `asctime`, `gmtime`, `mktime` and their `_r` variants are purely computational functions which convert between well defined time representations and/or strings. There's no reason these shouldn't be available in bare-metal builds of the library as well as hosted ones: even if the library has no way to find out the time in POSIX format, it might still see POSIX-style `time_t` values in input data (e.g. network protocols) and need to interpret them. The only obstacle to this was that the `out_of_range()` helper function set `errno` to `EOVERFLOW`, which fails in a bare-metal build because the extra POSIX error values aren't defined, including `EOVERFLOW`. So I've made that assignment conditional on `EOVERFLOW` being defined. Fixes #85556.
1 parent 441b672 commit efd71d9

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ set(TARGET_LIBC_ENTRYPOINTS
201201
libc.src.stdlib.strtoull
202202

203203
# time.h entrypoints
204+
libc.src.time.asctime
205+
libc.src.time.asctime_r
204206
libc.src.time.difftime
207+
libc.src.time.gmtime
208+
libc.src.time.gmtime_r
209+
libc.src.time.mktime
205210

206211
# internal entrypoints
207212
libc.startup.baremetal.init

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ set(TARGET_LIBC_ENTRYPOINTS
197197
libc.src.stdlib.strtoull
198198

199199
# time.h entrypoints
200+
libc.src.time.asctime
201+
libc.src.time.asctime_r
200202
libc.src.time.difftime
203+
libc.src.time.gmtime
204+
libc.src.time.gmtime_r
205+
libc.src.time.mktime
201206

202207
# internal entrypoints
203208
libc.startup.baremetal.init

libc/src/time/time_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
9292

9393
// POSIX.1-2017 requires this.
9494
LIBC_INLINE time_t out_of_range() {
95+
#ifdef EOVERFLOW
96+
// For non-POSIX uses of the standard C time functions, where EOVERFLOW is
97+
// not defined, it's OK not to set errno at all. The plain C standard doesn't
98+
// require it.
9599
libc_errno = EOVERFLOW;
100+
#endif
96101
return TimeConstants::OUT_OF_RANGE_RETURN_VALUE;
97102
}
98103

0 commit comments

Comments
 (0)