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 <simgear/compiler.h>
43 #ifdef SG_HAVE_STD_INCLUDES
49 #ifdef HAVE_SYS_TIMEB_H
50 # include <sys/timeb.h> // for ftime() and struct timeb
53 # include <unistd.h> // for gettimeofday()
55 #ifdef HAVE_SYS_TIME_H
56 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
59 // -dw- want to use metrowerks time.h
67 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
71 # include <mmsystem.h>
74 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
76 SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
77 long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
89 SGTimeStamp( const long s, const long m );
92 // Set time to current time
95 SGTimeStamp& operator = ( const SGTimeStamp& t );
97 friend SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
98 friend long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
100 inline long get_seconds() const { return seconds; }
101 // inline long get_usec() const { return usec; }
104 inline SGTimeStamp::SGTimeStamp() {
107 inline SGTimeStamp::SGTimeStamp( const long s, const long u ) {
112 inline SGTimeStamp::~SGTimeStamp() {
115 inline SGTimeStamp& SGTimeStamp::operator = (const SGTimeStamp& t)
122 inline void SGTimeStamp::stamp() {
128 #elif defined( HAVE_GETTIMEOFDAY )
129 struct timeval current;
131 // sg_timestamp currtime;
132 gettimeofday(¤t, &tz);
133 seconds = current.tv_sec;
134 usec = current.tv_usec;
135 #elif defined( HAVE_GETLOCALTIME )
137 GetLocalTime(¤t);
138 seconds = current.wSecond;
139 usec = current.wMilliseconds * 1000;
140 #elif defined( HAVE_FTIME )
141 struct timeb current;
143 seconds = current.time;
144 usec = current.millitm * 1000;
145 // -dw- uses time manager
146 #elif defined( macintosh )
150 seconds = ms.lo / 1000000;
151 usec = ms.lo - ( seconds * 1000000 );
157 // increment the time stamp by the number of microseconds (usec)
158 inline SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
160 return SGTimeStamp( 0, t.usec + m );
162 return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
163 ( t.usec + m ) % 1000000 );
167 // difference between time stamps in microseconds (usec)
168 inline long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
171 return a.usec - b.usec;
173 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
178 #endif // _TIMESTAMP_HXX