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