]>
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)
58 #include <sys/types.h> // for time_t
61 #if defined(HAVE_GETTIMEOFDAY)
64 #elif defined(HAVE_FTIME)
65 #include <sys/timeb.h>
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxTimerEvent
, wxEvent
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // on some really old systems gettimeofday() doesn't have the second argument,
81 // define wxGetTimeOfDay() to hide this difference
82 #ifdef HAVE_GETTIMEOFDAY
83 #ifdef WX_GETTIMEOFDAY_NO_TZ
85 #define wxGetTimeOfDay(tv, tz) gettimeofday(tv)
87 #define wxGetTimeOfDay(tv, tz) gettimeofday((tv), (tz))
89 #endif // HAVE_GETTIMEOFDAY
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
101 void wxTimerBase::Notify()
103 // the base class version generates an event if it has owner - which it
104 // should because otherwise nobody can process timer events
105 wxCHECK_RET( m_owner
, _T("wxTimer::Notify() should be overridden.") );
107 wxTimerEvent
event(m_idTimer
, m_milli
);
108 (void)m_owner
->ProcessEvent(event
);
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 void wxStopWatch::Start(long t
)
119 m_t0
= wxGetLocalTimeMillis() - t
;
123 long wxStopWatch::GetElapsedTime() const
125 return (wxGetLocalTimeMillis() - m_t0
).GetLo();
128 long wxStopWatch::Time() const
130 return (m_pause
? m_pause
: GetElapsedTime());
133 // ----------------------------------------------------------------------------
134 // old timer functions superceded by wxStopWatch
135 // ----------------------------------------------------------------------------
137 static wxLongLong wxStartTime
= 0l;
139 // starts the global timer
142 wxStartTime
= wxGetLocalTimeMillis();
145 // Returns elapsed time in milliseconds
146 long wxGetElapsedTime(bool resetTimer
)
148 wxLongLong oldTime
= wxStartTime
;
149 wxLongLong newTime
= wxGetLocalTimeMillis();
152 wxStartTime
= newTime
;
154 return (newTime
- oldTime
).GetLo();
158 // ----------------------------------------------------------------------------
159 // the functions to get the current time and timezone info
160 // ----------------------------------------------------------------------------
162 // Get local time as seconds since 00:00:00, Jan 1st 1970
163 long wxGetLocalTime()
168 // This cannot be made static because mktime can overwrite it.
170 memset(&tm
, 0, sizeof(tm
));
173 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
177 tm
.tm_isdst
= -1; // let mktime guess
179 // Note that mktime assumes that the struct tm contains local time.
181 t1
= time(&t1
); // now
182 t0
= mktime(&tm
); // origin
184 // Return the difference in seconds.
186 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
187 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
189 wxLogSysError(_("Failed to get the local system time"));
193 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
199 // This cannot be made static because mktime can overwrite it
201 memset(&tm
, 0, sizeof(tm
));
204 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
208 tm
.tm_isdst
= -1; // let mktime guess
210 // Note that mktime assumes that the struct tm contains local time.
212 t1
= time(&t1
); // now
213 t0
= mktime(&tm
); // origin in localtime
215 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
217 // To get t0 as GMT we convert to a struct tm with gmtime,
218 // and then back again.
224 memcpy(&tm
, ptm
, sizeof(tm
));
227 if (t0
!= (time_t)-1 )
228 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
229 wxLogSysError(_("mktime() failed"));
233 wxLogSysError(_("gmtime() failed"));
237 wxLogError(_("Failed to get the UTC system time."));
243 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
244 wxLongLong
wxGetLocalTimeMillis()
246 wxLongLong val
= 1000l;
248 // If possible, use a functin which avoids conversions from
249 // broken-up time structures to milliseconds,
251 #if defined(HAVE_GETTIMEOFDAY)
253 if ( wxGetTimeOfDay(&tp
, (struct timezone
*)NULL
) != -1 )
256 return (val
+ (tp
.tv_usec
/ 1000));
260 wxLogError(_("wxGetTimeOfDay failed."));
263 #elif defined(HAVE_FTIME)
266 // ftime() is void and not int in some mingw32 headers, so don't
267 // test the return code (well, it shouldn't fail anyhow...)
270 return (val
+ tp
.millitm
);
271 #else // no gettimeofday() nor ftime()
272 // We use wxGetLocalTime() to get the seconds since
273 // 00:00:00 Jan 1st 1970 and then whatever is available
274 // to get millisecond resolution.
276 // NOTE that this might lead to a problem if the clocks
277 // use different sources, so this approach should be
278 // avoided where possible.
280 val
*= wxGetLocalTime();
282 #if defined(__VISAGECPP__)
283 // If your platform/compiler needs to use two different functions
284 // to get ms resolution, please do NOT just shut off these warnings,
285 // drop me a line instead at <guille@iies.es>
286 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
289 ::DosGetDateTime(&dt
);
290 val
+= (dt
.hundredths
*10);
291 #elif defined (__WIN32__)
292 // If your platform/compiler needs to use two different functions
293 // to get ms resolution, please do NOT just shut off these warnings,
294 // drop me a line instead at <guille@iies.es>
295 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
299 val
+= st
.wMilliseconds
;
301 // If your platform/compiler does not support ms resolution please
302 // do NOT just shut off these warnings, drop me a line instead at
305 #if defined(__VISUALC__)
306 #pragma message("wxStopWatch will be up to second resolution!")
307 #elif defined(__BORLANDC__)
308 #pragma message "wxStopWatch will be up to second resolution!"
310 #warning "wxStopWatch will be up to second resolution!"
316 #endif // time functions