]>
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 void wxTimerBase::Notify()
115 // the base class version generates an event if it has owner - which it
116 // should because otherwise nobody can process timer events
117 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
119 wxTimerEvent
event(m_idTimer
, m_milli
);
120 (void)m_owner
->ProcessEvent(event
);
123 bool wxTimerBase::Start(int milliseconds
, bool oneShot
)
127 // not stopping the already running timer might work for some
128 // platforms (no problems under MSW) but leads to mysterious crashes
129 // on the others (GTK), so to be on the safe side do it here
133 if ( milliseconds
!= -1 )
135 m_milli
= milliseconds
;
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
151 void wxStopWatch::Start(long t
)
153 m_t0
= wxGetLocalTimeMillis() - t
;
157 long wxStopWatch::GetElapsedTime() const
159 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
162 long wxStopWatch::Time() const
164 return (m_pause
? m_pause
: GetElapsedTime());
167 #endif // wxUSE_LONGLONG
169 // ----------------------------------------------------------------------------
170 // old timer functions superceded by wxStopWatch
171 // ----------------------------------------------------------------------------
175 static wxLongLong wxStartTime
= 0l;
177 // starts the global timer
180 wxStartTime
= wxGetLocalTimeMillis();
183 // Returns elapsed time in milliseconds
184 long wxGetElapsedTime(bool resetTimer
)
186 wxLongLong oldTime
= wxStartTime
;
187 wxLongLong newTime
= wxGetLocalTimeMillis();
190 wxStartTime
= newTime
;
192 return (newTime
- oldTime
).GetLo();
195 #endif // wxUSE_LONGLONG
197 // ----------------------------------------------------------------------------
198 // the functions to get the current time and timezone info
199 // ----------------------------------------------------------------------------
201 // Get local time as seconds since 00:00:00, Jan 1st 1970
202 long wxGetLocalTime()
207 // This cannot be made static because mktime can overwrite it.
209 memset(&tm
, 0, sizeof(tm
));
212 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
216 tm
.tm_isdst
= -1; // let mktime guess
218 // Note that mktime assumes that the struct tm contains local time.
220 t1
= time(&t1
); // now
221 t0
= mktime(&tm
); // origin
223 // Return the difference in seconds.
225 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
226 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
228 wxLogSysError(_("Failed to get the local system time"));
232 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
239 // This cannot be made static because mktime can overwrite it
241 memset(&tm
, 0, sizeof(tm
));
244 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
248 tm
.tm_isdst
= -1; // let mktime guess
250 // Note that mktime assumes that the struct tm contains local time.
252 t1
= time(&t1
); // now
253 t0
= mktime(&tm
); // origin in localtime
255 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
257 // To get t0 as GMT we convert to a struct tm with gmtime,
258 // and then back again.
264 memcpy(&tm
, ptm
, sizeof(tm
));
267 if (t0
!= (time_t)-1 )
268 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
269 wxLogSysError(_("mktime() failed"));
273 wxLogSysError(_("gmtime() failed"));
277 wxLogError(_("Failed to get the UTC system time."));
284 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
285 wxLongLong
wxGetLocalTimeMillis()
287 wxLongLong val
= 1000l;
289 // If possible, use a function which avoids conversions from
290 // broken-up time structures to milliseconds
292 #if defined(HAVE_GETTIMEOFDAY)
294 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
297 return (val
+ (tp
.tv_usec
/ 1000));
301 wxLogError(_("wxGetTimeOfDay failed."));
304 #elif defined(HAVE_FTIME)
307 // ftime() is void and not int in some mingw32 headers, so don't
308 // test the return code (well, it shouldn't fail anyhow...)
311 return (val
+ tp
.millitm
);
312 #else // no gettimeofday() nor ftime()
313 // We use wxGetLocalTime() to get the seconds since
314 // 00:00:00 Jan 1st 1970 and then whatever is available
315 // to get millisecond resolution.
317 // NOTE that this might lead to a problem if the clocks
318 // use different sources, so this approach should be
319 // avoided where possible.
321 val
*= wxGetLocalTime();
323 // GRG: This will go soon as all WIN32 seem to have ftime
324 #if defined (__WIN32__)
325 // If your platform/compiler needs to use two different functions
326 // to get ms resolution, please do NOT just shut off these warnings,
327 // drop me a line instead at <guille@iies.es>
328 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
332 val
+= st
.wMilliseconds
;
334 // If your platform/compiler does not support ms resolution please
335 // do NOT just shut off these warnings, drop me a line instead at
338 #if defined(__VISUALC__) || defined (__WATCOMC__)
339 #pragma message("wxStopWatch will be up to second resolution!")
340 #elif defined(__BORLANDC__)
341 #pragma message "wxStopWatch will be up to second resolution!"
343 #warning "wxStopWatch will be up to second resolution!"
349 #endif // time functions
352 #endif // wxUSE_LONGLONG