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