From a7c6163e7c54a3231d0f133533dff72a72ba0921 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 12:05:22 +0200 Subject: [PATCH] libc: Add gettimeofday() --- libc/include/bits/timespec.h | 8 +++++++- libc/include/sys/time.h | 21 +++++++++++++++++++++ libc/include/sys/types.h | 1 + libc/src/time.cpp | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 libc/include/sys/time.h diff --git a/libc/include/bits/timespec.h b/libc/include/bits/timespec.h index a5866912..2cdfcdc3 100644 --- a/libc/include/bits/timespec.h +++ b/libc/include/bits/timespec.h @@ -1,4 +1,4 @@ -/* bits/timespec.h: The timespec structure. */ +/* bits/timespec.h: The timespec and timeval structures. */ #ifndef _BITS_TIMESPEC_H #define _BITS_TIMESPEC_H @@ -11,4 +11,10 @@ struct timespec long tv_nsec; }; +struct timeval +{ + time_t tv_sec; + suseconds_t tv_usec; +}; + #endif diff --git a/libc/include/sys/time.h b/libc/include/sys/time.h new file mode 100644 index 00000000..761b2208 --- /dev/null +++ b/libc/include/sys/time.h @@ -0,0 +1,21 @@ +/* sys/time.h: POSIX time types. */ + +#ifndef _SYS_TIME_H +#define _SYS_TIME_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Get the current time of day. */ + __deprecated int gettimeofday(struct timeval* tp, void* timezone); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h index f082063f..0ee7f598 100644 --- a/libc/include/sys/types.h +++ b/libc/include/sys/types.h @@ -19,6 +19,7 @@ typedef __u64_t ino_t; typedef __u32_t uid_t; typedef __u32_t gid_t; typedef __u64_t nlink_t; +typedef __i64_t suseconds_t; typedef off_t fpos_t; diff --git a/libc/src/time.cpp b/libc/src/time.cpp index 8224c7d4..33c6a22e 100644 --- a/libc/src/time.cpp +++ b/libc/src/time.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include #include @@ -148,4 +150,21 @@ extern "C" { return asctime(localtime(tp)); } + + int gettimeofday(struct timeval* tp, void* timezone) + { + if (timezone) + { + fputs("gettimeofday: timezone was not NULL, abort\n", stderr); + abort(); + } + + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) < 0) return -1; + + tp->tv_sec = ts.tv_sec; + tp->tv_usec = ts.tv_nsec / 1000; + + return 0; + } }