Evil: add gettimeofday()

Add gettimeofday implementation based on the mingw one, using a more acurate function if Windows >= 8

Reviewed-by: João Paulo Taylor Ienczak Zanette <joao.tiz@expertisesolutions.com.br>
Reviewed-by: Stefan Schmidt <stefan@datenfreihafen.org>
Differential Revision: https://phab.enlightenment.org/D12111
This commit is contained in:
Vincent Torri 2020-08-26 18:44:28 +00:00 committed by Stefan Schmidt
parent 33304d656c
commit d910fa3fa7
4 changed files with 97 additions and 2 deletions

View File

@ -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 */

View File

@ -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
*/

View File

@ -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

View File

@ -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 .