]>
git.saurik.com Git - wxWidgets.git/blob - src/common/timercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/timercmn.cpp
3 // Purpose: Common timer implementation
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)
12 // Copyright: (c) Julian Smart
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows licence
15 /////////////////////////////////////////////////////////////////////////////
17 // ============================================================================
19 // ============================================================================
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 #pragma implementation "timerbase.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
39 #include "wx/thread.h"
43 #include "wx/longlong.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if defined(__WIN32__)
53 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
57 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
59 # if __IBMCPP__ >= 400
60 # define ftime(x) _ftime(x)
64 #if defined(__MWERKS__) && defined(__WXMSW__)
66 # undef HAVE_GETTIMEOFDAY
71 #include <sys/types.h> // for time_t
74 #if defined(HAVE_GETTIMEOFDAY)
77 #elif defined(HAVE_FTIME)
78 #include <sys/timeb.h>
83 #include <DriverServices.h>
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 #if wxUSE_GUI && wxUSE_TIMER
90 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
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
102 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
104 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
106 #endif // HAVE_GETTIMEOFDAY
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 #if wxUSE_GUI && wxUSE_TIMER
118 wxTimerBase::~wxTimerBase()
120 // this destructor is required for Darwin
123 void wxTimerBase::Notify()
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.") );
129 wxTimerEvent
event(m_idTimer
, m_milli
);
130 (void)m_owner
->ProcessEvent(event
);
133 bool wxTimerBase::Start(int milliseconds
, bool oneShot
)
135 // under MSW timers only work when they're started from the main thread so
136 // let the caller know about it
138 wxASSERT_MSG( wxThread::IsMain(),
139 _T("timer can only be started from the main thread") );
140 #endif // wxUSE_THREADS
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
150 if ( milliseconds
!= -1 )
152 m_milli
= milliseconds
;
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 #if wxUSE_STOPWATCH && wxUSE_LONGLONG
168 void wxStopWatch::Start(long t
)
170 m_t0
= wxGetLocalTimeMillis() - t
;
175 long wxStopWatch::GetElapsedTime() const
177 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
180 long wxStopWatch::Time() const
182 return m_pauseCount
? m_pause
: GetElapsedTime();
185 #endif // wxUSE_LONGLONG
187 // ----------------------------------------------------------------------------
188 // old timer functions superceded by wxStopWatch
189 // ----------------------------------------------------------------------------
193 static wxLongLong wxStartTime
= 0l;
195 // starts the global timer
198 wxStartTime
= wxGetLocalTimeMillis();
201 // Returns elapsed time in milliseconds
202 long wxGetElapsedTime(bool resetTimer
)
204 wxLongLong oldTime
= wxStartTime
;
205 wxLongLong newTime
= wxGetLocalTimeMillis();
208 wxStartTime
= newTime
;
210 return (newTime
- oldTime
).GetLo();
213 #endif // wxUSE_LONGLONG
215 // ----------------------------------------------------------------------------
216 // the functions to get the current time and timezone info
217 // ----------------------------------------------------------------------------
219 // Get local time as seconds since 00:00:00, Jan 1st 1970
220 long wxGetLocalTime()
225 // This cannot be made static because mktime can overwrite it.
227 memset(&tm
, 0, sizeof(tm
));
230 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
234 tm
.tm_isdst
= -1; // let mktime guess
236 // Note that mktime assumes that the struct tm contains local time.
238 t1
= time(&t1
); // now
239 t0
= mktime(&tm
); // origin
241 // Return the difference in seconds.
243 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
244 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
246 wxLogSysError(_("Failed to get the local system time"));
250 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
257 // This cannot be made static because mktime can overwrite it
259 memset(&tm
, 0, sizeof(tm
));
262 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
266 tm
.tm_isdst
= -1; // let mktime guess
268 // Note that mktime assumes that the struct tm contains local time.
270 t1
= time(&t1
); // now
271 t0
= mktime(&tm
); // origin in localtime
273 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
275 // To get t0 as GMT we convert to a struct tm with gmtime,
276 // and then back again.
282 memcpy(&tm
, ptm
, sizeof(tm
));
285 if (t0
!= (time_t)-1 )
286 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
287 wxLogSysError(_("mktime() failed"));
291 wxLogSysError(_("gmtime() failed"));
295 wxLogError(_("Failed to get the UTC system time."));
302 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
303 wxLongLong
wxGetLocalTimeMillis()
305 wxLongLong val
= 1000l;
307 // If possible, use a function which avoids conversions from
308 // broken-up time structures to milliseconds
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
314 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
316 SystemTimeToFileTime( &thenst
, &thenft
);
317 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
320 GetLocalTime( &nowst
);
322 SystemTimeToFileTime( &nowst
, &nowft
);
323 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
325 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
327 #elif defined(HAVE_GETTIMEOFDAY)
329 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
332 return (val
+ (tp
.tv_usec
/ 1000));
336 wxLogError(_("wxGetTimeOfDay failed."));
339 #elif defined(HAVE_FTIME)
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...)
346 return (val
+ tp
.millitm
);
347 #elif defined(__WXMAC__)
349 static UInt64 gMilliAtStart
= 0;
351 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() );
353 if ( gMilliAtStart
== 0 )
355 time_t start
= time(NULL
);
356 gMilliAtStart
= ((UInt64
) start
) * 1000000L;
357 gMilliAtStart
-= upTime
.lo
/ 1000 ;
358 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
361 UInt64 millival
= gMilliAtStart
;
362 millival
+= upTime
.lo
/ (1000 * 1000);
363 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / (1000 * 1000);
367 #else // no gettimeofday() nor ftime()
368 // We use wxGetLocalTime() to get the seconds since
369 // 00:00:00 Jan 1st 1970 and then whatever is available
370 // to get millisecond resolution.
372 // NOTE that this might lead to a problem if the clocks
373 // use different sources, so this approach should be
374 // avoided where possible.
376 val
*= wxGetLocalTime();
378 // GRG: This will go soon as all WIN32 seem to have ftime
379 #if defined (__WIN32__)
380 // If your platform/compiler needs to use two different functions
381 // to get ms resolution, please do NOT just shut off these warnings,
382 // drop me a line instead at <guille@iies.es>
383 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
387 val
+= st
.wMilliseconds
;
389 // If your platform/compiler does not support ms resolution please
390 // do NOT just shut off these warnings, drop me a line instead at
393 #if defined(__VISUALC__) || defined (__WATCOMC__)
394 #pragma message("wxStopWatch will be up to second resolution!")
395 #elif defined(__BORLANDC__)
396 #pragma message "wxStopWatch will be up to second resolution!"
398 #warning "wxStopWatch will be up to second resolution!"
404 #endif // time functions
407 #endif // #if wxUSE_STOPWATCH && wxUSE_LONGLONG