libc: Implement time() using clock_gettime().
All checks were successful
continuous-integration/drone/push Build is passing

The cool POSIX kids use clock_gettime() now because it has NANOSECONDS (and different clocks!),
but ANSI C prefers this function.

We can still implement it based on clock_gettime(), we just have to discard the NANOSECONDS.
This commit is contained in:
apio 2023-01-07 00:27:23 +01:00
parent a8a64863c8
commit 7fb2807d0c
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 15 additions and 3 deletions

View File

@ -16,9 +16,8 @@ int main()
console_print(buffer);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s, %ld ns\n", ts.tv_sec, ts.tv_nsec);
time_t now = time(NULL);
snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s\n", now);
console_print(buffer);

View File

@ -12,6 +12,9 @@ extern "C"
/* Get the current value of a system clock. */
int clock_gettime(clockid_t id, struct timespec* ts);
/* Get the current wall clock time. */
time_t time(time_t* tp);
#ifdef __cplusplus
}
#endif

View File

@ -10,4 +10,14 @@ extern "C"
long rc = syscall(SYS_clock_gettime, id, ts);
__errno_return(rc, int);
}
time_t time(time_t* tp)
{
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) return (time_t)-1;
if (tp) *tp = ts.tv_sec;
return ts.tv_sec;
}
}