]>
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)
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(HAVE_GETTIMEOFDAY)
299 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
302 return (val
+ (tp
.tv_usec
/ 1000));
306 wxLogError(_("wxGetTimeOfDay failed."));
309 #elif defined(HAVE_FTIME)
312 // ftime() is void and not int in some mingw32 headers, so don't
313 // test the return code (well, it shouldn't fail anyhow...)
316 return (val
+ tp
.millitm
);
317 #else // no gettimeofday() nor ftime()
318 // We use wxGetLocalTime() to get the seconds since
319 // 00:00:00 Jan 1st 1970 and then whatever is available
320 // to get millisecond resolution.
322 // NOTE that this might lead to a problem if the clocks
323 // use different sources, so this approach should be
324 // avoided where possible.
326 val
*= wxGetLocalTime();
328 // GRG: This will go soon as all WIN32 seem to have ftime
329 #if defined (__WIN32__)
330 // If your platform/compiler needs to use two different functions
331 // to get ms resolution, please do NOT just shut off these warnings,
332 // drop me a line instead at <guille@iies.es>
333 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
337 val
+= st
.wMilliseconds
;
339 // If your platform/compiler does not support ms resolution please
340 // do NOT just shut off these warnings, drop me a line instead at
343 #if defined(__VISUALC__) || defined (__WATCOMC__)
344 #pragma message("wxStopWatch will be up to second resolution!")
345 #elif defined(__BORLANDC__)
346 #pragma message "wxStopWatch will be up to second resolution!"
348 #warning "wxStopWatch will be up to second resolution!"
354 #endif // time functions
357 #endif // wxUSE_LONGLONG