]> git.saurik.com Git - wxWidgets.git/blame - src/common/stopwatch.cpp
Ignorant WinCE fixes for compile breakage
[wxWidgets.git] / src / common / stopwatch.cpp
CommitLineData
e2478fde
VZ
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$
77ffb593 12// Copyright: (c) 1998-2003 wxWidgets Team
0a53b9b8 13// License: wxWindows license
e2478fde
VZ
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
8df6de97
VS
32 #include "wx/intl.h"
33 #include "wx/log.h"
e2478fde
VZ
34#endif //WX_PRECOMP
35
e2478fde 36#include "wx/longlong.h"
d5b12ad9 37#include "wx/stopwatch.h"
e2478fde
VZ
38
39// ----------------------------------------------------------------------------
40// System headers
41// ----------------------------------------------------------------------------
42
43#if defined(__WIN32__)
9ed0d735 44 #include "wx/msw/wrapwin.h"
e2478fde
VZ
45#endif
46
1c193821 47#if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
e2478fde
VZ
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
1c193821 63#ifndef __WXWINCE__
e2478fde 64#include <time.h>
1c193821
JS
65#else
66#include "wx/msw/private.h"
67#include "wx/msw/wince/time.h"
68#endif
69
70#if !defined(__WXMAC__) && !defined(__WXWINCE__)
e2478fde
VZ
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
111void wxStopWatch::Start(long t)
112{
85445d34
RR
113#if 0
114// __WXMSW__
2555c77a
RR
115 LARGE_INTEGER frequency_li;
116 ::QueryPerformanceFrequency( &frequency_li );
117 m_frequency = frequency_li.QuadPart;
118 if (m_frequency == 0)
119 {
120 m_t0 = wxGetLocalTimeMillis() - t;
121 }
122 else
123 {
124 LARGE_INTEGER counter_li;
125 ::QueryPerformanceCounter( &counter_li );
126 wxLongLong counter = counter_li.QuadPart;
127 m_t0 = (counter * 10000 / m_frequency) - t*10;
128 }
129#else
e2478fde 130 m_t0 = wxGetLocalTimeMillis() - t;
2555c77a 131#endif
e2478fde
VZ
132 m_pause = 0;
133 m_pauseCount = 0;
134}
135
136long wxStopWatch::GetElapsedTime() const
137{
85445d34
RR
138#if 0
139//__WXMSW__
2555c77a
RR
140 if (m_frequency == 0)
141 {
142 return (wxGetLocalTimeMillis() - m_t0).GetLo();
143 }
144 else
145 {
146 LARGE_INTEGER counter_li;
147 ::QueryPerformanceCounter( &counter_li );
148 wxLongLong counter = counter_li.QuadPart;
149 wxLongLong res = (counter * 10000 / m_frequency) - m_t0;
150 return res.GetLo() / 10;
151 }
152#else
e2478fde 153 return (wxGetLocalTimeMillis() - m_t0).GetLo();
2555c77a 154#endif
e2478fde
VZ
155}
156
157long wxStopWatch::Time() const
158{
159 return m_pauseCount ? m_pause : GetElapsedTime();
160}
161
162#endif // wxUSE_STOPWATCH
163
164// ----------------------------------------------------------------------------
165// old timer functions superceded by wxStopWatch
166// ----------------------------------------------------------------------------
167
168#if wxUSE_LONGLONG
169
170static wxLongLong wxStartTime = 0l;
171
172// starts the global timer
173void wxStartTimer()
174{
175 wxStartTime = wxGetLocalTimeMillis();
176}
177
178// Returns elapsed time in milliseconds
179long wxGetElapsedTime(bool resetTimer)
180{
181 wxLongLong oldTime = wxStartTime;
182 wxLongLong newTime = wxGetLocalTimeMillis();
183
184 if ( resetTimer )
185 wxStartTime = newTime;
186
187 return (newTime - oldTime).GetLo();
188}
189
190#endif // wxUSE_LONGLONG
191
192// ----------------------------------------------------------------------------
193// the functions to get the current time and timezone info
194// ----------------------------------------------------------------------------
195
196// Get local time as seconds since 00:00:00, Jan 1st 1970
197long wxGetLocalTime()
198{
199 struct tm tm;
200 time_t t0, t1;
201
202 // This cannot be made static because mktime can overwrite it.
203 //
204 memset(&tm, 0, sizeof(tm));
205 tm.tm_year = 70;
206 tm.tm_mon = 0;
207 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
208 tm.tm_hour = 0;
209 tm.tm_min = 0;
210 tm.tm_sec = 0;
211 tm.tm_isdst = -1; // let mktime guess
212
213 // Note that mktime assumes that the struct tm contains local time.
214 //
215 t1 = time(&t1); // now
216 t0 = mktime(&tm); // origin
217
218 // Return the difference in seconds.
219 //
220 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
221 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
222
223 wxLogSysError(_("Failed to get the local system time"));
224 return -1;
225}
226
227// Get UTC time as seconds since 00:00:00, Jan 1st 1970
228long wxGetUTCTime()
229{
940b4f07 230 return (long)time(NULL);
e2478fde
VZ
231}
232
233#if wxUSE_LONGLONG
234
235// Get local time as milliseconds since 00:00:00, Jan 1st 1970
236wxLongLong wxGetLocalTimeMillis()
237{
238 wxLongLong val = 1000l;
239
240 // If possible, use a function which avoids conversions from
241 // broken-up time structures to milliseconds
242
cd2a4e16 243#if defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
e2478fde
VZ
244 // This should probably be the way all WXMSW compilers should do it
245 // Go direct to the OS for time
246
247 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
248 FILETIME thenft;
249 SystemTimeToFileTime( &thenst, &thenft );
250 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
251
252 SYSTEMTIME nowst;
253 GetLocalTime( &nowst );
254 FILETIME nowft;
255 SystemTimeToFileTime( &nowst, &nowft );
256 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
257
258 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
d775fa82 259
e2478fde
VZ
260#elif defined(HAVE_GETTIMEOFDAY)
261 struct timeval tp;
262 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
263 {
264 val *= tp.tv_sec;
265 return (val + (tp.tv_usec / 1000));
266 }
267 else
268 {
269 wxLogError(_("wxGetTimeOfDay failed."));
270 return 0;
271 }
272#elif defined(HAVE_FTIME)
273 struct timeb tp;
274
275 // ftime() is void and not int in some mingw32 headers, so don't
276 // test the return code (well, it shouldn't fail anyhow...)
277 (void)ftime(&tp);
278 val *= tp.time;
279 return (val + tp.millitm);
280#elif defined(__WXMAC__)
d775fa82 281
e2478fde
VZ
282 static UInt64 gMilliAtStart = 0;
283
284 Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() );
285
286 if ( gMilliAtStart == 0 )
287 {
288 time_t start = time(NULL);
289 gMilliAtStart = ((UInt64) start) * 1000000L;
290 gMilliAtStart -= upTime.lo / 1000 ;
291 gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
292 }
293
294 UInt64 millival = gMilliAtStart;
295 millival += upTime.lo / (1000 * 1000);
296 millival += ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
297 val = millival;
298
299 return val;
300#else // no gettimeofday() nor ftime()
301 // We use wxGetLocalTime() to get the seconds since
302 // 00:00:00 Jan 1st 1970 and then whatever is available
303 // to get millisecond resolution.
304 //
305 // NOTE that this might lead to a problem if the clocks
306 // use different sources, so this approach should be
307 // avoided where possible.
308
309 val *= wxGetLocalTime();
310
311// GRG: This will go soon as all WIN32 seem to have ftime
1c193821 312// JACS: unfortunately not. WinCE doesn't have it.
e2478fde
VZ
313#if defined (__WIN32__)
314 // If your platform/compiler needs to use two different functions
315 // to get ms resolution, please do NOT just shut off these warnings,
316 // drop me a line instead at <guille@iies.es>
1c193821
JS
317
318 // FIXME
319#ifndef __WXWINCE__
e2478fde 320 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
1c193821 321#endif
e2478fde
VZ
322
323 SYSTEMTIME st;
324 ::GetLocalTime(&st);
325 val += st.wMilliseconds;
326#else // !Win32
327 // If your platform/compiler does not support ms resolution please
328 // do NOT just shut off these warnings, drop me a line instead at
329 // <guille@iies.es>
330
331 #if defined(__VISUALC__) || defined (__WATCOMC__)
332 #pragma message("wxStopWatch will be up to second resolution!")
333 #elif defined(__BORLANDC__)
334 #pragma message "wxStopWatch will be up to second resolution!"
335 #else
336 #warning "wxStopWatch will be up to second resolution!"
337 #endif // compiler
338#endif
339
340 return val;
341
342#endif // time functions
343}
344
345#endif // wxUSE_LONGLONG
346
347