]> git.mxchange.org Git - flightgear.git/blob - Simulator/Time/timestamp.hxx
Merge Include as subdirectory
[flightgear.git] / Simulator / Time / timestamp.hxx
1 //
2 // timestamp.hxx -- class for managing a timestamp (seconds & milliseconds.)
3 //
4 // Written by Curtis Olson, started December 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #ifndef _TIMESTAMP_HXX
27 #define _TIMESTAMP_HXX
28
29
30 #ifndef __cplusplus                                                          
31 # error This library requires C++
32 #endif                                   
33
34
35 #ifdef HAVE_CONFIG_H
36 #  include <config.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include "Include/compiler.h"
44 #ifdef FG_HAVE_STD_INCLUDES
45 #  include <ctime>
46 #else
47 #  include <time.h>
48 #endif
49
50 #ifdef HAVE_SYS_TIMEB_H
51 #  include <sys/timeb.h> // for ftime() and struct timeb
52 #endif
53 #ifdef HAVE_UNISTD_H
54 #  include <unistd.h>    // for gettimeofday()
55 #endif
56 #ifdef HAVE_SYS_TIME_H
57 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
58 #endif
59
60 #ifdef  WIN32
61 #  include <windows.h>
62 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
63 #    define NEAR /* */
64 #    define FAR  /* */
65 #  endif
66 #  include <mmsystem.h>
67 #endif
68
69 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
70 class FGTimeStamp;
71 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
72 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
73
74 class FGTimeStamp {
75
76 private:
77
78     long seconds;
79     long usec;
80
81 public:
82
83     FGTimeStamp();
84     FGTimeStamp( const long s, const long m );
85     ~FGTimeStamp();
86
87     // Set time to current time
88     void stamp();
89
90     FGTimeStamp& operator = ( const FGTimeStamp& t );
91
92     friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
93     friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
94
95     inline long get_seconds() const { return seconds; }
96     // inline long get_usec() const { return usec; }
97 };
98
99 inline FGTimeStamp::FGTimeStamp() {
100 }
101
102 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
103     seconds = s;
104     usec = u;
105 }
106
107 inline FGTimeStamp::~FGTimeStamp() {
108 }
109
110 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
111 {
112     seconds = t.seconds;
113     usec = t.usec;
114     return *this;
115 }
116
117 inline void FGTimeStamp::stamp() {
118 #if defined( WIN32 )
119     unsigned int t;
120     t = timeGetTime();
121     seconds = 0;
122     usec =  t * 1000;
123 #elif defined( HAVE_GETTIMEOFDAY )
124     struct timeval current;
125     struct timezone tz;
126     // fg_timestamp currtime;
127     gettimeofday(&current, &tz);
128     seconds = current.tv_sec;
129     usec = current.tv_usec;
130 #elif defined( HAVE_GETLOCALTIME )
131     SYSTEMTIME current;
132     GetLocalTime(&current);
133     seconds = current.wSecond;
134     usec = current.wMilliseconds * 1000;
135 #elif defined( HAVE_FTIME )
136     struct timeb current;
137     ftime(&current);
138     seconds = current.time;
139     usec = current.millitm * 1000;
140 #else
141 # error Port me
142 #endif
143 }
144
145 // difference between time stamps in microseconds (usec)
146 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
147 #ifdef WIN32
148     return FGTimeStamp( 0, t.usec + m );
149 #else
150     return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
151                         ( t.usec + m ) % 1000000 );
152 #endif
153 }
154
155 // difference between time stamps in microseconds (usec)
156 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
157 {
158 #if defined( WIN32 )
159     return a.usec - b.usec;
160 #else
161     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
162 #endif
163 }
164
165
166 #endif // _TIMESTAMP_HXX
167
168
169 // $Log$
170 // Revision 1.5  1999/02/02 20:13:43  curt
171 // MSVC++ portability changes by Bernie Bright:
172 //
173 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
174 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
175 // Simulator/Cockpit/hud.cxx: Added Standard headers
176 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
177 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG.  Deleted <stdio.h>
178 // Simulator/Main/fg_init.cxx:
179 // Simulator/Main/GLUTmain.cxx:
180 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
181 // Simulator/Objects/material.hxx:
182 // Simulator/Time/timestamp.hxx: VC++ friend kludge
183 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
184 // Simulator/Main/views.hxx: Added a constant
185 //
186 // Revision 1.4  1999/01/09 13:37:46  curt
187 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
188 //
189 // Revision 1.3  1999/01/07 20:25:39  curt
190 // Portability changes and updates from Bernie Bright.
191 //
192 // Revision 1.2  1998/12/11 20:26:56  curt
193 // #include tweaks.
194 //
195 // Revision 1.1  1998/12/05 14:21:33  curt
196 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
197 // to it's own file, timestamp.hxx.
198 //