]>
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>
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 #if wxUSE_GUI && wxUSE_TIMER
85 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // on some really old systems gettimeofday() doesn't have the second argument,
93 // define wxGetTimeOfDay() to hide this difference
94 #ifdef HAVE_GETTIMEOFDAY
95 #ifdef WX_GETTIMEOFDAY_NO_TZ
97 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
99 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
101 #endif // HAVE_GETTIMEOFDAY
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 #if wxUSE_GUI && wxUSE_TIMER
113 wxTimerBase::~wxTimerBase()
115 // this destructor is required for Darwin
118 void wxTimerBase::Notify()
120 // the base class version generates an event if it has owner - which it
121 // should because otherwise nobody can process timer events
122 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
124 wxTimerEvent
event(m_idTimer
, m_milli
);
125 (void)m_owner
->ProcessEvent(event
);
128 bool wxTimerBase::Start(int milliseconds
, bool oneShot
)
132 // not stopping the already running timer might work for some
133 // platforms (no problems under MSW) but leads to mysterious crashes
134 // on the others (GTK), so to be on the safe side do it here
138 if ( milliseconds
!= -1 )
140 m_milli
= milliseconds
;
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
156 void wxStopWatch::Start(long t
)
158 m_t0
= wxGetLocalTimeMillis() - t
;
162 long wxStopWatch::GetElapsedTime() const
164 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
167 long wxStopWatch::Time() const
169 return m_pauseCount
? m_pause
: GetElapsedTime();
172 #endif // wxUSE_LONGLONG
174 // ----------------------------------------------------------------------------
175 // old timer functions superceded by wxStopWatch
176 // ----------------------------------------------------------------------------
180 static wxLongLong wxStartTime
= 0l;
182 // starts the global timer
185 wxStartTime
= wxGetLocalTimeMillis();
188 // Returns elapsed time in milliseconds
189 long wxGetElapsedTime(bool resetTimer
)
191 wxLongLong oldTime
= wxStartTime
;
192 wxLongLong newTime
= wxGetLocalTimeMillis();
195 wxStartTime
= newTime
;
197 return (newTime
- oldTime
).GetLo();
200 #endif // wxUSE_LONGLONG
202 // ----------------------------------------------------------------------------
203 // the functions to get the current time and timezone info
204 // ----------------------------------------------------------------------------
206 // Get local time as seconds since 00:00:00, Jan 1st 1970
207 long wxGetLocalTime()
212 // This cannot be made static because mktime can overwrite it.
214 memset(&tm
, 0, sizeof(tm
));
217 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
221 tm
.tm_isdst
= -1; // let mktime guess
223 // Note that mktime assumes that the struct tm contains local time.
225 t1
= time(&t1
); // now
226 t0
= mktime(&tm
); // origin
228 // Return the difference in seconds.
230 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
231 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
233 wxLogSysError(_("Failed to get the local system time"));
237 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
244 // This cannot be made static because mktime can overwrite it
246 memset(&tm
, 0, sizeof(tm
));
249 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
253 tm
.tm_isdst
= -1; // let mktime guess
255 // Note that mktime assumes that the struct tm contains local time.
257 t1
= time(&t1
); // now
258 t0
= mktime(&tm
); // origin in localtime
260 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
262 // To get t0 as GMT we convert to a struct tm with gmtime,
263 // and then back again.
269 memcpy(&tm
, ptm
, sizeof(tm
));
272 if (t0
!= (time_t)-1 )
273 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
274 wxLogSysError(_("mktime() failed"));
278 wxLogSysError(_("gmtime() failed"));
282 wxLogError(_("Failed to get the UTC system time."));
289 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
290 wxLongLong
wxGetLocalTimeMillis()
292 wxLongLong val
= 1000l;
294 // If possible, use a function which avoids conversions from
295 // broken-up time structures to milliseconds
297 #if defined(__WXMSW__) && defined(__MWERKS__)
298 // This should probably be the way all WXMSW compilers should do it
299 // Go direct to the OS for time
301 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
303 SystemTimeToFileTime( &thenst
, &thenft
);
304 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
307 GetLocalTime( &nowst
);
309 SystemTimeToFileTime( &nowst
, &nowft
);
310 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
312 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
314 #elif defined(HAVE_GETTIMEOFDAY)
316 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
319 return (val
+ (tp
.tv_usec
/ 1000));
323 wxLogError(_("wxGetTimeOfDay failed."));
326 #elif defined(HAVE_FTIME)
329 // ftime() is void and not int in some mingw32 headers, so don't
330 // test the return code (well, it shouldn't fail anyhow...)
333 return (val
+ tp
.millitm
);
334 #else // no gettimeofday() nor ftime()
335 // We use wxGetLocalTime() to get the seconds since
336 // 00:00:00 Jan 1st 1970 and then whatever is available
337 // to get millisecond resolution.
339 // NOTE that this might lead to a problem if the clocks
340 // use different sources, so this approach should be
341 // avoided where possible.
343 val
*= wxGetLocalTime();
345 // GRG: This will go soon as all WIN32 seem to have ftime
346 #if defined (__WIN32__)
347 // If your platform/compiler needs to use two different functions
348 // to get ms resolution, please do NOT just shut off these warnings,
349 // drop me a line instead at <guille@iies.es>
350 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
354 val
+= st
.wMilliseconds
;
356 // If your platform/compiler does not support ms resolution please
357 // do NOT just shut off these warnings, drop me a line instead at
360 #if defined(__VISUALC__) || defined (__WATCOMC__)
361 #pragma message("wxStopWatch will be up to second resolution!")
362 #elif defined(__BORLANDC__)
363 #pragma message "wxStopWatch will be up to second resolution!"
365 #warning "wxStopWatch will be up to second resolution!"
371 #endif // time functions
374 #endif // wxUSE_LONGLONG