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 - http://www.flightgear.org/~curt
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.
28 # include <simgear_config.h>
35 #include <simgear/compiler.h>
37 #ifdef SG_HAVE_STD_INCLUDES
43 #ifdef HAVE_SYS_TIMEB_H
44 # include <sys/timeb.h> // for ftime() and struct timeb
47 # include <unistd.h> // for gettimeofday()
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
53 // -dw- want to use metrowerks time.h
61 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
65 # include <mmsystem.h>
68 #include "timestamp.hxx"
71 void SGTimeStamp::stamp() {
72 #if defined( WIN32 ) && !defined(__CYGWIN__)
77 #elif defined( HAVE_GETTIMEOFDAY )
78 struct timeval current;
80 // sg_timestamp currtime;
81 gettimeofday(¤t, &tz);
82 seconds = current.tv_sec;
83 usec = current.tv_usec;
84 #elif defined( HAVE_GETLOCALTIME )
86 GetLocalTime(¤t);
87 seconds = current.wSecond;
88 usec = current.wMilliseconds * 1000;
89 #elif defined( HAVE_FTIME )
92 seconds = current.time;
93 usec = current.millitm * 1000;
94 // -dw- uses time manager
95 #elif defined( macintosh )
99 seconds = ms.lo / 1000000;
100 usec = ms.lo - ( seconds * 1000000 );
106 // increment the time stamp by the number of microseconds (usec)
107 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
108 #if defined( WIN32 ) && !defined(__CYGWIN__)
109 return SGTimeStamp( 0, t.usec + m );
111 return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
112 ( t.usec + m ) % 1000000 );
116 // difference between time stamps in microseconds (usec)
117 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
119 #if defined( WIN32 ) && !defined(__CYGWIN__)
120 return a.usec - b.usec;
122 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);