1998-12-05 14:21:28 +00:00
|
|
|
//
|
|
|
|
// 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 <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
#include "Include/compiler.h"
|
|
|
|
#ifdef FG_HAVE_STD_INCLUDES
|
|
|
|
# include <ctime>
|
|
|
|
#else
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
1998-12-05 14:21:28 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIMEB_H
|
|
|
|
# include <sys/timeb.h> // for ftime() and struct timeb
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h> // for gettimeofday()
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
|
|
|
|
#endif
|
|
|
|
|
1998-12-11 20:26:54 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
# if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
|
|
|
|
# define NEAR /* */
|
|
|
|
# define FAR /* */
|
|
|
|
# endif
|
|
|
|
# include <mmsystem.h>
|
|
|
|
#endif
|
|
|
|
|
1999-02-02 20:13:29 +00:00
|
|
|
// MSVC++ 6.0 kuldge - Need forward declaration of friends.
|
|
|
|
class FGTimeStamp;
|
|
|
|
FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
|
|
|
|
long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
|
1998-12-05 14:21:28 +00:00
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
class FGTimeStamp {
|
1998-12-05 14:21:28 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
long seconds;
|
1999-01-09 13:37:32 +00:00
|
|
|
long usec;
|
1998-12-05 14:21:28 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
FGTimeStamp();
|
|
|
|
FGTimeStamp( const long s, const long m );
|
|
|
|
~FGTimeStamp();
|
1998-12-05 14:21:28 +00:00
|
|
|
|
|
|
|
// Set time to current time
|
|
|
|
void stamp();
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
FGTimeStamp& operator = ( const FGTimeStamp& t );
|
1998-12-05 14:21:28 +00:00
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
|
|
|
|
friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
|
1998-12-05 14:21:28 +00:00
|
|
|
|
|
|
|
inline long get_seconds() const { return seconds; }
|
1999-01-09 13:37:32 +00:00
|
|
|
// inline long get_usec() const { return usec; }
|
1998-12-05 14:21:28 +00:00
|
|
|
};
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
inline FGTimeStamp::FGTimeStamp() {
|
1998-12-05 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
|
1998-12-05 14:21:28 +00:00
|
|
|
seconds = s;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = u;
|
1998-12-05 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
inline FGTimeStamp::~FGTimeStamp() {
|
1998-12-05 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
|
1998-12-05 14:21:28 +00:00
|
|
|
{
|
|
|
|
seconds = t.seconds;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = t.usec;
|
1998-12-05 14:21:28 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
inline void FGTimeStamp::stamp() {
|
1998-12-05 14:21:28 +00:00
|
|
|
#if defined( WIN32 )
|
|
|
|
unsigned int t;
|
|
|
|
t = timeGetTime();
|
|
|
|
seconds = 0;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = t * 1000;
|
1998-12-05 14:21:28 +00:00
|
|
|
#elif defined( HAVE_GETTIMEOFDAY )
|
|
|
|
struct timeval current;
|
|
|
|
struct timezone tz;
|
|
|
|
// fg_timestamp currtime;
|
|
|
|
gettimeofday(¤t, &tz);
|
|
|
|
seconds = current.tv_sec;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = current.tv_usec;
|
1998-12-05 14:21:28 +00:00
|
|
|
#elif defined( HAVE_GETLOCALTIME )
|
|
|
|
SYSTEMTIME current;
|
|
|
|
GetLocalTime(¤t);
|
|
|
|
seconds = current.wSecond;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = current.wMilliseconds * 1000;
|
1998-12-05 14:21:28 +00:00
|
|
|
#elif defined( HAVE_FTIME )
|
|
|
|
struct timeb current;
|
|
|
|
ftime(¤t);
|
|
|
|
seconds = current.time;
|
1999-01-09 13:37:32 +00:00
|
|
|
usec = current.millitm * 1000;
|
1998-12-05 14:21:28 +00:00
|
|
|
#else
|
|
|
|
# error Port me
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
// difference between time stamps in microseconds (usec)
|
|
|
|
inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
|
1998-12-05 14:21:28 +00:00
|
|
|
#ifdef WIN32
|
1999-01-09 13:37:32 +00:00
|
|
|
return FGTimeStamp( 0, t.usec + m );
|
1998-12-05 14:21:28 +00:00
|
|
|
#else
|
1999-01-09 13:37:32 +00:00
|
|
|
return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
|
|
|
|
( t.usec + m ) % 1000000 );
|
1998-12-05 14:21:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-01-09 13:37:32 +00:00
|
|
|
// difference between time stamps in microseconds (usec)
|
|
|
|
inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
|
1998-12-05 14:21:28 +00:00
|
|
|
{
|
|
|
|
#if defined( WIN32 )
|
1999-01-09 13:37:32 +00:00
|
|
|
return a.usec - b.usec;
|
1998-12-05 14:21:28 +00:00
|
|
|
#else
|
1999-01-09 13:37:32 +00:00
|
|
|
return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
|
1998-12-05 14:21:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _TIMESTAMP_HXX
|
|
|
|
|
|
|
|
|