diff --git a/arch/posix/include/posix_cheats.h b/arch/posix/include/posix_cheats.h index b18d7756f7f..576db1635e8 100644 --- a/arch/posix/include/posix_cheats.h +++ b/arch/posix/include/posix_cheats.h @@ -26,6 +26,7 @@ #ifdef CONFIG_PTHREAD_IPC #define timespec zap_timespec +#define timeval zap_timeval #define pthread_mutex_t zap_pthread_mutex_t #define pthread_mutexattr_t zap_pthread_mutexattr_t #define pthread_cond_t zap_pthread_cond_t @@ -131,6 +132,7 @@ /* Clock */ #define clock_gettime(...) zap_clock_gettime(__VA_ARGS__) #define clock_settime(...) zap_clock_settime(__VA_ARGS__) +#define gettimeofday(...) zap_clock_gettimeofday(__VA_ARGS__) /* Timer */ #define timer_create(...) zap_timer_create(__VA_ARGS__) diff --git a/include/posix/time.h b/include/posix/time.h index fbbc70a3955..e0b53a5d38e 100644 --- a/include/posix/time.h +++ b/include/posix/time.h @@ -20,6 +20,11 @@ struct itimerspec { struct timespec it_value; /* Timer expiration */ }; +struct timeval { + signed int tv_sec; + signed int tv_usec; +}; + #include #include #include "sys/types.h" @@ -63,6 +68,8 @@ int timer_gettime(timer_t timerid, struct itimerspec *its); int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue); +int gettimeofday(struct timeval *tv, const void *tz); + #ifdef __cplusplus } #endif diff --git a/lib/posix/clock.c b/lib/posix/clock.c index 3ed4363c424..631e35a6cf0 100644 --- a/lib/posix/clock.c +++ b/lib/posix/clock.c @@ -29,3 +29,24 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts) return 0; } + +/** + * @brief Get current real time. + * + * See IEEE 1003.1 + */ +int gettimeofday(struct timeval *tv, const void *tz) +{ + struct timespec ts; + int res; + + /* As per POSIX, "if tzp is not a null pointer, the behavior + * is unspecified." "tzp" is the "tz" parameter above. */ + ARG_UNUSED(tv); + + res = clock_gettime(CLOCK_REALTIME, &ts); + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; + + return res; +}