1 // timestamp.hxx -- class for managing a timestamp (seconds & milliseconds.)
3 // Written by Curtis Olson, started December 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifndef _TIMESTAMP_HXX
25 #define _TIMESTAMP_HXX
29 # error This library requires C++
41 #include "Include/compiler.h"
42 #ifdef FG_HAVE_STD_INCLUDES
48 #ifdef HAVE_SYS_TIMEB_H
49 # include <sys/timeb.h> // for ftime() and struct timeb
52 # include <unistd.h> // for gettimeofday()
54 #ifdef HAVE_SYS_TIME_H
55 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
58 // -dw- want to use metrowerks time.h
66 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
70 # include <mmsystem.h>
73 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
75 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
76 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
88 FGTimeStamp( const long s, const long m );
91 // Set time to current time
94 FGTimeStamp& operator = ( const FGTimeStamp& t );
96 friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
97 friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
99 inline long get_seconds() const { return seconds; }
100 // inline long get_usec() const { return usec; }
103 inline FGTimeStamp::FGTimeStamp() {
106 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
111 inline FGTimeStamp::~FGTimeStamp() {
114 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
121 inline void FGTimeStamp::stamp() {
127 #elif defined( HAVE_GETTIMEOFDAY )
128 struct timeval current;
130 // fg_timestamp currtime;
131 gettimeofday(¤t, &tz);
132 seconds = current.tv_sec;
133 usec = current.tv_usec;
134 #elif defined( HAVE_GETLOCALTIME )
136 GetLocalTime(¤t);
137 seconds = current.wSecond;
138 usec = current.wMilliseconds * 1000;
139 #elif defined( HAVE_FTIME )
140 struct timeb current;
142 seconds = current.time;
143 usec = current.millitm * 1000;
144 // -dw- uses time manager
145 #elif defined( MACOS )
149 seconds = ms.lo / 1000000;
150 usec = ms.lo - ( seconds * 1000000 );
156 // difference between time stamps in microseconds (usec)
157 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
159 return FGTimeStamp( 0, t.usec + m );
161 return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
162 ( t.usec + m ) % 1000000 );
166 // difference between time stamps in microseconds (usec)
167 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
170 return a.usec - b.usec;
172 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
177 #endif // _TIMESTAMP_HXX