]> git.mxchange.org Git - flightgear.git/blob - Time/timestamp.hxx
#include tweaks.
[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 <time.h>
44
45 #ifdef HAVE_SYS_TIMEB_H
46 #  include <sys/timeb.h> // for ftime() and struct timeb
47 #endif
48 #ifdef HAVE_UNISTD_H
49 #  include <unistd.h>    // for gettimeofday()
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
53 #endif
54
55 #ifdef  WIN32
56 #  include <windows.h>
57 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
58 #    define NEAR /* */
59 #    define FAR  /* */
60 #  endif
61 #  include <mmsystem.h>
62 #endif
63
64
65 class fgTIMESTAMP {
66
67 private:
68
69     long seconds;
70     long millis;
71
72 public:
73
74     fgTIMESTAMP();
75     fgTIMESTAMP( const long s, const long m );
76     ~fgTIMESTAMP();
77
78     // Set time to current time
79     void stamp();
80
81     fgTIMESTAMP& operator = ( const fgTIMESTAMP& t );
82
83     friend fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m);
84     friend long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b);
85
86     inline long get_seconds() const { return seconds; }
87     inline long get_millis() const { return millis; }
88 };
89
90 inline fgTIMESTAMP::fgTIMESTAMP() {
91 }
92
93 inline fgTIMESTAMP::fgTIMESTAMP( const long s, const long m ) {
94     seconds = s;
95     millis = m;
96 }
97
98 inline fgTIMESTAMP::~fgTIMESTAMP() {
99 }
100
101 inline fgTIMESTAMP& fgTIMESTAMP::operator = (const fgTIMESTAMP& t)
102 {
103     seconds = t.seconds;
104     millis = t.millis;
105     return *this;
106 }
107
108 inline void fgTIMESTAMP::stamp() {
109 #if defined( WIN32 )
110     unsigned int t;
111     t = timeGetTime();
112     seconds = 0;
113     millis =  t;
114 #elif defined( HAVE_GETTIMEOFDAY )
115     struct timeval current;
116     struct timezone tz;
117     // fg_timestamp currtime;
118     gettimeofday(&current, &tz);
119     seconds = current.tv_sec;
120     millis = current.tv_usec / 1000;
121 #elif defined( HAVE_GETLOCALTIME )
122     SYSTEMTIME current;
123     GetLocalTime(&current);
124     seconds = current.wSecond;
125     millis = current.wMilliseconds;
126 #elif defined( HAVE_FTIME )
127     struct timeb current;
128     ftime(&current);
129     seconds = current.time;
130     millis = current.millitm;
131 #else
132 # error Port me
133 #endif
134 }
135
136 // difference between time stamps in milliseconds
137 inline fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m) {
138 #ifdef WIN32
139     return fgTIMESTAMP( 0, t.millis + m );
140 #else
141     return fgTIMESTAMP( t.seconds + ( t.millis + m ) / 1000,
142                         ( t.millis + m ) % 1000 );
143 #endif
144 }
145
146 // difference between time stamps in milliseconds
147 inline long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b)
148 {
149 #if defined( WIN32 )
150     return a.millis - b.millis;
151 #else
152     return 1000 * (a.seconds - b.seconds) + (a.millis - b.millis);
153 #endif
154 }
155
156
157 #endif // _TIMESTAMP_HXX
158
159
160 // $Log$
161 // Revision 1.2  1998/12/11 20:26:56  curt
162 // #include tweaks.
163 //
164 // Revision 1.1  1998/12/05 14:21:33  curt
165 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
166 // to it's own file, timestamp.hxx.
167 //