From: curt Date: Thu, 30 Nov 2000 17:38:23 +0000 (+0000) Subject: Initial revision. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=806083cb2558696ab2ab37d8bd939772705b4ec6;p=simgear.git Initial revision. --- diff --git a/simgear/timing/Makefile.am b/simgear/timing/Makefile.am index 7e6f68f1..9bd6bfff 100644 --- a/simgear/timing/Makefile.am +++ b/simgear/timing/Makefile.am @@ -6,6 +6,7 @@ include_HEADERS = \ geocoord.h \ lowleveltime.h \ sg_time.hxx \ + timestamp.hxx \ timezone.h libsgtiming_a_SOURCES = \ diff --git a/simgear/timing/timestamp.hxx b/simgear/timing/timestamp.hxx new file mode 100644 index 00000000..03efa8db --- /dev/null +++ b/simgear/timing/timestamp.hxx @@ -0,0 +1,180 @@ +// timestamp.hxx -- class for managing a timestamp (seconds & milliseconds.) +// +// Written by Curtis Olson, started December 1998. +// +// Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// 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. +// +// $Id$ + + +#ifndef _TIMESTAMP_HXX +#define _TIMESTAMP_HXX + + +#ifndef __cplusplus +# error This library requires C++ +#endif + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef HAVE_WINDOWS_H +# include +#endif + +#include + +#ifdef FG_HAVE_STD_INCLUDES +# include +#else +# include +#endif + +#ifdef HAVE_SYS_TIMEB_H +# include // for ftime() and struct timeb +#endif +#ifdef HAVE_UNISTD_H +# include // for gettimeofday() +#endif +#ifdef HAVE_SYS_TIME_H +# include // for get/setitimer, gettimeofday, struct timeval +#endif + +// -dw- want to use metrowerks time.h +#ifdef macintosh +# include +# include +#endif + +#ifdef WIN32 +# include +# if defined( __CYGWIN__ ) || defined( __CYGWIN32__ ) +# define NEAR /* */ +# define FAR /* */ +# endif +# include +#endif + +// MSVC++ 6.0 kuldge - Need forward declaration of friends. +class SGTimeStamp; +SGTimeStamp operator + (const SGTimeStamp& t, const long& m); +long operator - (const SGTimeStamp& a, const SGTimeStamp& b); + +class SGTimeStamp { + +private: + + long seconds; + long usec; + +public: + + SGTimeStamp(); + SGTimeStamp( const long s, const long m ); + ~SGTimeStamp(); + + // Set time to current time + void stamp(); + + SGTimeStamp& operator = ( const SGTimeStamp& t ); + + friend SGTimeStamp operator + (const SGTimeStamp& t, const long& m); + friend long operator - (const SGTimeStamp& a, const SGTimeStamp& b); + + inline long get_seconds() const { return seconds; } + // inline long get_usec() const { return usec; } +}; + +inline SGTimeStamp::SGTimeStamp() { +} + +inline SGTimeStamp::SGTimeStamp( const long s, const long u ) { + seconds = s; + usec = u; +} + +inline SGTimeStamp::~SGTimeStamp() { +} + +inline SGTimeStamp& SGTimeStamp::operator = (const SGTimeStamp& t) +{ + seconds = t.seconds; + usec = t.usec; + return *this; +} + +inline void SGTimeStamp::stamp() { +#if defined( WIN32 ) + unsigned int t; + t = timeGetTime(); + seconds = 0; + usec = t * 1000; +#elif defined( HAVE_GETTIMEOFDAY ) + struct timeval current; + struct timezone tz; + // sg_timestamp currtime; + gettimeofday(¤t, &tz); + seconds = current.tv_sec; + usec = current.tv_usec; +#elif defined( HAVE_GETLOCALTIME ) + SYSTEMTIME current; + GetLocalTime(¤t); + seconds = current.wSecond; + usec = current.wMilliseconds * 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 ); +#else +# error Port me +#endif +} + +// increment the time stamp by the number of microseconds (usec) +inline 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) +inline long operator - (const SGTimeStamp& a, const SGTimeStamp& b) +{ +#if defined( WIN32 ) + return a.usec - b.usec; +#else + return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec); +#endif +} + + +#endif // _TIMESTAMP_HXX + +