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>
31 #include <simgear/compiler.h>
35 #ifdef HAVE_SYS_TIMEB_H
36 # include <sys/timeb.h> // for ftime() and struct timeb
39 # include <unistd.h> // for gettimeofday()
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
47 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
51 # include <mmsystem.h>
54 #include "timestamp.hxx"
57 void SGTimeStamp::stamp() {
58 #if defined( WIN32 ) && !defined(__CYGWIN__)
62 usec = ( t - ( seconds * 1000 ) ) * 1000;
63 #elif defined( HAVE_GETTIMEOFDAY )
64 struct timeval current;
66 // sg_timestamp currtime;
67 gettimeofday(¤t, &tz);
68 seconds = current.tv_sec;
69 usec = current.tv_usec;
70 #elif defined( HAVE_GETLOCALTIME )
72 GetLocalTime(¤t);
73 seconds = current.wSecond;
74 usec = current.wMilliseconds * 1000;
75 #elif defined( HAVE_FTIME )
78 seconds = current.time;
79 usec = current.millitm * 1000;
85 // increment the time stamp by the number of microseconds (usec)
86 SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
87 return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
88 ( t.usec + m ) % 1000000 );
91 // difference between time stamps in microseconds (usec)
92 long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
94 return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);