libc: Implement time() using clock_gettime().
All checks were successful
continuous-integration/drone/push Build is passing
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:
parent
a8a64863c8
commit
7fb2807d0c
@ -16,9 +16,8 @@ int main()
|
|||||||
|
|
||||||
console_print(buffer);
|
console_print(buffer);
|
||||||
|
|
||||||
struct timespec ts;
|
time_t now = time(NULL);
|
||||||
clock_gettime(CLOCK_REALTIME, &ts);
|
snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s\n", now);
|
||||||
snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s, %ld ns\n", ts.tv_sec, ts.tv_nsec);
|
|
||||||
|
|
||||||
console_print(buffer);
|
console_print(buffer);
|
||||||
|
|
||||||
|
@ -12,6 +12,9 @@ extern "C"
|
|||||||
/* Get the current value of a system clock. */
|
/* Get the current value of a system clock. */
|
||||||
int clock_gettime(clockid_t id, struct timespec* ts);
|
int clock_gettime(clockid_t id, struct timespec* ts);
|
||||||
|
|
||||||
|
/* Get the current wall clock time. */
|
||||||
|
time_t time(time_t* tp);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,4 +10,14 @@ extern "C"
|
|||||||
long rc = syscall(SYS_clock_gettime, id, ts);
|
long rc = syscall(SYS_clock_gettime, id, ts);
|
||||||
__errno_return(rc, int);
|
__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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user