3 * Provides a class for managing a timestamp (seconds & milliseconds.)
6 // Written by Curtis Olson, started December 1998.
8 // Copyright (C) 1998 Curtis L. Olson - curt@flightgear.org
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #ifndef _TIMESTAMP_HXX
28 #define _TIMESTAMP_HXX
32 # error This library requires C++
44 #include <simgear/compiler.h>
46 #ifdef SG_HAVE_STD_INCLUDES
52 #ifdef HAVE_SYS_TIMEB_H
53 # include <sys/timeb.h> // for ftime() and struct timeb
56 # include <unistd.h> // for gettimeofday()
58 #ifdef HAVE_SYS_TIME_H
59 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
62 // -dw- want to use metrowerks time.h
70 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
74 # include <mmsystem.h>
77 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
79 SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
80 long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
83 * The SGTimeStamp class allows you to mark and compare time stamps
84 * with microsecond accuracy (if your system has support for this
87 * The SGTimeStamp is useful for tracking the elapsed time of various
88 * events in your program. You can also use it to keep constistant
89 * motion across varying frame rates.
101 /** Default constructor */
105 * This creates an instance of the SGTimeStamp object. When
106 * calling the constructor you may provide initial seconds an
107 * microseconds values.
108 * @param s initial seconds value
109 * @param m initial microseconds value
111 SGTimeStamp( const long s, const long m );
114 /** Update stored time to current time (seconds and microseconds) */
117 /** Compare two time stamps for equality */
118 SGTimeStamp& operator = ( const SGTimeStamp& t );
121 * Increment the saved time by the specified number of microseconds
122 * @param t time stamp
123 * @param m microseconds increment
124 * @return new time stamp
126 friend SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
129 * Subtract two time stamps returning the difference in microseconds.
130 * @param a timestamp 1
131 * @param b timestame 2
132 * @return difference in microseconds
134 friend long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
136 /** @return the saved seconds of this time stamp */
137 inline long get_seconds() const { return seconds; }
139 /** @return the saved microseconds of this time stamp */
140 inline long get_usec() const { return usec; }
143 inline SGTimeStamp::SGTimeStamp() {
146 inline SGTimeStamp::SGTimeStamp( const long s, const long u ) {
151 inline SGTimeStamp::~SGTimeStamp() {
154 inline SGTimeStamp& SGTimeStamp::operator = (const SGTimeStamp& t)
161 inline void SGTimeStamp::stamp() {
167 #elif defined( HAVE_GETTIMEOFDAY )
168 struct timeval current;
170 // sg_timestamp currtime;
171 gettimeofday(¤t, &tz);
172 seconds = current.tv_sec;
173 usec = current.tv_usec;
174 #elif defined( HAVE_GETLOCALTIME )
176 GetLocalTime(¤t);
177 seconds = current.wSecond;
178 usec = current.wMilliseconds * 1000;
179 #elif defined( HAVE_FTIME )
180 struct timeb current;
182 seconds = current.time;
183 usec = current.millitm * 1000;
184 // -dw- uses time manager
185 #elif defined( macintosh )
189 seconds = ms.lo / 1000000;
190 usec = ms.lo - ( seconds * 1000000 );
196 // increment the time stamp by the number of microseconds (usec)
197 inline SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
199 return SGTimeStamp( 0, t.usec + m );
201 return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
202 ( t.usec + m ) % 1000000 );
206 // difference between time stamps in microseconds (usec)
207 inline long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
210 return a.usec - b.usec;
212 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
217 #endif // _TIMESTAMP_HXX