]> git.mxchange.org Git - flightgear.git/blob - src/Time/timestamp.hxx
Updates to material properties so --disable-textures now works, although
[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 "Include/compiler.h"
42 #ifdef FG_HAVE_STD_INCLUDES
43 #  include <ctime>
44 #else
45 #  include <time.h>
46 #endif
47
48 #ifdef HAVE_SYS_TIMEB_H
49 #  include <sys/timeb.h> // for ftime() and struct timeb
50 #endif
51 #ifdef HAVE_UNISTD_H
52 #  include <unistd.h>    // for gettimeofday()
53 #endif
54 #ifdef HAVE_SYS_TIME_H
55 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
56 #endif
57
58 // -dw- want to use metrowerks time.h
59 #ifdef MACOS
60 #  include <time.h>
61 #  include <timer.h>
62 #endif
63
64 #ifdef WIN32
65 #  include <windows.h>
66 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
67 #    define NEAR /* */
68 #    define FAR  /* */
69 #  endif
70 #  include <mmsystem.h>
71 #endif
72
73 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
74 class FGTimeStamp;
75 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
76 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
77
78 class FGTimeStamp {
79
80 private:
81
82     long seconds;
83     long usec;
84
85 public:
86
87     FGTimeStamp();
88     FGTimeStamp( const long s, const long m );
89     ~FGTimeStamp();
90
91     // Set time to current time
92     void stamp();
93
94     FGTimeStamp& operator = ( const FGTimeStamp& t );
95
96     friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
97     friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
98
99     inline long get_seconds() const { return seconds; }
100     // inline long get_usec() const { return usec; }
101 };
102
103 inline FGTimeStamp::FGTimeStamp() {
104 }
105
106 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
107     seconds = s;
108     usec = u;
109 }
110
111 inline FGTimeStamp::~FGTimeStamp() {
112 }
113
114 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
115 {
116     seconds = t.seconds;
117     usec = t.usec;
118     return *this;
119 }
120
121 inline void FGTimeStamp::stamp() {
122 #if defined( WIN32 )
123     unsigned int t;
124     t = timeGetTime();
125     seconds = 0;
126     usec =  t * 1000;
127 #elif defined( HAVE_GETTIMEOFDAY )
128     struct timeval current;
129     struct timezone tz;
130     // fg_timestamp currtime;
131     gettimeofday(&current, &tz);
132     seconds = current.tv_sec;
133     usec = current.tv_usec;
134 #elif defined( HAVE_GETLOCALTIME )
135     SYSTEMTIME current;
136     GetLocalTime(&current);
137     seconds = current.wSecond;
138     usec = current.wMilliseconds * 1000;
139 #elif defined( HAVE_FTIME )
140     struct timeb current;
141     ftime(&current);
142     seconds = current.time;
143     usec = current.millitm * 1000;
144 // -dw- uses time manager
145 #elif defined( MACOS )
146     UnsignedWide ms;
147     Microseconds(&ms);
148         
149     seconds = ms.lo / 1000000;
150     usec = ms.lo - ( seconds * 1000000 );
151 #else
152 # error Port me
153 #endif
154 }
155
156 // difference between time stamps in microseconds (usec)
157 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
158 #ifdef WIN32
159     return FGTimeStamp( 0, t.usec + m );
160 #else
161     return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
162                         ( t.usec + m ) % 1000000 );
163 #endif
164 }
165
166 // difference between time stamps in microseconds (usec)
167 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
168 {
169 #if defined( WIN32 )
170     return a.usec - b.usec;
171 #else
172     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
173 #endif
174 }
175
176
177 #endif // _TIMESTAMP_HXX
178
179