]> git.saurik.com Git - wxWidgets.git/blame - src/common/timercmn.cpp
Try this again
[wxWidgets.git] / src / common / timercmn.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
0470b1e6 2// Name: common/timercmn.cpp
c801d85f 3// Purpose: Common timer implementation
5ebdc86a
GRG
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:
c801d85f
KB
10// Created: 04/01/98
11// RCS-ID: $Id$
12// Copyright: (c) Julian Smart and Markus Holzem
b704229e 13// (c) 1999 Guillermo Rodriguez <guille@iies.es>
ed791986 14// Licence: wxWindows license
c801d85f
KB
15/////////////////////////////////////////////////////////////////////////////
16
0470b1e6
VZ
17// ============================================================================
18// declarations
19// ============================================================================
20
21// ----------------------------------------------------------------------------
91ff98b7 22// wxWin headers
0470b1e6
VZ
23// ----------------------------------------------------------------------------
24
c801d85f 25#ifdef __GNUG__
0470b1e6 26 #pragma implementation "timerbase.h"
c801d85f
KB
27#endif
28
29// For compilers that support precompilation, includes "wx.h".
30#include "wx/wxprec.h"
31
32#ifdef __BORLANDC__
0470b1e6 33 #pragma hdrstop
c801d85f
KB
34#endif
35
36#ifndef WX_PRECOMP
0470b1e6
VZ
37 #include "wx/intl.h"
38 #include "wx/log.h"
2b5f62a0 39 #include "wx/thread.h"
c801d85f
KB
40#endif
41
42#include "wx/timer.h"
b8f04990 43#include "wx/longlong.h"
c801d85f 44
91ff98b7
GRG
45// ----------------------------------------------------------------------------
46// System headers
47// ----------------------------------------------------------------------------
48
b8f04990
GRG
49#if defined(__WIN32__)
50 #include <windows.h>
0470b1e6
VZ
51#endif
52
ba14d986 53#if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
91ff98b7 54 #define HAVE_FTIME
91ff98b7
GRG
55#endif
56
47af9124
GRG
57#if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
58 #define HAVE_FTIME
07d7f20b
DW
59# if __IBMCPP__ >= 400
60 # define ftime(x) _ftime(x)
61# endif
47af9124
GRG
62#endif
63
de85a884
VZ
64#if defined(__MWERKS__) && defined(__WXMSW__)
65# undef HAVE_FTIME
66# undef HAVE_GETTIMEOFDAY
67#endif
68
b8f04990
GRG
69#include <time.h>
70#ifndef __WXMAC__
71 #include <sys/types.h> // for time_t
07cf98cb
VZ
72#endif
73
0470b1e6
VZ
74#if defined(HAVE_GETTIMEOFDAY)
75 #include <sys/time.h>
76 #include <unistd.h>
0470b1e6
VZ
77#elif defined(HAVE_FTIME)
78 #include <sys/timeb.h>
c801d85f
KB
79#endif
80
a4c4cf10
SC
81#ifdef __WXMAC__
82 #include <Timer.h>
83 #include <DriverServices.h>
84#endif
ed791986
VZ
85// ----------------------------------------------------------------------------
86// wxWin macros
87// ----------------------------------------------------------------------------
88
1e6feb95 89#if wxUSE_GUI && wxUSE_TIMER
52a07708
VZ
90 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent)
91#endif // wxUSE_GUI
ed791986 92
0470b1e6
VZ
93// ----------------------------------------------------------------------------
94// macros
95// ----------------------------------------------------------------------------
ce3ed50d 96
0470b1e6
VZ
97// on some really old systems gettimeofday() doesn't have the second argument,
98// define wxGetTimeOfDay() to hide this difference
99#ifdef HAVE_GETTIMEOFDAY
100 #ifdef WX_GETTIMEOFDAY_NO_TZ
101 struct timezone;
102 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
103 #else
104 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
105 #endif
106#endif // HAVE_GETTIMEOFDAY
c801d85f 107
0470b1e6
VZ
108// ============================================================================
109// implementation
110// ============================================================================
c801d85f 111
ed791986
VZ
112// ----------------------------------------------------------------------------
113// wxTimerBase
114// ----------------------------------------------------------------------------
115
1e6feb95 116#if wxUSE_GUI && wxUSE_TIMER
91ff98b7 117
799ea011
GD
118wxTimerBase::~wxTimerBase()
119{
120 // this destructor is required for Darwin
121}
122
ed791986
VZ
123void wxTimerBase::Notify()
124{
125 // the base class version generates an event if it has owner - which it
126 // should because otherwise nobody can process timer events
127 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
128
129 wxTimerEvent event(m_idTimer, m_milli);
130 (void)m_owner->ProcessEvent(event);
131}
b8f04990 132
99646f7e
VZ
133bool wxTimerBase::Start(int milliseconds, bool oneShot)
134{
2b5f62a0
VZ
135 // under MSW timers only work when they're started from the main thread so
136 // let the caller know about it
137#if wxUSE_THREADS
138 wxASSERT_MSG( wxThread::IsMain(),
139 _T("timer can only be started from the main thread") );
140#endif // wxUSE_THREADS
141
99646f7e
VZ
142 if ( IsRunning() )
143 {
144 // not stopping the already running timer might work for some
145 // platforms (no problems under MSW) but leads to mysterious crashes
146 // on the others (GTK), so to be on the safe side do it here
147 Stop();
148 }
149
150 if ( milliseconds != -1 )
151 {
152 m_milli = milliseconds;
153 }
154
155 m_oneShot = oneShot;
156
157 return TRUE;
158}
159
52a07708
VZ
160#endif // wxUSE_GUI
161
0470b1e6
VZ
162// ----------------------------------------------------------------------------
163// wxStopWatch
164// ----------------------------------------------------------------------------
c801d85f 165
1e6feb95
VZ
166#if wxUSE_LONGLONG
167
0470b1e6
VZ
168void wxStopWatch::Start(long t)
169{
b8f04990 170 m_t0 = wxGetLocalTimeMillis() - t;
0470b1e6 171 m_pause = 0;
5553c7cf 172 m_pauseCount = 0;
0470b1e6
VZ
173}
174
92da8bde 175long wxStopWatch::GetElapsedTime() const
0470b1e6 176{
677eff07 177 return (wxGetLocalTimeMillis() - m_t0).GetLo();
b8f04990
GRG
178}
179
8242fb3b 180long wxStopWatch::Time() const
b8f04990 181{
677eff07 182 return m_pauseCount ? m_pause : GetElapsedTime();
0470b1e6
VZ
183}
184
1e6feb95
VZ
185#endif // wxUSE_LONGLONG
186
0470b1e6
VZ
187// ----------------------------------------------------------------------------
188// old timer functions superceded by wxStopWatch
189// ----------------------------------------------------------------------------
c801d85f 190
1e6feb95
VZ
191#if wxUSE_LONGLONG
192
cd0b1709 193static wxLongLong wxStartTime = 0l;
c801d85f 194
0470b1e6
VZ
195// starts the global timer
196void wxStartTimer()
c801d85f 197{
b8f04990 198 wxStartTime = wxGetLocalTimeMillis();
c801d85f
KB
199}
200
201// Returns elapsed time in milliseconds
202long wxGetElapsedTime(bool resetTimer)
f0599ea9 203{
b8f04990
GRG
204 wxLongLong oldTime = wxStartTime;
205 wxLongLong newTime = wxGetLocalTimeMillis();
f0599ea9 206
0470b1e6
VZ
207 if ( resetTimer )
208 wxStartTime = newTime;
209
b8f04990 210 return (newTime - oldTime).GetLo();
f0599ea9
SB
211}
212
1e6feb95 213#endif // wxUSE_LONGLONG
f0599ea9 214
0470b1e6
VZ
215// ----------------------------------------------------------------------------
216// the functions to get the current time and timezone info
217// ----------------------------------------------------------------------------
218
b8f04990
GRG
219// Get local time as seconds since 00:00:00, Jan 1st 1970
220long wxGetLocalTime()
c801d85f 221{
b8f04990
GRG
222 struct tm tm;
223 time_t t0, t1;
224
225 // This cannot be made static because mktime can overwrite it.
226 //
227 memset(&tm, 0, sizeof(tm));
228 tm.tm_year = 70;
229 tm.tm_mon = 0;
230 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
231 tm.tm_hour = 0;
232 tm.tm_min = 0;
233 tm.tm_sec = 0;
234 tm.tm_isdst = -1; // let mktime guess
235
236 // Note that mktime assumes that the struct tm contains local time.
237 //
238 t1 = time(&t1); // now
239 t0 = mktime(&tm); // origin
240
241 // Return the difference in seconds.
242 //
243 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
244 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
245
246 wxLogSysError(_("Failed to get the local system time"));
247 return -1;
248}
b76b015e 249
b8f04990
GRG
250// Get UTC time as seconds since 00:00:00, Jan 1st 1970
251long wxGetUTCTime()
252{
33ac7e6f 253 struct tm tm;
1e6feb95 254 struct tm *ptm;
b8f04990
GRG
255 time_t t0, t1;
256
257 // This cannot be made static because mktime can overwrite it
258 //
259 memset(&tm, 0, sizeof(tm));
260 tm.tm_year = 70;
261 tm.tm_mon = 0;
262 tm.tm_mday = 5; // not Jan 1st 1970 due to mktime 'feature'
263 tm.tm_hour = 0;
264 tm.tm_min = 0;
265 tm.tm_sec = 0;
266 tm.tm_isdst = -1; // let mktime guess
267
268 // Note that mktime assumes that the struct tm contains local time.
269 //
270 t1 = time(&t1); // now
271 t0 = mktime(&tm); // origin in localtime
272
273 if (( t0 != (time_t)-1 ) && ( t1 != (time_t)-1 ))
0470b1e6 274 {
b8f04990
GRG
275 // To get t0 as GMT we convert to a struct tm with gmtime,
276 // and then back again.
277 //
278 ptm = gmtime(&t0);
0470b1e6 279
b8f04990 280 if (ptm)
0470b1e6 281 {
b8f04990
GRG
282 memcpy(&tm, ptm, sizeof(tm));
283 t0 = mktime(&tm);
284
285 if (t0 != (time_t)-1 )
286 return (long)difftime(t1, t0) + (60 * 60 * 24 * 4);
2c8e4738
VZ
287 wxLogSysError(_("mktime() failed"));
288 }
289 else
290 {
291 wxLogSysError(_("gmtime() failed"));
0470b1e6
VZ
292 }
293 }
2c8e4738
VZ
294
295 wxLogError(_("Failed to get the UTC system time."));
296
b8f04990
GRG
297 return -1;
298}
299
1e6feb95 300#if wxUSE_LONGLONG
b8f04990
GRG
301
302// Get local time as milliseconds since 00:00:00, Jan 1st 1970
303wxLongLong wxGetLocalTimeMillis()
304{
0158f584
SB
305 wxLongLong val = 1000l;
306
9e3cb9ee
JS
307 // If possible, use a function which avoids conversions from
308 // broken-up time structures to milliseconds
658c5400 309
ba14d986
VZ
310#if defined(__WXMSW__) && defined(__MWERKS__)
311 // This should probably be the way all WXMSW compilers should do it
312 // Go direct to the OS for time
313
314 SYSTEMTIME thenst = { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
315 FILETIME thenft;
316 SystemTimeToFileTime( &thenst, &thenft );
317 wxLongLong then( thenft.dwHighDateTime, thenft.dwLowDateTime ); // time in 100 nanoseconds
318
319 SYSTEMTIME nowst;
320 GetLocalTime( &nowst );
321 FILETIME nowft;
322 SystemTimeToFileTime( &nowst, &nowft );
323 wxLongLong now( nowft.dwHighDateTime, nowft.dwLowDateTime ); // time in 100 nanoseconds
324
325 return ( now - then ) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
326
327#elif defined(HAVE_GETTIMEOFDAY)
0158f584
SB
328 struct timeval tp;
329 if ( wxGetTimeOfDay(&tp, (struct timezone *)NULL) != -1 )
330 {
331 val *= tp.tv_sec;
332 return (val + (tp.tv_usec / 1000));
333 }
91ff98b7
GRG
334 else
335 {
336 wxLogError(_("wxGetTimeOfDay failed."));
337 return 0;
338 }
658c5400
GRG
339#elif defined(HAVE_FTIME)
340 struct timeb tp;
2c8e4738 341
91ff98b7
GRG
342 // ftime() is void and not int in some mingw32 headers, so don't
343 // test the return code (well, it shouldn't fail anyhow...)
2c8e4738
VZ
344 (void)ftime(&tp);
345 val *= tp.time;
346 return (val + tp.millitm);
a4c4cf10
SC
347#elif defined(__WXMAC__)
348
a4c4cf10
SC
349 UInt64 gMilliAtStart = 0 ;
350 Nanoseconds upTime = AbsoluteToNanoseconds( UpTime() ) ;
351 if ( gMilliAtStart == 0 )
352 {
353 time_t start = time(NULL) ;
354 gMilliAtStart = ((UInt64) start) * 1000L ;
355 gMilliAtStart -= upTime.lo / 1000 ;
356 gMilliAtStart -= ( ( (UInt64) upTime.hi ) << 32 ) / 1000 ;
357 }
358 UInt64 millival = gMilliAtStart ;
359 millival += upTime.lo / 1000 ;
360 millival += ( ( (UInt64) upTime.hi ) << 32 ) / 1000 ;
79654e25 361 val = millival ;
3340066a 362 return val ;
f6bcfd97 363#else // no gettimeofday() nor ftime()
b8f04990
GRG
364 // We use wxGetLocalTime() to get the seconds since
365 // 00:00:00 Jan 1st 1970 and then whatever is available
366 // to get millisecond resolution.
658c5400 367 //
91ff98b7
GRG
368 // NOTE that this might lead to a problem if the clocks
369 // use different sources, so this approach should be
370 // avoided where possible.
658c5400 371
cd0b1709 372 val *= wxGetLocalTime();
b8f04990 373
47af9124
GRG
374// GRG: This will go soon as all WIN32 seem to have ftime
375#if defined (__WIN32__)
f6bcfd97
BP
376 // If your platform/compiler needs to use two different functions
377 // to get ms resolution, please do NOT just shut off these warnings,
378 // drop me a line instead at <guille@iies.es>
379 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
380
91ff98b7
GRG
381 SYSTEMTIME st;
382 ::GetLocalTime(&st);
383 val += st.wMilliseconds;
f6bcfd97
BP
384#else // !Win32
385 // If your platform/compiler does not support ms resolution please
386 // do NOT just shut off these warnings, drop me a line instead at
387 // <guille@iies.es>
388
19193a2c 389 #if defined(__VISUALC__) || defined (__WATCOMC__)
f6bcfd97
BP
390 #pragma message("wxStopWatch will be up to second resolution!")
391 #elif defined(__BORLANDC__)
392 #pragma message "wxStopWatch will be up to second resolution!"
ec7cd4c4 393 #else
f6bcfd97
BP
394 #warning "wxStopWatch will be up to second resolution!"
395 #endif // compiler
c801d85f 396#endif
c801d85f 397
b8f04990 398 return val;
91ff98b7 399
f6bcfd97 400#endif // time functions
c801d85f 401}
1e6feb95
VZ
402
403#endif // wxUSE_LONGLONG
404