]> git.mxchange.org Git - flightgear.git/blob - Time/timestamp.hxx
Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
[flightgear.git] / 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
70 class FGTimeStamp {
71
72 private:
73
74     long seconds;
75     long usec;
76
77 public:
78
79     FGTimeStamp();
80     FGTimeStamp( const long s, const long m );
81     ~FGTimeStamp();
82
83     // Set time to current time
84     void stamp();
85
86     FGTimeStamp& operator = ( const FGTimeStamp& t );
87
88     friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
89     friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
90
91     inline long get_seconds() const { return seconds; }
92     // inline long get_usec() const { return usec; }
93 };
94
95 inline FGTimeStamp::FGTimeStamp() {
96 }
97
98 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
99     seconds = s;
100     usec = u;
101 }
102
103 inline FGTimeStamp::~FGTimeStamp() {
104 }
105
106 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
107 {
108     seconds = t.seconds;
109     usec = t.usec;
110     return *this;
111 }
112
113 inline void FGTimeStamp::stamp() {
114 #if defined( WIN32 )
115     unsigned int t;
116     t = timeGetTime();
117     seconds = 0;
118     usec =  t * 1000;
119 #elif defined( HAVE_GETTIMEOFDAY )
120     struct timeval current;
121     struct timezone tz;
122     // fg_timestamp currtime;
123     gettimeofday(&current, &tz);
124     seconds = current.tv_sec;
125     usec = current.tv_usec;
126 #elif defined( HAVE_GETLOCALTIME )
127     SYSTEMTIME current;
128     GetLocalTime(&current);
129     seconds = current.wSecond;
130     usec = current.wMilliseconds * 1000;
131 #elif defined( HAVE_FTIME )
132     struct timeb current;
133     ftime(&current);
134     seconds = current.time;
135     usec = current.millitm * 1000;
136 #else
137 # error Port me
138 #endif
139 }
140
141 // difference between time stamps in microseconds (usec)
142 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
143 #ifdef WIN32
144     return FGTimeStamp( 0, t.usec + m );
145 #else
146     return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
147                         ( t.usec + m ) % 1000000 );
148 #endif
149 }
150
151 // difference between time stamps in microseconds (usec)
152 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
153 {
154 #if defined( WIN32 )
155     return a.usec - b.usec;
156 #else
157     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
158 #endif
159 }
160
161
162 #endif // _TIMESTAMP_HXX
163
164
165 // $Log$
166 // Revision 1.4  1999/01/09 13:37:46  curt
167 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
168 //
169 // Revision 1.3  1999/01/07 20:25:39  curt
170 // Portability changes and updates from Bernie Bright.
171 //
172 // Revision 1.2  1998/12/11 20:26:56  curt
173 // #include tweaks.
174 //
175 // Revision 1.1  1998/12/05 14:21:33  curt
176 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
177 // to it's own file, timestamp.hxx.
178 //