#include "config.h"
#include "CurrentTime.h"
#if OS(WINDOWS)
#undef WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <math.h>
#include <stdint.h>
#include <time.h>
#if USE(QUERY_PERFORMANCE_COUNTER)
#if OS(WINCE)
extern "C" time_t mktime(struct tm *t);
#else
#include <sys/timeb.h>
#include <sys/types.h>
#endif
#endif
#elif PLATFORM(GTK)
#include <glib.h>
#elif PLATFORM(WX)
#include <wx/datetime.h>
#elif PLATFORM(BREWMP)
#include <AEEStdLib.h>
#else
#include <sys/time.h>
#endif
#if PLATFORM(CHROMIUM)
#error Chromium uses a different timer implementation
#endif
namespace WTF {
const double msPerSecond = 1000.0;
#if OS(WINDOWS)
#if USE(QUERY_PERFORMANCE_COUNTER)
static LARGE_INTEGER qpcFrequency;
static bool syncedTime;
static double highResUpTime()
{
static LARGE_INTEGER qpcLast;
static DWORD tickCountLast;
static bool inited;
LARGE_INTEGER qpc;
QueryPerformanceCounter(&qpc);
DWORD tickCount = GetTickCount();
if (inited) {
__int64 qpcElapsed = ((qpc.QuadPart - qpcLast.QuadPart) * 1000) / qpcFrequency.QuadPart;
__int64 tickCountElapsed;
if (tickCount >= tickCountLast)
tickCountElapsed = (tickCount - tickCountLast);
else {
#if COMPILER(MINGW)
__int64 tickCountLarge = tickCount + 0x100000000ULL;
#else
__int64 tickCountLarge = tickCount + 0x100000000I64;
#endif
tickCountElapsed = tickCountLarge - tickCountLast;
}
__int64 diff = tickCountElapsed - qpcElapsed;
if (diff > 500 || diff < -500)
syncedTime = false;
} else
inited = true;
qpcLast = qpc;
tickCountLast = tickCount;
return (1000.0 * qpc.QuadPart) / static_cast<double>(qpcFrequency.QuadPart);
}
static double lowResUTCTime()
{
#if OS(WINCE)
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
struct tm tmtime;
tmtime.tm_year = systemTime.wYear - 1900;
tmtime.tm_mon = systemTime.wMonth - 1;
tmtime.tm_mday = systemTime.wDay;
tmtime.tm_wday = systemTime.wDayOfWeek;
tmtime.tm_hour = systemTime.wHour;
tmtime.tm_min = systemTime.wMinute;
tmtime.tm_sec = systemTime.wSecond;
time_t timet = mktime(&tmtime);
return timet * msPerSecond + systemTime.wMilliseconds;
#else
struct _timeb timebuffer;
_ftime(&timebuffer);
return timebuffer.time * msPerSecond + timebuffer.millitm;
#endif
}
static bool qpcAvailable()
{
static bool available;
static bool checked;
if (checked)
return available;
available = QueryPerformanceFrequency(&qpcFrequency);
checked = true;
return available;
}
double currentTime()
{
static double syncLowResUTCTime;
static double syncHighResUpTime;
static double lastUTCTime;
double lowResTime = lowResUTCTime();
if (!qpcAvailable())
return lowResTime / 1000.0;
double highResTime = highResUpTime();
if (!syncedTime) {
timeBeginPeriod(1); syncLowResUTCTime = lowResTime = lowResUTCTime();
timeEndPeriod(1); syncHighResUpTime = highResTime;
syncedTime = true;
}
double highResElapsed = highResTime - syncHighResUpTime;
double utc = syncLowResUTCTime + highResElapsed;
double lowResElapsed = lowResTime - syncLowResUTCTime;
const double maximumAllowedDriftMsec = 15.625 * 2.0; if (fabs(highResElapsed - lowResElapsed) > maximumAllowedDriftMsec)
syncedTime = false;
const double backwardTimeLimit = 2000.0;
if (utc < lastUTCTime && (lastUTCTime - utc) < backwardTimeLimit)
return lastUTCTime / 1000.0;
lastUTCTime = utc;
return utc / 1000.0;
}
#else
static double currentSystemTime()
{
FILETIME ft;
GetCurrentFT(&ft);
ULARGE_INTEGER t;
memcpy(&t, &ft, sizeof(t));
return t.QuadPart * 0.0000001 - 11644473600.0;
}
double currentTime()
{
static bool init = false;
static double lastTime;
static DWORD lastTickCount;
if (!init) {
lastTime = currentSystemTime();
lastTickCount = GetTickCount();
init = true;
return lastTime;
}
DWORD tickCountNow = GetTickCount();
DWORD elapsed = tickCountNow - lastTickCount;
double timeNow = lastTime + (double)elapsed / 1000.;
if (elapsed >= 0x7FFFFFFF) {
lastTime = timeNow;
lastTickCount = tickCountNow;
}
return timeNow;
}
#endif // USE(QUERY_PERFORMANCE_COUNTER)
#elif PLATFORM(GTK)
double currentTime()
{
GTimeVal now;
g_get_current_time(&now);
return static_cast<double>(now.tv_sec) + static_cast<double>(now.tv_usec / 1000000.0);
}
#elif PLATFORM(WX)
double currentTime()
{
wxDateTime now = wxDateTime::UNow();
return (double)now.GetTicks() + (double)(now.GetMillisecond() / 1000.0);
}
#elif PLATFORM(BREWMP)
double currentTime()
{
const unsigned diffSeconds = 315964800;
return static_cast<double>(diffSeconds + GETUTCSECONDS() + ((GETTIMEMS() % 1000) / msPerSecond));
}
#else
double currentTime()
{
struct timeval now;
gettimeofday(&now, 0);
return now.tv_sec + now.tv_usec / 1000000.0;
}
#endif
}