X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Ftiming%2Ftimestamp.cxx;h=83eff2fc10ea485c8039634132b958f1ac866cef;hb=b47d1ad5fd8ed111cae99c1f65f5bb65a5371501;hp=7c8cffa9f9662b29993ec3d99e0070932bebe987;hpb=e2b4c0787a4398199ef2d35e05963acd58b7a04c;p=simgear.git diff --git a/simgear/timing/timestamp.cxx b/simgear/timing/timestamp.cxx index 7c8cffa9..83eff2fc 100644 --- a/simgear/timing/timestamp.cxx +++ b/simgear/timing/timestamp.cxx @@ -5,7 +5,7 @@ // Written by Curtis Olson, started December 1998. // -// Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org +// Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -19,7 +19,7 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -28,32 +28,23 @@ # include #endif -#ifdef HAVE_WINDOWS_H -# include -#endif - #include -#ifdef SG_HAVE_STD_INCLUDES -# include -#else -# include -#endif +#include #ifdef HAVE_SYS_TIMEB_H # include // for ftime() and struct timeb #endif #ifdef HAVE_UNISTD_H -# include // for gettimeofday() +# include // for gettimeofday() and the _POSIX_TIMERS define #endif #ifdef HAVE_SYS_TIME_H # include // for get/setitimer, gettimeofday, struct timeval #endif -// -dw- want to use metrowerks time.h -#ifdef macintosh +#if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) # include -# include +# include #endif #ifdef WIN32 @@ -67,58 +58,56 @@ #include "timestamp.hxx" - void SGTimeStamp::stamp() { -#if defined( WIN32 ) +#ifdef _WIN32 unsigned int t; t = timeGetTime(); - seconds = 0; - usec = t * 1000; + _sec = t / 1000; + _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000; +#elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) + struct timespec ts; +#if defined(_POSIX_MONOTONIC_CLOCK) + static clockid_t clockid = CLOCK_MONOTONIC; + static bool firstTime = true; + if (firstTime) { + firstTime = false; + // For the first time test if the monotonic clock is available. + // If so use this if not use the realtime clock. + if (-1 == clock_gettime(clockid, &ts) && errno == EINVAL) + clockid = CLOCK_REALTIME; + } + clock_gettime(clockid, &ts); +#else + clock_gettime(CLOCK_REALTIME, &ts); +#endif + _sec = ts.tv_sec; + _nsec = ts.tv_nsec; #elif defined( HAVE_GETTIMEOFDAY ) struct timeval current; struct timezone tz; // sg_timestamp currtime; gettimeofday(¤t, &tz); - seconds = current.tv_sec; - usec = current.tv_usec; + _sec = current.tv_sec; + _nsec = current.tv_usec * 1000; #elif defined( HAVE_GETLOCALTIME ) SYSTEMTIME current; GetLocalTime(¤t); - seconds = current.wSecond; - usec = current.wMilliseconds * 1000; + _sec = current.wSecond; + _nsec = current.wMilliseconds * 1000 * 1000; #elif defined( HAVE_FTIME ) struct timeb current; ftime(¤t); - seconds = current.time; - usec = current.millitm * 1000; -// -dw- uses time manager -#elif defined( macintosh ) - UnsignedWide ms; - Microseconds(&ms); - - seconds = ms.lo / 1000000; - usec = ms.lo - ( seconds * 1000000 ); + _sec = current.time; + _nsec = current.millitm * 1000 * 1000; #else # error Port me #endif } -// increment the time stamp by the number of microseconds (usec) -SGTimeStamp operator + (const SGTimeStamp& t, const long& m) { -#ifdef WIN32 - return SGTimeStamp( 0, t.usec + m ); -#else - return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000, - ( t.usec + m ) % 1000000 ); -#endif -} - -// difference between time stamps in microseconds (usec) -long operator - (const SGTimeStamp& a, const SGTimeStamp& b) +int SGTimeStamp::elapsedMSec() const { -#if defined( WIN32 ) - return a.usec - b.usec; -#else - return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec); -#endif + SGTimeStamp now; + now.stamp(); + + return static_cast((now - *this).toMSecs()); }