| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "ooCommon.h" |
| 18 |
#include "ooDateTime.h" |
| 19 |
|
| 20 |
#if defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) |
| 21 |
|
| 22 |
#ifndef __MINGW32__ |
| 23 |
#include <SYS\TIMEB.H> |
| 24 |
int gettimeofday (struct timeval *tv, struct timezone *tz) |
| 25 |
{ |
| 26 |
struct _timeb currSysTime; |
| 27 |
_ftime(&currSysTime); |
| 28 |
|
| 29 |
tv->tv_sec = currSysTime.time; |
| 30 |
tv->tv_usec = currSysTime.millitm * 1000; |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define uint64 unsigned long long |
| 40 |
#define uint32 unsigned long |
| 41 |
|
| 42 |
static uint64 calcEpocOffset() { |
| 43 |
SYSTEMTIME st; |
| 44 |
FILETIME ft; |
| 45 |
|
| 46 |
memset(&st, 0, sizeof(st)); |
| 47 |
memset(&ft, 0, sizeof(ft)); |
| 48 |
|
| 49 |
st.wYear = 1970; |
| 50 |
st.wMonth = 1; |
| 51 |
st.wDayOfWeek = 0; |
| 52 |
st.wDay = 1; |
| 53 |
st.wHour = 0; |
| 54 |
st.wMinute = 0; |
| 55 |
st.wSecond = 0; |
| 56 |
st.wMilliseconds = 0; |
| 57 |
|
| 58 |
if (!SystemTimeToFileTime(&st, &ft)) { |
| 59 |
|
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
uint64 t = ft.dwHighDateTime; |
| 64 |
t = t << 32; |
| 65 |
t |= ft.dwLowDateTime; |
| 66 |
|
| 67 |
return t; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static uint64 getRawCurMsSpin() { |
| 72 |
FILETIME tm; |
| 73 |
uint64 t_now; |
| 74 |
|
| 75 |
static uint64 epocOffset = 0; |
| 76 |
static int do_once = 1; |
| 77 |
if (do_once) { |
| 78 |
epocOffset = calcEpocOffset(); |
| 79 |
do_once = 0; |
| 80 |
} |
| 81 |
|
| 82 |
GetSystemTimeAsFileTime(&tm); |
| 83 |
uint64 t_start = tm.dwHighDateTime; |
| 84 |
t_start = t_start << 32; |
| 85 |
t_start |= tm.dwLowDateTime; |
| 86 |
while (1) { |
| 87 |
GetSystemTimeAsFileTime(&tm); |
| 88 |
t_now = tm.dwHighDateTime; |
| 89 |
t_now = t_now << 32; |
| 90 |
t_now |= tm.dwLowDateTime; |
| 91 |
|
| 92 |
if (t_now != t_start) { |
| 93 |
|
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
t_now -= epocOffset; |
| 100 |
|
| 101 |
|
| 102 |
t_now = t_now / 10000; |
| 103 |
|
| 104 |
return t_now; |
| 105 |
} |
| 106 |
|
| 107 |
static uint64 baselineMs = 0; |
| 108 |
static uint32 tickBaseline = 0; |
| 109 |
|
| 110 |
int gettimeofday(struct timeval* tv, void* null) { |
| 111 |
|
| 112 |
|
| 113 |
if (baselineMs == 0) { |
| 114 |
baselineMs = getRawCurMsSpin(); |
| 115 |
tickBaseline = timeGetTime(); |
| 116 |
} |
| 117 |
|
| 118 |
uint32 curTicks = timeGetTime(); |
| 119 |
if (curTicks < tickBaseline) { |
| 120 |
|
| 121 |
baselineMs = getRawCurMsSpin(); |
| 122 |
tickBaseline = timeGetTime(); |
| 123 |
} |
| 124 |
|
| 125 |
uint64 now_ms = (baselineMs + (curTicks - tickBaseline)); |
| 126 |
*tv = oo_ms_to_tv(now_ms); |
| 127 |
return 0; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
uint64 getCurMsFromClock() { |
| 135 |
|
| 136 |
FILETIME tm; |
| 137 |
|
| 138 |
static uint64 offset = 0; |
| 139 |
static int do_once = 1; |
| 140 |
if (do_once) { |
| 141 |
offset = calcEpocOffset(); |
| 142 |
do_once = 0; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
GetSystemTimeAsFileTime(&tm); |
| 147 |
uint64 t = tm.dwHighDateTime; |
| 148 |
t = t << 32; |
| 149 |
t |= tm.dwLowDateTime; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
t -= offset; |
| 157 |
|
| 158 |
|
| 159 |
t = t / 10000; |
| 160 |
return t; |
| 161 |
} |
| 162 |
#endif |
| 163 |
#endif |
| 164 |
|
| 165 |
int ooGetTimeOfDay (struct timeval *tv, struct timezone *tz) |
| 166 |
{ |
| 167 |
return gettimeofday (tv, tz); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
long ooGetTimeDiff(struct timeval *tv1, struct timeval *tv2) |
| 172 |
{ |
| 173 |
return ( ((tv2->tv_sec-tv1->tv_sec)*1000) + |
| 174 |
((tv2->tv_usec-tv1->tv_usec)/1000) ); |
| 175 |
} |