]> git.mxchange.org Git - flightgear.git/blob - src/Time/timestamp.hxx
Replaced gdbm with metakit. Involves a new simgear version and a new database
[flightgear.git] / src / Time / timestamp.hxx
1 // timestamp.hxx -- class for managing a timestamp (seconds & milliseconds.)
2 //
3 // Written by Curtis Olson, started December 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifndef _TIMESTAMP_HXX
25 #define _TIMESTAMP_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <simgear/compiler.h>
42
43 #ifdef FG_HAVE_STD_INCLUDES
44 #  include <ctime>
45 #else
46 #  include <time.h>
47 #endif
48
49 #ifdef HAVE_SYS_TIMEB_H
50 #  include <sys/timeb.h> // for ftime() and struct timeb
51 #endif
52 #ifdef HAVE_UNISTD_H
53 #  include <unistd.h>    // for gettimeofday()
54 #endif
55 #ifdef HAVE_SYS_TIME_H
56 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
57 #endif
58
59 // -dw- want to use metrowerks time.h
60 #ifdef MACOS
61 #  include <time.h>
62 #  include <timer.h>
63 #endif
64
65 #ifdef WIN32
66 #  include <windows.h>
67 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
68 #    define NEAR /* */
69 #    define FAR  /* */
70 #  endif
71 #  include <mmsystem.h>
72 #endif
73
74 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
75 class FGTimeStamp;
76 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
77 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
78
79 class FGTimeStamp {
80
81 private:
82
83     long seconds;
84     long usec;
85
86 public:
87
88     FGTimeStamp();
89     FGTimeStamp( const long s, const long m );
90     ~FGTimeStamp();
91
92     // Set time to current time
93     void stamp();
94
95     FGTimeStamp& operator = ( const FGTimeStamp& t );
96
97     friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
98     friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
99
100     inline long get_seconds() const { return seconds; }
101     // inline long get_usec() const { return usec; }
102 };
103
104 inline FGTimeStamp::FGTimeStamp() {
105 }
106
107 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
108     seconds = s;
109     usec = u;
110 }
111
112 inline FGTimeStamp::~FGTimeStamp() {
113 }
114
115 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
116 {
117     seconds = t.seconds;
118     usec = t.usec;
119     return *this;
120 }
121
122 inline void FGTimeStamp::stamp() {
123 #if defined( WIN32 )
124     unsigned int t;
125     t = timeGetTime();
126     seconds = 0;
127     usec =  t * 1000;
128 #elif defined( HAVE_GETTIMEOFDAY )
129     struct timeval current;
130     struct timezone tz;
131     // fg_timestamp currtime;
132     gettimeofday(&current, &tz);
133     seconds = current.tv_sec;
134     usec = current.tv_usec;
135 #elif defined( HAVE_GETLOCALTIME )
136     SYSTEMTIME current;
137     GetLocalTime(&current);
138     seconds = current.wSecond;
139     usec = current.wMilliseconds * 1000;
140 #elif defined( HAVE_FTIME )
141     struct timeb current;
142     ftime(&current);
143     seconds = current.time;
144     usec = current.millitm * 1000;
145 // -dw- uses time manager
146 #elif defined( MACOS )
147     UnsignedWide ms;
148     Microseconds(&ms);
149         
150     seconds = ms.lo / 1000000;
151     usec = ms.lo - ( seconds * 1000000 );
152 #else
153 # error Port me
154 #endif
155 }
156
157 // increment the time stamp by the number of microseconds (usec)
158 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
159 #ifdef WIN32
160     return FGTimeStamp( 0, t.usec + m );
161 #else
162     return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
163                         ( t.usec + m ) % 1000000 );
164 #endif
165 }
166
167 // difference between time stamps in microseconds (usec)
168 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
169 {
170 #if defined( WIN32 )
171     return a.usec - b.usec;
172 #else
173     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
174 #endif
175 }
176
177
178 #endif // _TIMESTAMP_HXX
179
180