Mercurial > hg > octave-lyh
diff liboctave/oct-time.cc @ 4085:ee4790097033
[project @ 2002-10-03 16:04:56 by jwe]
author | jwe |
---|---|
date | Thu, 03 Oct 2002 16:04:56 +0000 |
parents | 7da18459c08b |
children | ea537559ab07 |
line wrap: on
line diff
--- a/liboctave/oct-time.cc +++ b/liboctave/oct-time.cc @@ -34,6 +34,10 @@ #include <unistd.h> #endif +#if defined (__WIN32__) && ! defined (HAVE_GETTIMEOFDAY) +#include <windows.h> +#endif + #include "lo-error.h" #include "lo-utils.h" #include "oct-time.h" @@ -87,8 +91,74 @@ #endif ot_unix_time = tp.tv_sec; + ot_usec = tp.tv_usec; - ot_usec = tp.tv_usec; +#elif defined (__WIN32__) + + // Loosely based on the code from Cygwin + // Copyright 1996-2002 Red Hat, Inc. + // Licenced under the GPL. + + const LONGLONG TIME_OFFSET = 0x19db1ded53e8000LL; + + static int init = 1; + static LARGE_INTEGER base; + static LARGE_INTEGER t0; + static double dt; + + if (init) + { + LARGE_INTEGER ifreq; + + if (QueryPerformanceFrequency (&ifreq)) + { + // Get clock frequency + dt = (double) 1000000.0 / (double) ifreq.QuadPart; + + // Get base time as microseconds from Jan 1. 1970 + int priority = GetThreadPriority (GetCurrentThread ()); + SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + if (QueryPerformanceCounter (&base)) + { + FILETIME f; + + GetSystemTimeAsFileTime (&f); + + t0.HighPart = f.dwHighDateTime; + t0.LowPart = f.dwLowDateTime; + t0.QuadPart -= TIME_OFFSET; + t0.QuadPart /= 10; + + init = 0; + } + + SetThreadPriority (GetCurrentThread (), priority); + } + + if (! init) + { + ot_unix_time = time (0); + ot_usec = 0; + + return; + } + } + + LARGE_INTEGER now; + + if (QueryPerformanceCounter (&now)) + { + now.QuadPart = (LONGLONG) (dt * (double)(now.QuadPart - base.QuadPart)); + now.QuadPart += t0.QuadPart; + + ot_unix_time = now.QuadPart / 1000000LL; + ot_usec = now.QuadPart % 1000000LL; + } + else + { + ot_unix_time = time (0); + ot_usec = 0; + } #else