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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # include <simgear_config.h>
35 #include <simgear/compiler.h>
39 #ifdef HAVE_SYS_TIMEB_H
40 # include <sys/timeb.h> // for ftime() and struct timeb
43 # include <unistd.h> // for gettimeofday()
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
51 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
55 # include <mmsystem.h>
58 #include "timestamp.hxx"
61 void SGTimeStamp::stamp() {
62 #if defined( WIN32 ) && !defined(__CYGWIN__)
66 usec = ( t - ( seconds * 1000 ) ) * 1000;
67 #elif defined( HAVE_GETTIMEOFDAY )
68 struct timeval current;
70 // sg_timestamp currtime;
71 gettimeofday(¤t, &tz);
72 seconds = current.tv_sec;
73 usec = current.tv_usec;
74 #elif defined( HAVE_GETLOCALTIME )
76 GetLocalTime(¤t);
77 seconds = current.wSecond;
78 usec = current.wMilliseconds * 1000;
79 #elif defined( HAVE_FTIME )
82 seconds = current.time;
83 usec = current.millitm * 1000;
89 // increment the time stamp by the number of microseconds (usec)
90 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
91 return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
92 ( t.usec + m ) % 1000000 );
95 // difference between time stamps in microseconds (usec)
96 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
98 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);