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>
80 fgTIMESTAMP( const long s, const long m );
83 // Set time to current time
86 fgTIMESTAMP& operator = ( const fgTIMESTAMP& t );
88 friend fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m);
89 friend long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b);
91 inline long get_seconds() const { return seconds; }
92 inline long get_millis() const { return millis; }
95 inline fgTIMESTAMP::fgTIMESTAMP() {
98 inline fgTIMESTAMP::fgTIMESTAMP( const long s, const long m ) {
103 inline fgTIMESTAMP::~fgTIMESTAMP() {
106 inline fgTIMESTAMP& fgTIMESTAMP::operator = (const fgTIMESTAMP& t)
113 inline void fgTIMESTAMP::stamp() {
119 #elif defined( HAVE_GETTIMEOFDAY )
120 struct timeval current;
122 // fg_timestamp currtime;
123 gettimeofday(¤t, &tz);
124 seconds = current.tv_sec;
125 millis = current.tv_usec / 1000;
126 #elif defined( HAVE_GETLOCALTIME )
128 GetLocalTime(¤t);
129 seconds = current.wSecond;
130 millis = current.wMilliseconds;
131 #elif defined( HAVE_FTIME )
132 struct timeb current;
134 seconds = current.time;
135 millis = current.millitm;
141 // difference between time stamps in milliseconds
142 inline fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m) {
144 return fgTIMESTAMP( 0, t.millis + m );
146 return fgTIMESTAMP( t.seconds + ( t.millis + m ) / 1000,
147 ( t.millis + m ) % 1000 );
151 // difference between time stamps in milliseconds
152 inline long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b)
155 return a.millis - b.millis;
157 return 1000 * (a.seconds - b.seconds) + (a.millis - b.millis);
162 #endif // _TIMESTAMP_HXX
166 // Revision 1.3 1999/01/07 20:25:39 curt
167 // Portability changes and updates from Bernie Bright.
169 // Revision 1.2 1998/12/11 20:26:56 curt
172 // Revision 1.1 1998/12/05 14:21:33 curt
173 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
174 // to it's own file, timestamp.hxx.