]> git.mxchange.org Git - flightgear.git/blob - Simulator/Time/timestamp.hxx
Removed in-src cvs logs.
[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
24
25 #ifndef _TIMESTAMP_HXX
26 #define _TIMESTAMP_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37
38 #ifdef HAVE_WINDOWS_H
39 #  include <windows.h>
40 #endif
41
42 #include "Include/compiler.h"
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 #ifdef  WIN32
60 #  include <windows.h>
61 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
62 #    define NEAR /* */
63 #    define FAR  /* */
64 #  endif
65 #  include <mmsystem.h>
66 #endif
67
68 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
69 class FGTimeStamp;
70 FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
71 long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
72
73 class FGTimeStamp {
74
75 private:
76
77     long seconds;
78     long usec;
79
80 public:
81
82     FGTimeStamp();
83     FGTimeStamp( const long s, const long m );
84     ~FGTimeStamp();
85
86     // Set time to current time
87     void stamp();
88
89     FGTimeStamp& operator = ( const FGTimeStamp& t );
90
91     friend FGTimeStamp operator + (const FGTimeStamp& t, const long& m);
92     friend long operator - (const FGTimeStamp& a, const FGTimeStamp& b);
93
94     inline long get_seconds() const { return seconds; }
95     // inline long get_usec() const { return usec; }
96 };
97
98 inline FGTimeStamp::FGTimeStamp() {
99 }
100
101 inline FGTimeStamp::FGTimeStamp( const long s, const long u ) {
102     seconds = s;
103     usec = u;
104 }
105
106 inline FGTimeStamp::~FGTimeStamp() {
107 }
108
109 inline FGTimeStamp& FGTimeStamp::operator = (const FGTimeStamp& t)
110 {
111     seconds = t.seconds;
112     usec = t.usec;
113     return *this;
114 }
115
116 inline void FGTimeStamp::stamp() {
117 #if defined( WIN32 )
118     unsigned int t;
119     t = timeGetTime();
120     seconds = 0;
121     usec =  t * 1000;
122 #elif defined( HAVE_GETTIMEOFDAY )
123     struct timeval current;
124     struct timezone tz;
125     // fg_timestamp currtime;
126     gettimeofday(&current, &tz);
127     seconds = current.tv_sec;
128     usec = current.tv_usec;
129 #elif defined( HAVE_GETLOCALTIME )
130     SYSTEMTIME current;
131     GetLocalTime(&current);
132     seconds = current.wSecond;
133     usec = current.wMilliseconds * 1000;
134 #elif defined( HAVE_FTIME )
135     struct timeb current;
136     ftime(&current);
137     seconds = current.time;
138     usec = current.millitm * 1000;
139 #else
140 # error Port me
141 #endif
142 }
143
144 // difference between time stamps in microseconds (usec)
145 inline FGTimeStamp operator + (const FGTimeStamp& t, const long& m) {
146 #ifdef WIN32
147     return FGTimeStamp( 0, t.usec + m );
148 #else
149     return FGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
150                         ( t.usec + m ) % 1000000 );
151 #endif
152 }
153
154 // difference between time stamps in microseconds (usec)
155 inline long operator - (const FGTimeStamp& a, const FGTimeStamp& b)
156 {
157 #if defined( WIN32 )
158     return a.usec - b.usec;
159 #else
160     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
161 #endif
162 }
163
164
165 #endif // _TIMESTAMP_HXX
166
167