2 // timestamp.hxx -- class for managing a timestamp (seconds & milliseconds.)
4 // Written by Curtis Olson, started December 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 // (Log is kept at end of this file)
26 #ifndef _TIMESTAMP_HXX
27 #define _TIMESTAMP_HXX
31 # error This library requires C++
43 #include "Include/compiler.h"
44 #ifdef FG_HAVE_STD_INCLUDES
50 #ifdef HAVE_SYS_TIMEB_H
51 # include <sys/timeb.h> // for ftime() and struct timeb
54 # include <unistd.h> // for gettimeofday()
56 #ifdef HAVE_SYS_TIME_H
57 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
62 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
66 # include <mmsystem.h>
69 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
71 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
72 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
84 FGTimeStamp( const long s, const long m );
87 // Set time to current time
90 FGTimeStamp& operator = ( const FGTimeStamp& t );
92 friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
93 friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
95 inline long get_seconds() const { return seconds; }
96 // inline long get_usec() const { return usec; }
99 inline FGTimeStamp::FGTimeStamp() {
102 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
107 inline FGTimeStamp::~FGTimeStamp() {
110 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
117 inline void FGTimeStamp::stamp() {
123 #elif defined( HAVE_GETTIMEOFDAY )
124 struct timeval current;
126 // fg_timestamp currtime;
127 gettimeofday(¤t, &tz);
128 seconds = current.tv_sec;
129 usec = current.tv_usec;
130 #elif defined( HAVE_GETLOCALTIME )
132 GetLocalTime(¤t);
133 seconds = current.wSecond;
134 usec = current.wMilliseconds * 1000;
135 #elif defined( HAVE_FTIME )
136 struct timeb current;
138 seconds = current.time;
139 usec = current.millitm * 1000;
145 // difference between time stamps in microseconds (usec)
146 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
148 return FGTimeStamp( 0, t.usec + m );
150 return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
151 ( t.usec + m ) % 1000000 );
155 // difference between time stamps in microseconds (usec)
156 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
159 return a.usec - b.usec;
161 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
166 #endif // _TIMESTAMP_HXX
170 // Revision 1.5 1999/02/02 20:13:43 curt
171 // MSVC++ portability changes by Bernie Bright:
173 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
174 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
175 // Simulator/Cockpit/hud.cxx: Added Standard headers
176 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
177 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG. Deleted <stdio.h>
178 // Simulator/Main/fg_init.cxx:
179 // Simulator/Main/GLUTmain.cxx:
180 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
181 // Simulator/Objects/material.hxx:
182 // Simulator/Time/timestamp.hxx: VC++ friend kludge
183 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
184 // Simulator/Main/views.hxx: Added a constant
186 // Revision 1.4 1999/01/09 13:37:46 curt
187 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
189 // Revision 1.3 1999/01/07 20:25:39 curt
190 // Portability changes and updates from Bernie Bright.
192 // Revision 1.2 1998/12/11 20:26:56 curt
195 // Revision 1.1 1998/12/05 14:21:33 curt
196 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
197 // to it's own file, timestamp.hxx.