<time.h>: change the new C23 TIME_ constants.
Jens Gustedt suggested a better implementation last year on the musl
mailing list: https://www.openwall.com/lists/musl/2022/11/19/1
It means the constants are sparse, but in return it means we can add
future constants and they'll be backward compatible. (Sadly you'll need
to be on API level 35 before you can use anything but TIME_UTC.)
I doubt this will ever matter, because everyone should just stick to
clock_gettime()/clock_getres() anyway, and anyone who does have a
legitimate use for timespec_get() and timespec_getres() probably needs
to support non-Linux and so can't use any clocks that aren't in ISO C
anyway. But given that we don't _have_ to paint ourselves into a corner
here, we may as well take the opportunity to not do so.
Test: strace
Change-Id: I293d32fcbcf7f6703564dac0978ae2a10192a482
diff --git a/libc/bionic/time.cpp b/libc/bionic/time.cpp
index 3a41027..800395e 100644
--- a/libc/bionic/time.cpp
+++ b/libc/bionic/time.cpp
@@ -28,27 +28,10 @@
#include <time.h>
-static clockid_t __base_to_clock(int base) {
- switch (base) {
- case TIME_UTC:
- return CLOCK_REALTIME;
- case TIME_MONOTONIC:
- return CLOCK_MONOTONIC;
- case TIME_ACTIVE:
- return CLOCK_PROCESS_CPUTIME_ID;
- case TIME_THREAD_ACTIVE:
- return CLOCK_THREAD_CPUTIME_ID;
- default:
- return -1;
- }
-}
-
int timespec_get(timespec* ts, int base) {
- clockid_t clock = __base_to_clock(base);
- return (clock != -1 && clock_gettime(clock, ts) != -1) ? base : 0;
+ return (clock_gettime(base - 1, ts) != -1) ? base : 0;
}
int timespec_getres(timespec* ts, int base) {
- clockid_t clock = __base_to_clock(base);
- return (clock != -1 && clock_getres(clock, ts) != -1) ? base : 0;
+ return (clock_getres(base - 1, ts) != -1) ? base : 0;
}