]> git.saurik.com Git - wxWidgets.git/blame - src/common/stopwatch.cpp
i18n files are installed as part of wxBase (and should be ideally part of separate...
[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
bb90a3e6
WS
31#include "wx/stopwatch.h"
32
e2478fde 33#ifndef WX_PRECOMP
8df6de97
VS
34 #include "wx/intl.h"
35 #include "wx/log.h"
e2478fde
VZ
36#endif //WX_PRECOMP
37
e2478fde
VZ
38// ----------------------------------------------------------------------------
39// System headers
40// ----------------------------------------------------------------------------
41
42#if defined(__WIN32__)
9ed0d735 43 #include "wx/msw/wrapwin.h"
e2478fde
VZ
44#endif
45
1c193821 46#if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
e2478fde
VZ
47 #define HAVE_FTIME
48#endif
49
50#if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
51 #define HAVE_FTIME
52# if __IBMCPP__ >= 400
53 # define ftime(x) _ftime(x)
54# endif
55#endif
56
57#if defined(__MWERKS__) && defined(__WXMSW__)
58# undef HAVE_FTIME
59# undef HAVE_GETTIMEOFDAY
60#endif
61
1c193821 62#ifndef __WXWINCE__
e2478fde 63#include <time.h>
1c193821
JS
64#else
65#include "wx/msw/private.h"
66#include "wx/msw/wince/time.h"
67#endif
68
69#if !defined(__WXMAC__) && !defined(__WXWINCE__)
e2478fde
VZ
70 #include <sys/types.h> // for time_t
71#endif
72
73#if defined(HAVE_GETTIMEOFDAY)
74 #include <sys/time.h>
75 #include <unistd.h>
76#elif defined(HAVE_FTIME)
77 #include <sys/timeb.h>
78#endif
79
80#ifdef __WXMAC__
768c6e8b 81#ifndef __DARWIN__
e2478fde
VZ
82 #include <Timer.h>
83 #include <DriverServices.h>
768c6e8b
SC
84#else
85 #include <Carbon/Carbon.h>
86#endif
e2478fde
VZ
87#endif
88
20bc5ad8
WS
89#ifdef __WXPALMOS__
90 #include <DateTime.h>
91 #include <TimeMgr.h>
92 #include <SystemMgr.h>
93#endif
94
e2478fde
VZ
95// ----------------------------------------------------------------------------
96// macros
97// ----------------------------------------------------------------------------
98
99// on some really old systems gettimeofday() doesn't have the second argument,
100// define wxGetTimeOfDay() to hide this difference
101#ifdef HAVE_GETTIMEOFDAY
102 #ifdef WX_GETTIMEOFDAY_NO_TZ
103 struct timezone;
104 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
105 #else
106 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
107 #endif
108#endif // HAVE_GETTIMEOFDAY
109
110// ============================================================================
111// implementation
112// ============================================================================
113
114// ----------------------------------------------------------------------------
115// wxStopWatch
116// ----------------------------------------------------------------------------
117
118#if wxUSE_STOPWATCH
119
120void wxStopWatch::Start(long t)
121{
85445d34
RR
122#if 0
123// __WXMSW__
2555c77a
RR
124 LARGE_INTEGER frequency_li;
125 ::QueryPerformanceFrequency( &frequency_li );
126 m_frequency = frequency_li.QuadPart;
127 if (m_frequency == 0)
128 {
129 m_t0 = wxGetLocalTimeMillis() - t;
130 }
131 else
132 {
133 LARGE_INTEGER counter_li;
134 ::QueryPerformanceCounter( &counter_li );
135 wxLongLong counter = counter_li.QuadPart;
136 m_t0 = (counter * 10000 / m_frequency) - t*10;
137 }
138#else
e2478fde 139 m_t0 = wxGetLocalTimeMillis() - t;
2555c77a 140#endif
e2478fde
VZ
141 m_pause = 0;
142 m_pauseCount = 0;
143}
144
145long wxStopWatch::GetElapsedTime() const
146{
85445d34
RR
147#if 0
148//__WXMSW__
2555c77a
RR
149 if (m_frequency == 0)
150 {
151 return (wxGetLocalTimeMillis() - m_t0).GetLo();
152 }
153 else
154 {
155 LARGE_INTEGER counter_li;
156 ::QueryPerformanceCounter( &counter_li );
157 wxLongLong counter = counter_li.QuadPart;
158 wxLongLong res = (counter * 10000 / m_frequency) - m_t0;
159 return res.GetLo() / 10;
160 }
161#else
e2478fde 162 return (wxGetLocalTimeMillis() - m_t0).GetLo();
2555c77a 163#endif
e2478fde
VZ
164}
165
166long wxStopWatch::Time() const
167{
168 return m_pauseCount ? m_pause : GetElapsedTime();
169}
170
171#endif // wxUSE_STOPWATCH
172
173// ----------------------------------------------------------------------------
174// old timer functions superceded by wxStopWatch
175// ----------------------------------------------------------------------------
176
177#if wxUSE_LONGLONG
178
179static wxLongLong wxStartTime = 0l;
180
181// starts the global timer
182void wxStartTimer()
183{
184 wxStartTime = wxGetLocalTimeMillis();
185}
186
187// Returns elapsed time in milliseconds
188long wxGetElapsedTime(bool resetTimer)
189{
190 wxLongLong oldTime = wxStartTime;
191 wxLongLong newTime = wxGetLocalTimeMillis();
192
193 if ( resetTimer )
194 wxStartTime = newTime;
195
196 return (newTime - oldTime).GetLo();
197}
198
199#endif // wxUSE_LONGLONG
200
201// ----------------------------------------------------------------------------
202// the functions to get the current time and timezone info
203// ----------------------------------------------------------------------------
204
205// Get local time as seconds since 00:00:00, Jan 1st 1970
206long wxGetLocalTime()
207{
208 struct tm tm;
209 time_t t0, t1;
210
211 // This cannot be made static because mktime can overwrite it.
212 //
213 memset(&tm, 0, sizeof(tm));
214 tm.tm_year = 70;
215 tm.tm_mon = 0;
216 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
217 tm.tm_hour = 0;
218 tm.tm_min = 0;
219 tm.tm_sec = 0;
220 tm.tm_isdst = -1; // let mktime guess
221
222 // Note that mktime assumes that the struct tm contains local time.
223 //
224 t1 = time(&t1); // now
225 t0 = mktime(&tm); // origin
226
227 // Return the difference in seconds.
228 //
229 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
230 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
231
232 wxLogSysError(_("Failed to get the local system time"));
233 return -1;
234}
235
236// Get UTC time as seconds since 00:00:00, Jan 1st 1970
237long wxGetUTCTime()
238{
940b4f07 239 return (long)time(NULL);
e2478fde
VZ
240}
241
242#if wxUSE_LONGLONG
243
244// Get local time as milliseconds since 00:00:00, Jan 1st 1970
245wxLongLong wxGetLocalTimeMillis()
246{
247 wxLongLong val = 1000l;
248
249 // If possible, use a function which avoids conversions from
250 // broken-up time structures to milliseconds
251
60582201
WS
252#if defined(__WXPALMOS__)
253 DateTimeType thenst;
254 thenst.second = 0;
255 thenst.minute = 0;
256 thenst.hour = 0;
257 thenst.day = 1;
258 thenst.month = 1;
259 thenst.year = 1970;
260 thenst.weekDay = 5;
261 uint32_t now = TimGetSeconds();
262 uint32_t then = TimDateTimeToSeconds (&thenst);
263 return SysTimeToMilliSecs(SysTimeInSecs(now - then));
264#elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
e2478fde
VZ
265 // This should probably be the way all WXMSW compilers should do it
266 // Go direct to the OS for time
267
268 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
269 FILETIME thenft;
270 SystemTimeToFileTime( &thenst, &thenft );
271 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
272
273 SYSTEMTIME nowst;
274 GetLocalTime( &nowst );
275 FILETIME nowft;
276 SystemTimeToFileTime( &nowst, &nowft );
277 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
278
279 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
d775fa82 280
e2478fde
VZ
281#elif defined(HAVE_GETTIMEOFDAY)
282 struct timeval tp;
283 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
284 {
285 val *= tp.tv_sec;
286 return (val + (tp.tv_usec / 1000));
287 }
288 else
289 {
290 wxLogError(_("wxGetTimeOfDay failed."));
291 return 0;
292 }
293#elif defined(HAVE_FTIME)
294 struct timeb tp;
295
296 // ftime() is void and not int in some mingw32 headers, so don't
297 // test the return code (well, it shouldn't fail anyhow...)
29468083 298 (void)::ftime(&tp);
e2478fde
VZ
299 val *= tp.time;
300 return (val + tp.millitm);
301#elif defined(__WXMAC__)
d775fa82 302
e2478fde
VZ
303 static UInt64 gMilliAtStart = 0;
304
305 Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() );
306
307 if ( gMilliAtStart == 0 )
308 {
309 time_t start = time(NULL);
310 gMilliAtStart = ((UInt64) start) * 1000000L;
311 gMilliAtStart -= upTime.lo / 1000 ;
312 gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
313 }
314
315 UInt64 millival = gMilliAtStart;
316 millival += upTime.lo / (1000 * 1000);
317 millival += ( ( (UInt64) upTime.hi ) << 32 ) / (1000 * 1000);
318 val = millival;
319
320 return val;
321#else // no gettimeofday() nor ftime()
322 // We use wxGetLocalTime() to get the seconds since
323 // 00:00:00 Jan 1st 1970 and then whatever is available
324 // to get millisecond resolution.
325 //
326 // NOTE that this might lead to a problem if the clocks
327 // use different sources, so this approach should be
328 // avoided where possible.
329
330 val *= wxGetLocalTime();
331
332// GRG: This will go soon as all WIN32 seem to have ftime
1c193821 333// JACS: unfortunately not. WinCE doesn't have it.
e2478fde
VZ
334#if defined (__WIN32__)
335 // If your platform/compiler needs to use two different functions
336 // to get ms resolution, please do NOT just shut off these warnings,
337 // drop me a line instead at <guille@iies.es>
1c193821
JS
338
339 // FIXME
340#ifndef __WXWINCE__
e2478fde 341 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
1c193821 342#endif
e2478fde
VZ
343
344 SYSTEMTIME st;
345 ::GetLocalTime(&st);
346 val += st.wMilliseconds;
347#else // !Win32
348 // If your platform/compiler does not support ms resolution please
349 // do NOT just shut off these warnings, drop me a line instead at
350 // <guille@iies.es>
351
352 #if defined(__VISUALC__) || defined (__WATCOMC__)
353 #pragma message("wxStopWatch will be up to second resolution!")
354 #elif defined(__BORLANDC__)
355 #pragma message "wxStopWatch will be up to second resolution!"
356 #else
357 #warning "wxStopWatch will be up to second resolution!"
358 #endif // compiler
359#endif
360
361 return val;
362
363#endif // time functions
364}
365
608f8a11
VZ
366#else // !wxUSE_LONGLONG
367
368double wxGetLocalTimeMillis(void)
369{
370 return (double(clock()) / double(CLOCKS_PER_SEC)) * 1000.0;
371}
372
373#endif // wxUSE_LONGLONG/!wxUSE_LONGLONG
e2478fde
VZ
374
375