Temporary fix for VC++ compile breakage.
[wxWidgets.git] / src / common / stopwatch.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/stopwatch.cpp
3 // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h
4 // Author:
5 // Original version by Julian Smart
6 // Vadim Zeitlin got rid of all ifdefs (11.12.99)
7 // Sylvain Bougnoux added wxStopWatch class
8 // Guillermo Rodriguez <guille@iies.es> rewrote from scratch (Dic/99)
9 // Modified by:
10 // Created: 20.06.2003 (extracted from common/timercmn.cpp)
11 // RCS-ID: $Id$
12 // Copyright: (c) 1998-2003 wxWindows Team
13 // License: wxWindows license
14 ///////////////////////////////////////////////////////////////////////////////
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif //WX_PRECOMP
35
36 #include "wx/longlong.h"
37 #include "wx/stopwatch.h"
38
39 // ----------------------------------------------------------------------------
40 // System headers
41 // ----------------------------------------------------------------------------
42
43 #if defined(__WIN32__)
44 #include "wx/msw/wrapwin.h"
45 #endif
46
47 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
48 #define HAVE_FTIME
49 #endif
50
51 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
52 #define HAVE_FTIME
53 # if __IBMCPP__ >= 400
54 # define ftime(x) _ftime(x)
55 # endif
56 #endif
57
58 #if defined(__MWERKS__) && defined(__WXMSW__)
59 # undef HAVE_FTIME
60 # undef HAVE_GETTIMEOFDAY
61 #endif
62
63 #ifndef __WXWINCE__
64 #include <time.h>
65 #else
66 #include "wx/msw/private.h"
67 #include "wx/msw/wince/time.h"
68 #endif
69
70 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
71 #include <sys/types.h> // for time_t
72 #endif
73
74 #if defined(HAVE_GETTIMEOFDAY)
75 #include <sys/time.h>
76 #include <unistd.h>
77 #elif defined(HAVE_FTIME)
78 #include <sys/timeb.h>
79 #endif
80
81 #ifdef __WXMAC__
82 #include <Timer.h>
83 #include <DriverServices.h>
84 #endif
85
86 // ----------------------------------------------------------------------------
87 // macros
88 // ----------------------------------------------------------------------------
89
90 // on some really old systems gettimeofday() doesn't have the second argument,
91 // define wxGetTimeOfDay() to hide this difference
92 #ifdef HAVE_GETTIMEOFDAY
93 #ifdef WX_GETTIMEOFDAY_NO_TZ
94 struct timezone;
95 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
96 #else
97 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
98 #endif
99 #endif // HAVE_GETTIMEOFDAY
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ----------------------------------------------------------------------------
106 // wxStopWatch
107 // ----------------------------------------------------------------------------
108
109 #if wxUSE_STOPWATCH
110
111 void wxStopWatch::Start(long t)
112 {
113 m_t0 = wxGetLocalTimeMillis() - t;
114 m_pause = 0;
115 m_pauseCount = 0;
116 }
117
118 long wxStopWatch::GetElapsedTime() const
119 {
120 return (wxGetLocalTimeMillis() - m_t0).GetLo();
121 }
122
123 long wxStopWatch::Time() const
124 {
125 return m_pauseCount ? m_pause : GetElapsedTime();
126 }
127
128 #endif // wxUSE_STOPWATCH
129
130 // ----------------------------------------------------------------------------
131 // old timer functions superceded by wxStopWatch
132 // ----------------------------------------------------------------------------
133
134 #if wxUSE_LONGLONG
135
136 static wxLongLong wxStartTime = 0l;
137
138 // starts the global timer
139 void wxStartTimer()
140 {
141 wxStartTime = wxGetLocalTimeMillis();
142 }
143
144 // Returns elapsed time in milliseconds
145 long wxGetElapsedTime(bool resetTimer)
146 {
147 wxLongLong oldTime = wxStartTime;
148 wxLongLong newTime = wxGetLocalTimeMillis();
149
150 if ( resetTimer )
151 wxStartTime = newTime;
152
153 return (newTime - oldTime).GetLo();
154 }
155
156 #endif // wxUSE_LONGLONG
157
158 // ----------------------------------------------------------------------------
159 // the functions to get the current time and timezone info
160 // ----------------------------------------------------------------------------
161
162 // Get local time as seconds since 00:00:00, Jan 1st 1970
163 long wxGetLocalTime()
164 {
165 struct tm tm;
166 time_t t0, t1;
167
168 // This cannot be made static because mktime can overwrite it.
169 //
170 memset(&tm, 0, sizeof(tm));
171 tm.tm_year = 70;
172 tm.tm_mon = 0;
173 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
174 tm.tm_hour = 0;
175 tm.tm_min = 0;
176 tm.tm_sec = 0;
177 tm.tm_isdst = -1; // let mktime guess
178
179 // Note that mktime assumes that the struct tm contains local time.
180 //
181 t1 = time(&t1); // now
182 t0 = mktime(&tm); // origin
183
184 // Return the difference in seconds.
185 //
186 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
187 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
188
189 wxLogSysError(_("Failed to get the local system time"));
190 return -1;
191 }
192
193 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
194 long wxGetUTCTime()
195 {
196 #ifdef _MSC_VER
197 unsigned
198 #endif
199 long timenow = 0;
200 time(&timenow);
201 return timenow;
202 }
203
204 #if wxUSE_LONGLONG
205
206 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
207 wxLongLong wxGetLocalTimeMillis()
208 {
209 wxLongLong val = 1000l;
210
211 // If possible, use a function which avoids conversions from
212 // broken-up time structures to milliseconds
213
214 #if defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
215 // This should probably be the way all WXMSW compilers should do it
216 // Go direct to the OS for time
217
218 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
219 FILETIME thenft;
220 SystemTimeToFileTime( &thenst, &thenft );
221 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
222
223 SYSTEMTIME nowst;
224 GetLocalTime( &nowst );
225 FILETIME nowft;
226 SystemTimeToFileTime( &nowst, &nowft );
227 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
228
229 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
230
231 #elif defined(HAVE_GETTIMEOFDAY)
232 struct timeval tp;
233 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
234 {
235 val *= tp.tv_sec;
236 return (val + (tp.tv_usec / 1000));
237 }
238 else
239 {
240 wxLogError(_("wxGetTimeOfDay failed."));
241 return 0;
242 }
243 #elif defined(HAVE_FTIME)
244 struct timeb tp;
245
246 // ftime() is void and not int in some mingw32 headers, so don't
247 // test the return code (well, it shouldn't fail anyhow...)
248 (void)ftime(&tp);
249 val *= tp.time;
250 return (val + tp.millitm);
251 #elif defined(__WXMAC__)
252
253 static UInt64 gMilliAtStart = 0;
254
255 Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() );
256
257 if ( gMilliAtStart == 0 )
258 {
259 time_t start = time(NULL);
260 gMilliAtStart = ((UInt64) start) * 1000000L;
261 gMilliAtStart -= upTime.lo / 1000 ;
262 gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
263 }
264
265 UInt64 millival = gMilliAtStart;
266 millival += upTime.lo / (1000 * 1000);
267 millival += ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
268 val = millival;
269
270 return val;
271 #else // no gettimeofday() nor ftime()
272 // We use wxGetLocalTime() to get the seconds since
273 // 00:00:00 Jan 1st 1970 and then whatever is available
274 // to get millisecond resolution.
275 //
276 // NOTE that this might lead to a problem if the clocks
277 // use different sources, so this approach should be
278 // avoided where possible.
279
280 val *= wxGetLocalTime();
281
282 // GRG: This will go soon as all WIN32 seem to have ftime
283 // JACS: unfortunately not. WinCE doesn't have it.
284 #if defined (__WIN32__)
285 // If your platform/compiler needs to use two different functions
286 // to get ms resolution, please do NOT just shut off these warnings,
287 // drop me a line instead at <guille@iies.es>
288
289 // FIXME
290 #ifndef __WXWINCE__
291 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
292 #endif
293
294 SYSTEMTIME st;
295 ::GetLocalTime(&st);
296 val += st.wMilliseconds;
297 #else // !Win32
298 // If your platform/compiler does not support ms resolution please
299 // do NOT just shut off these warnings, drop me a line instead at
300 // <guille@iies.es>
301
302 #if defined(__VISUALC__) || defined (__WATCOMC__)
303 #pragma message("wxStopWatch will be up to second resolution!")
304 #elif defined(__BORLANDC__)
305 #pragma message "wxStopWatch will be up to second resolution!"
306 #else
307 #warning "wxStopWatch will be up to second resolution!"
308 #endif // compiler
309 #endif
310
311 return val;
312
313 #endif // time functions
314 }
315
316 #endif // wxUSE_LONGLONG
317
318