diff --git a/apps/app.c b/apps/app.c index 73a9dbff..2e185b9b 100644 --- a/apps/app.c +++ b/apps/app.c @@ -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); diff --git a/libc/include/time.h b/libc/include/time.h index 9a951da0..82eb7884 100644 --- a/libc/include/time.h +++ b/libc/include/time.h @@ -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 diff --git a/libc/src/time.cpp b/libc/src/time.cpp index aaa33f57..b33a3c6c 100644 --- a/libc/src/time.cpp +++ b/libc/src/time.cpp @@ -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; + } }