Luna/libc/src/time.cpp
apio 7fb2807d0c
All checks were successful
continuous-integration/drone/push Build is passing
libc: Implement time() using clock_gettime().
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.
2023-01-07 00:27:23 +01:00

24 lines
467 B
C++

#include <bits/errno-return.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
extern "C"
{
int clock_gettime(clockid_t id, struct timespec* ts)
{
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;
}
}