99 #if 0 // def __APPLE__, it is in fact available on apple, so...
105 #ifndef CLOCK_REALTIME
106 #define CLOCK_REALTIME 0
108 static int clock_gettime(
int clk_id,
struct timespec* tp)
113 int rv = gettimeofday(&now, NULL);
117 tp->tv_sec = now.tv_sec;
118 tp->tv_nsec = now.tv_usec * 1000;
137 typedef void(WINAPI * GetSystemTimeType)(LPFILETIME);
138 static GetSystemTimeType getSystemTime = 0;
139 static int havePreciseFileTime = 0;
140 if (getSystemTime == 0)
146 getSystemTime = (GetSystemTimeType)GetProcAddress(GetModuleHandleA(
"Kernel32"),
"GetSystemTimePreciseAsFileTime");
148 havePreciseFileTime = 1;
150 getSystemTime = GetSystemTimeAsFileTime;
153 getSystemTime = GetSystemTimeAsFileTime;
156 if (havePreciseFileTime)
162 t = (int64_t)(((((uint64_t) now.dwHighDateTime << 32) | now.dwLowDateTime) / 10000) - 11644473600000);
165 _localtime64_s(date_, &tin);
167 *secs_ = (time_t) tin;
173 static const int64_t CORRECTION_DELTA_MS = 32;
174 static int64_t startTimePerfCount;
175 static int64_t startTimeSysTime;
176 static int64_t perfCountFreq = 0;
182 GetSystemTimeAsFileTime(&now);
183 t = (int64_t)(((((uint64_t) now.dwHighDateTime << 32) | now.dwLowDateTime) / 10000) - 11644473600000);
185 if (QueryPerformanceCounter(&pc))
191 QueryPerformanceFrequency(&tmp);
192 perfCountFreq = tmp.QuadPart;
193 startTimePerfCount = pc.QuadPart;
194 startTimeSysTime = t;
197 tNow = startTimeSysTime + (1000 * (pc.QuadPart - startTimePerfCount)) / perfCountFreq;
199 if (t > tNow || (tNow - t) > CORRECTION_DELTA_MS)
201 startTimePerfCount = pc.QuadPart;
202 startTimeSysTime = t;
210 _localtime64_s(date_, &tin);
212 *secs_ = (time_t) tin;
217 clock_gettime(CLOCK_REALTIME, &tp);
220 localtime_r(&tp.tv_sec, date_);
223 secs_[0] = tp.tv_sec;
240 _localtime64_s(date, &t);
247 struct tm* result = localtime(&t);
248 memcpy(date, result,
sizeof(
struct tm));
263 unsigned int year, month;
270 year = (
unsigned int) (dt.tm_year + 1900);
271 month = (
unsigned int) (dt.tm_mon + 1);
274 #if defined(__GNUC__) && !defined(__APPLE__) && !defined(__ANDROID_API__)
275 #pragma GCC diagnostic push
276 #pragma GCC diagnostic ignored "-Wformat-truncation" // valid date values will always fit properly
278 snprintf(tmpDest, 9,
"%04u%02u%02u", year, month, (
unsigned int) dt.tm_mday);
279 #if defined(__GNUC__) && !defined(__APPLE__) && !defined(__ANDROID_API__)
280 #pragma GCC diagnostic pop
282 memcpy(dest, tmpDest, 8);
302 snprintf(tmpDest, 9,
"%02d%02d%02d%02d", dt.tm_hour, dt.tm_min, dt.tm_sec, 0);
303 memcpy(dest, tmpDest, 8);
322 year = dt.tm_year + 1900;
323 month = dt.tm_mon + 1;
324 swprintf(dest, 9, L
"%04d%02d%02d", year, month, dt.tm_mday);
343 swprintf(dest, 9, L
"%02d%02d%02d%02d", dt.tm_hour, dt.tm_min, dt.tm_sec, 0);
372 LARGE_INTEGER freq,
start, stop;
373 double countPerMicroSecond ;
374 QueryPerformanceCounter(&
start);
375 QueryPerformanceFrequency(&freq);
376 countPerMicroSecond = freq.QuadPart / 1.0e6;
379 QueryPerformanceCounter(&stop);
380 }
while (
start.QuadPart + ((
double)(us) * countPerMicroSecond) > stop.QuadPart);
385 ts.tv_sec = us / 1000000;
386 ts.tv_nsec = ((long)us - (ts.tv_sec * 1000000)) * 1000;
390 ret = nanosleep(&ts, &ts);
392 assert(errno == EINTR);
416 int64_t XsTime_utcToLocalValue = 0;
417 int64_t XsTime_localToUtcValue = 0;
428 FILETIME now, nowloc;
430 GetSystemTimeAsFileTime(&now);
431 FileTimeToLocalFileTime(&now, &nowloc);
433 tutc = (int64_t)(((((uint64_t) now.dwHighDateTime << 32) | now.dwLowDateTime) / 10000) - 11644473600000);
434 tloc = (int64_t)(((((uint64_t) nowloc.dwHighDateTime << 32) | nowloc.dwLowDateTime) / 10000) - 11644473600000);
438 struct tm now, nowloc;
440 clock_gettime(CLOCK_REALTIME, &tp);
441 gmtime_r(&tp.tv_sec, &now);
442 localtime_r(&tp.tv_sec, &nowloc);
444 tutc = (int64_t) now.tm_min * 60000;
445 tloc = (int64_t) nowloc.tm_min * 60000;
448 XsTime_utcToLocalValue = tloc - tutc;
449 XsTime_localToUtcValue = tutc - tloc;
461 return XsTime_utcToLocalValue;
470 return XsTime_localToUtcValue;