From d910fa3fa77860ea2e36c38d0c4b7212d203d159 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Wed, 26 Aug 2020 18:44:28 +0000 Subject: [PATCH] Evil: add gettimeofday() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add gettimeofday implementation based on the mingw one, using a more acurate function if Windows >= 8 Reviewed-by: João Paulo Taylor Ienczak Zanette Reviewed-by: Stefan Schmidt Differential Revision: https://phab.enlightenment.org/D12111 --- src/lib/evil/evil_dlfcn.h | 4 +-- src/lib/evil/evil_macro_wrapper.h | 16 ++++++++++ src/lib/evil/evil_time.c | 52 +++++++++++++++++++++++++++++++ src/lib/evil/evil_time.h | 27 ++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/lib/evil/evil_dlfcn.h b/src/lib/evil/evil_dlfcn.h index 22f48b72ca..c55ce2562e 100644 --- a/src/lib/evil/evil_dlfcn.h +++ b/src/lib/evil/evil_dlfcn.h @@ -194,7 +194,7 @@ EAPI int dlclose(void* handle); */ EAPI void *dlsym(void* handle, const char* symbol); #ifndef HAVE_DLSYM -#define HAVE_DLSYM 1 +# define HAVE_DLSYM 1 #endif #ifdef _GNU_SOURCE @@ -223,7 +223,7 @@ EAPI void *dlsym(void* handle, const char* symbol); */ EAPI int dladdr (const void *addr, Dl_info *info); #ifndef HAVE_DLADDR -#define HAVE_DLADDR 1 +# define HAVE_DLADDR 1 #endif #endif /* _GNU_SOURCE */ diff --git a/src/lib/evil/evil_macro_wrapper.h b/src/lib/evil/evil_macro_wrapper.h index cd650e5e12..640f5a307e 100644 --- a/src/lib/evil/evil_macro_wrapper.h +++ b/src/lib/evil/evil_macro_wrapper.h @@ -45,6 +45,22 @@ #endif #define mkdir(dirname, mode) evil_mkdir(dirname, mode) +/* + * evil_time.h + */ + +/** + * @def gettimeofday(tv, tz) + * + * Wrapper around evil_gettimeofday(). + * + * @since 1.25 + */ +#ifndef _GETTIMEOFDAY_DEFINED +# define _GETTIMEOFDAY_DEFINED +# define gettimeofday(tv, tz) evil_gettimeofday(tv, tz) +#endif + /* * evil_unistd.h */ diff --git a/src/lib/evil/evil_time.c b/src/lib/evil/evil_time.c index 32cfc2cf07..7d8de8f37c 100644 --- a/src/lib/evil/evil_time.c +++ b/src/lib/evil/evil_time.c @@ -9,6 +9,58 @@ #include "evil_private.h" +/* + * gettimeofday + * based on https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c + * public domain + */ + +#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */ + +int evil_gettimeofday(struct timeval *tv, struct timezone *tz) +{ + int res = 0; + union + { + unsigned long long ns100; /* time since 1 Jan 1601 in 100ns units */ + FILETIME ft; + } _now; + TIME_ZONE_INFORMATION time_zone_information; + DWORD tzi; + + if (tz != NULL) + { + tzi = GetTimeZoneInformation(&time_zone_information); + if (tzi != TIME_ZONE_ID_INVALID) + { + tz->tz_minuteswest = time_zone_information.Bias; + if (tzi == TIME_ZONE_ID_DAYLIGHT) + tz->tz_dsttime = 1; + else + tz->tz_dsttime = 0; + } + else + { + tz->tz_minuteswest = 0; + tz->tz_dsttime = 0; + } + } + + if (tv != NULL) + { +#if _WIN32_WINNT < 0x0602 + GetSystemTimeAsFileTime(&_now.ft); +#else + GetSystemTimePreciseAsFileTime(&_now.ft); +#endif + _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */ + tv->tv_sec = _now.ns100 / 10000000ull; /* seconds since 1-1-1970 */ + tv->tv_usec = (long) (_now.ns100 % 10000000ull) /10; /* nanoseconds */ + } + + return res; +} + /* * strptime * based on http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD diff --git a/src/lib/evil/evil_time.h b/src/lib/evil/evil_time.h index 42004144a2..e61692e079 100644 --- a/src/lib/evil/evil_time.h +++ b/src/lib/evil/evil_time.h @@ -16,6 +16,33 @@ * @{ */ +#ifdef _MSC_VER +struct timezone +{ + int tz_minuteswest; /* of Greenwich */ + int tz_dsttime; /* type of dst correction to apply */ +}; +#endif + +/** + * @brief Get time and timezone. + * + * @param tv A pointer that contains two sockets. + * @param tz A pointer that contains two sockets. + * @return 0 on success, -1 otherwise. + * + * This function gets the time and timezone. @p tv and @p tz can be + * @c NULL. It calls GetSystemTimePreciseAsFileTime() on Windows 8or + * above if _WIN32_WINNT is correctly defined. It returns 0 on + * success, -1 otherwise. + * + * @since 1.25 + */ +EAPI int evil_gettimeofday(struct timeval *tv, struct timezone *tz); +#ifndef HAVE_GETTIMEOFDAY +# define HAVE_GETTIMEOFDAY 1 +#endif + /** * @brief Convert a string representation of time to a time tm structure .