| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
|
| 23 |
#include <sys/time.h> |
| 24 |
|
| 25 |
#include <time.h> |
| 26 |
|
| 27 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 28 |
# define WINDOWS_NATIVE |
| 29 |
# include <windows.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "localtime-buffer.h" |
| 33 |
|
| 34 |
#ifdef WINDOWS_NATIVE |
| 35 |
|
| 36 |
|
| 37 |
# define GetProcAddress \ |
| 38 |
(void *) GetProcAddress |
| 39 |
|
| 40 |
|
| 41 |
typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime); |
| 42 |
static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL; |
| 43 |
static BOOL initialized = FALSE; |
| 44 |
|
| 45 |
static void |
| 46 |
initialize (void) |
| 47 |
{ |
| 48 |
HMODULE kernel32 = LoadLibrary ("kernel32.dll"); |
| 49 |
if (kernel32 != NULL) |
| 50 |
{ |
| 51 |
GetSystemTimePreciseAsFileTimeFunc = |
| 52 |
(GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime"); |
| 53 |
} |
| 54 |
initialized = TRUE; |
| 55 |
} |
| 56 |
|
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
int |
| 68 |
gettimeofday (struct timeval *restrict tv, void *restrict tz) |
| 69 |
{ |
| 70 |
#undef gettimeofday |
| 71 |
#ifdef WINDOWS_NATIVE |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
FILETIME current_time; |
| 86 |
|
| 87 |
if (!initialized) |
| 88 |
initialize (); |
| 89 |
if (GetSystemTimePreciseAsFileTimeFunc != NULL) |
| 90 |
GetSystemTimePreciseAsFileTimeFunc (¤t_time); |
| 91 |
else |
| 92 |
GetSystemTimeAsFileTime (¤t_time); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ULONGLONG since_1601 = |
| 97 |
((ULONGLONG) current_time.dwHighDateTime << 32) |
| 98 |
| (ULONGLONG) current_time.dwLowDateTime; |
| 99 |
|
| 100 |
|
| 101 |
ULONGLONG since_1970 = |
| 102 |
since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000; |
| 103 |
ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10; |
| 104 |
tv->tv_sec = microseconds_since_1970 / (ULONGLONG) 1000000; |
| 105 |
tv->tv_usec = microseconds_since_1970 % (ULONGLONG) 1000000; |
| 106 |
|
| 107 |
return 0; |
| 108 |
|
| 109 |
#else |
| 110 |
|
| 111 |
# if HAVE_GETTIMEOFDAY |
| 112 |
# if GETTIMEOFDAY_CLOBBERS_LOCALTIME |
| 113 |
|
| 114 |
|
| 115 |
struct tm save = *localtime_buffer_addr; |
| 116 |
# endif |
| 117 |
|
| 118 |
# if defined timeval |
| 119 |
# undef timeval |
| 120 |
struct timeval otv; |
| 121 |
int result = gettimeofday (&otv, (struct timezone *) tz); |
| 122 |
if (result == 0) |
| 123 |
{ |
| 124 |
tv->tv_sec = otv.tv_sec; |
| 125 |
tv->tv_usec = otv.tv_usec; |
| 126 |
} |
| 127 |
# else |
| 128 |
int result = gettimeofday (tv, (struct timezone *) tz); |
| 129 |
# endif |
| 130 |
|
| 131 |
# if GETTIMEOFDAY_CLOBBERS_LOCALTIME |
| 132 |
*localtime_buffer_addr = save; |
| 133 |
# endif |
| 134 |
|
| 135 |
return result; |
| 136 |
|
| 137 |
# else |
| 138 |
|
| 139 |
# if !defined OK_TO_USE_1S_CLOCK |
| 140 |
# error "Only 1-second nominal clock resolution found. Is that intended?" \ |
| 141 |
"If so, compile with the -DOK_TO_USE_1S_CLOCK option." |
| 142 |
# endif |
| 143 |
tv->tv_sec = time (NULL); |
| 144 |
tv->tv_usec = 0; |
| 145 |
|
| 146 |
return 0; |
| 147 |
|
| 148 |
# endif |
| 149 |
#endif |
| 150 |
} |