]>
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)
65 #include <sys/types.h> // for time_t
68 #if defined(HAVE_GETTIMEOFDAY)
71 #elif defined(HAVE_FTIME)
72 #include <sys/timeb.h>
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // on some really old systems gettimeofday() doesn't have the second argument,
88 // define wxGetTimeOfDay() to hide this difference
89 #ifdef HAVE_GETTIMEOFDAY
90 #ifdef WX_GETTIMEOFDAY_NO_TZ
92 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
94 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
96 #endif // HAVE_GETTIMEOFDAY
98 // ============================================================================
100 // ============================================================================
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
108 void wxTimerBase::Notify()
110 // the base class version generates an event if it has owner - which it
111 // should because otherwise nobody can process timer events
112 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
114 wxTimerEvent
event(m_idTimer
, m_milli
);
115 (void)m_owner
->ProcessEvent(event
);
118 bool wxTimerBase::Start(int milliseconds
, bool oneShot
)
122 // not stopping the already running timer might work for some
123 // platforms (no problems under MSW) but leads to mysterious crashes
124 // on the others (GTK), so to be on the safe side do it here
128 if ( milliseconds
!= -1 )
130 m_milli
= milliseconds
;
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 void wxStopWatch::Start(long t
)
146 m_t0
= wxGetLocalTimeMillis() - t
;
150 long wxStopWatch::GetElapsedTime() const
152 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
155 long wxStopWatch::Time() const
157 return (m_pause
? m_pause
: GetElapsedTime());
160 // ----------------------------------------------------------------------------
161 // old timer functions superceded by wxStopWatch
162 // ----------------------------------------------------------------------------
164 static wxLongLong wxStartTime
= 0l;
166 // starts the global timer
169 wxStartTime
= wxGetLocalTimeMillis();
172 // Returns elapsed time in milliseconds
173 long wxGetElapsedTime(bool resetTimer
)
175 wxLongLong oldTime
= wxStartTime
;
176 wxLongLong newTime
= wxGetLocalTimeMillis();
179 wxStartTime
= newTime
;
181 return (newTime
- oldTime
).GetLo();
185 // ----------------------------------------------------------------------------
186 // the functions to get the current time and timezone info
187 // ----------------------------------------------------------------------------
189 // Get local time as seconds since 00:00:00, Jan 1st 1970
190 long wxGetLocalTime()
195 // This cannot be made static because mktime can overwrite it.
197 memset(&tm
, 0, sizeof(tm
));
200 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
204 tm
.tm_isdst
= -1; // let mktime guess
206 // Note that mktime assumes that the struct tm contains local time.
208 t1
= time(&t1
); // now
209 t0
= mktime(&tm
); // origin
211 // Return the difference in seconds.
213 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
214 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
216 wxLogSysError(_("Failed to get the local system time"));
220 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
226 // This cannot be made static because mktime can overwrite it
228 memset(&tm
, 0, sizeof(tm
));
231 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
235 tm
.tm_isdst
= -1; // let mktime guess
237 // Note that mktime assumes that the struct tm contains local time.
239 t1
= time(&t1
); // now
240 t0
= mktime(&tm
); // origin in localtime
242 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
244 // To get t0 as GMT we convert to a struct tm with gmtime,
245 // and then back again.
251 memcpy(&tm
, ptm
, sizeof(tm
));
254 if (t0
!= (time_t)-1 )
255 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
256 wxLogSysError(_("mktime() failed"));
260 wxLogSysError(_("gmtime() failed"));
264 wxLogError(_("Failed to get the UTC system time."));
270 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
271 wxLongLong
wxGetLocalTimeMillis()
273 wxLongLong val
= 1000l;
275 // If possible, use a functin which avoids conversions from
276 // broken-up time structures to milliseconds,
278 #if defined(HAVE_GETTIMEOFDAY)
280 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
283 return (val
+ (tp
.tv_usec
/ 1000));
287 wxLogError(_("wxGetTimeOfDay failed."));
290 #elif defined(HAVE_FTIME)
293 // ftime() is void and not int in some mingw32 headers, so don't
294 // test the return code (well, it shouldn't fail anyhow...)
297 return (val
+ tp
.millitm
);
298 #else // no gettimeofday() nor ftime()
299 // We use wxGetLocalTime() to get the seconds since
300 // 00:00:00 Jan 1st 1970 and then whatever is available
301 // to get millisecond resolution.
303 // NOTE that this might lead to a problem if the clocks
304 // use different sources, so this approach should be
305 // avoided where possible.
307 val
*= wxGetLocalTime();
309 // GRG: This will go soon as all WIN32 seem to have ftime
310 #if defined (__WIN32__)
311 // If your platform/compiler needs to use two different functions
312 // to get ms resolution, please do NOT just shut off these warnings,
313 // drop me a line instead at <guille@iies.es>
314 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
318 val
+= st
.wMilliseconds
;
320 // If your platform/compiler does not support ms resolution please
321 // do NOT just shut off these warnings, drop me a line instead at
324 #if defined(__VISUALC__)
325 #pragma message("wxStopWatch will be up to second resolution!")
326 #elif defined(__BORLANDC__)
327 #pragma message "wxStopWatch will be up to second resolution!"
329 #warning "wxStopWatch will be up to second resolution!"
335 #endif // time functions