]>
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 and Markus Holzem
13 // (c) 1999 Guillermo Rodriguez <guille@iies.es>
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
17 // ============================================================================
19 // ============================================================================
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 #pragma implementation "timerbase.h"
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
42 #include "wx/longlong.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 #if defined(__WIN32__)
52 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__)
56 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
58 # if __IBMCPP__ >= 400
59 # define ftime(x) _ftime(x)
63 #if defined(__MWERKS__) && defined(__WXMSW__)
65 # undef HAVE_GETTIMEOFDAY
70 #include <sys/types.h> // for time_t
73 #if defined(HAVE_GETTIMEOFDAY)
76 #elif defined(HAVE_FTIME)
77 #include <sys/timeb.h>
82 #include <DriverServices.h>
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #if wxUSE_GUI && wxUSE_TIMER
89 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // on some really old systems gettimeofday() doesn't have the second argument,
97 // define wxGetTimeOfDay() to hide this difference
98 #ifdef HAVE_GETTIMEOFDAY
99 #ifdef WX_GETTIMEOFDAY_NO_TZ
101 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
103 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
105 #endif // HAVE_GETTIMEOFDAY
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 #if wxUSE_GUI && wxUSE_TIMER
117 wxTimerBase::~wxTimerBase()
119 // this destructor is required for Darwin
122 void wxTimerBase::Notify()
124 // the base class version generates an event if it has owner - which it
125 // should because otherwise nobody can process timer events
126 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
128 wxTimerEvent
event(m_idTimer
, m_milli
);
129 (void)m_owner
->ProcessEvent(event
);
132 bool wxTimerBase::Start(int milliseconds
, bool oneShot
)
136 // not stopping the already running timer might work for some
137 // platforms (no problems under MSW) but leads to mysterious crashes
138 // on the others (GTK), so to be on the safe side do it here
142 if ( milliseconds
!= -1 )
144 m_milli
= milliseconds
;
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
160 void wxStopWatch::Start(long t
)
162 m_t0
= wxGetLocalTimeMillis() - t
;
166 long wxStopWatch::GetElapsedTime() const
168 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
171 long wxStopWatch::Time() const
173 return m_pauseCount
? m_pause
: GetElapsedTime();
176 #endif // wxUSE_LONGLONG
178 // ----------------------------------------------------------------------------
179 // old timer functions superceded by wxStopWatch
180 // ----------------------------------------------------------------------------
184 static wxLongLong wxStartTime
= 0l;
186 // starts the global timer
189 wxStartTime
= wxGetLocalTimeMillis();
192 // Returns elapsed time in milliseconds
193 long wxGetElapsedTime(bool resetTimer
)
195 wxLongLong oldTime
= wxStartTime
;
196 wxLongLong newTime
= wxGetLocalTimeMillis();
199 wxStartTime
= newTime
;
201 return (newTime
- oldTime
).GetLo();
204 #endif // wxUSE_LONGLONG
206 // ----------------------------------------------------------------------------
207 // the functions to get the current time and timezone info
208 // ----------------------------------------------------------------------------
210 // Get local time as seconds since 00:00:00, Jan 1st 1970
211 long wxGetLocalTime()
216 // This cannot be made static because mktime can overwrite it.
218 memset(&tm
, 0, sizeof(tm
));
221 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
225 tm
.tm_isdst
= -1; // let mktime guess
227 // Note that mktime assumes that the struct tm contains local time.
229 t1
= time(&t1
); // now
230 t0
= mktime(&tm
); // origin
232 // Return the difference in seconds.
234 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
235 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
237 wxLogSysError(_("Failed to get the local system time"));
241 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
248 // This cannot be made static because mktime can overwrite it
250 memset(&tm
, 0, sizeof(tm
));
253 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
257 tm
.tm_isdst
= -1; // let mktime guess
259 // Note that mktime assumes that the struct tm contains local time.
261 t1
= time(&t1
); // now
262 t0
= mktime(&tm
); // origin in localtime
264 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
266 // To get t0 as GMT we convert to a struct tm with gmtime,
267 // and then back again.
273 memcpy(&tm
, ptm
, sizeof(tm
));
276 if (t0
!= (time_t)-1 )
277 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
278 wxLogSysError(_("mktime() failed"));
282 wxLogSysError(_("gmtime() failed"));
286 wxLogError(_("Failed to get the UTC system time."));
293 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
294 wxLongLong
wxGetLocalTimeMillis()
296 wxLongLong val
= 1000l;
298 // If possible, use a function which avoids conversions from
299 // broken-up time structures to milliseconds
301 #if defined(__WXMSW__) && defined(__MWERKS__)
302 // This should probably be the way all WXMSW compilers should do it
303 // Go direct to the OS for time
305 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
307 SystemTimeToFileTime( &thenst
, &thenft
);
308 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
311 GetLocalTime( &nowst
);
313 SystemTimeToFileTime( &nowst
, &nowft
);
314 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
316 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
318 #elif defined(HAVE_GETTIMEOFDAY)
320 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
323 return (val
+ (tp
.tv_usec
/ 1000));
327 wxLogError(_("wxGetTimeOfDay failed."));
330 #elif defined(HAVE_FTIME)
333 // ftime() is void and not int in some mingw32 headers, so don't
334 // test the return code (well, it shouldn't fail anyhow...)
337 return (val
+ tp
.millitm
);
338 #elif defined(__WXMAC__)
340 UInt64 gMilliAtStart
= 0 ;
341 Nanoseconds upTime
= AbsoluteToNanoseconds( UpTime() ) ;
342 if ( gMilliAtStart
== 0 )
344 time_t start
= time(NULL
) ;
345 gMilliAtStart
= ((UInt64
) start
) * 1000L ;
346 gMilliAtStart
-= upTime
.lo
/ 1000 ;
347 gMilliAtStart
-= ( ( (UInt64
) upTime
.hi
) << 32 ) / 1000 ;
349 UInt64 millival
= gMilliAtStart
;
350 millival
+= upTime
.lo
/ 1000 ;
351 millival
+= ( ( (UInt64
) upTime
.hi
) << 32 ) / 1000 ;
354 #else // no gettimeofday() nor ftime()
355 // We use wxGetLocalTime() to get the seconds since
356 // 00:00:00 Jan 1st 1970 and then whatever is available
357 // to get millisecond resolution.
359 // NOTE that this might lead to a problem if the clocks
360 // use different sources, so this approach should be
361 // avoided where possible.
363 val
*= wxGetLocalTime();
365 // GRG: This will go soon as all WIN32 seem to have ftime
366 #if defined (__WIN32__)
367 // If your platform/compiler needs to use two different functions
368 // to get ms resolution, please do NOT just shut off these warnings,
369 // drop me a line instead at <guille@iies.es>
370 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
374 val
+= st
.wMilliseconds
;
376 // If your platform/compiler does not support ms resolution please
377 // do NOT just shut off these warnings, drop me a line instead at
380 #if defined(__VISUALC__) || defined (__WATCOMC__)
381 #pragma message("wxStopWatch will be up to second resolution!")
382 #elif defined(__BORLANDC__)
383 #pragma message "wxStopWatch will be up to second resolution!"
385 #warning "wxStopWatch will be up to second resolution!"
391 #endif // time functions
394 #endif // wxUSE_LONGLONG